Swift CollectionView Reload Data Problems - swift

I am using one collectionView and adding multiple data into it. I add numberOfSections to the collectionView with the title "CategoryTitle". I fill these sections with model [section]. I load two strings inside the model variable. So I add data like Model = [[www ..., www ...], [www., Www ..., www]]. But sometimes data is added, sometimes I get an error in numberOfItemsInSection. Sometimes it doesn't load at all. Is it because I used Reload.Data in the wrong place? What is the problem?
class denemeView: UIViewController {
var davetiyeKatIsım = [String]()
var model = [[String]]()
var davetiyefilee = [String]()
var davetiyefilee2 = [String]()
override func viewDidLoad() {
super.viewDidLoad()
davetiyeCEK1()
davetiyecek2()
}
#objc func davetiyeCEK1(){
....
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["davetiyefilee"] as? String { let s = String(describing: soru_baslik)
self.davetiyefilee.append(s)
}
self.model.append(self.davetiyefilee)
self.modelKATE.append(self.davetiyeKatIsım) }
DispatchQueue.main.async { [weak self] in
self?.sonsuzCollec?.reloadData() } }
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["KATEGO.ISIM"] as? String { let s = String(describing: soru_baslik)
self.davetiyeKatIsım.append(s) } }
DispatchQueue.main.async { [weak self] in
self?.sonsuzCollec?.reloadData() }
}
#objc func davetiyecek2(){...
do {
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["KATEGO.ISIM"] as? String {
let s = String(describing: soru_baslik)
self.davetiyeKatIsım2.append(s) } }
DispatchQueue.main.async { [weak self] in
self?.sonsuzCollec?.reloadData() }
}
if let baslik = try JSONSerialization.jsonObject(with: data, options: []) as? [[String: Any]] {
for review in baslik {
if let soru_baslik = review["davetiyefilee"] as? String {
let s = String(describing: soru_baslik)
self.davetiyefilee2.append(s) }
self.model.append(self.davetiyefilee2)
self.modelKATE.append(self.davetiyeKatIsım2)
}
DispatchQueue.main.async { [weak self] in
self?.sonsuzCollec?.reloadData() } }
}
catch let parseError {
let responseString = String(data: data, encoding: .utf8)
print("raw response: \(responseString)")
}
}
}
extension denemeView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
func numberOfSections(in collectionView: UICollectionView) -> Int {
if (collectionView == sonsuzCollec) {
return kategoriIsımYeni.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView == sonsuzCollec) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellSonsuz", for: indexPath) as! sonsuzCell
let urlNew = URL(string: model[indexPath.section][indexPath.item])
cell.davetiyeFoto.sd_setImage(with: urlNew)
return cell
}
}

Related

How do I update collectionview based on added info to firebase

Im having a problem in my app, I am working on a Twitter/Instagram like app that posts a pic and then retrieves it in the "Home" section. But, I cant find a way to make the collection view update when the new post is added in firebase. I tried self.collectionview.reloadData(), it didn't work, I tried self.posts.removeAll() and then self.fetchPosts() to retrieve it again and it didn't work. I've been working on this problem for months and just can't find a possible solution without having to pay $$ for a "short problem".
Thanks in advance!
Here is my code:
#IBOutlet weak var collectionview: UICollectionView!
var posts = [Post]()
var following = [String]()
lazy var refreshControl: UIRefreshControl? = {
let refreshControl = UIRefreshControl()
refreshControl.tintColor = UIColor.gray
refreshControl.addTarget(self, action: #selector(refreshList), for: UIControlEvents.valueChanged)
collectionview.addSubview(refreshControl)
self.collectionview.reloadData()
return refreshControl
}()
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBarItems()
fetchPosts()
collectionview.refreshControl = refreshControl
}
func setupNavigationBarItems() {
navigationItem.title = "Home"
}
#objc func refreshList() {
let deadline = DispatchTime.now() + .milliseconds(1000)
DispatchQueue.main.asyncAfter(deadline: deadline) {
self.collectionview.reloadData()
self.refreshControl?.endRefreshing()
print("reloaded...")
}
self.collectionview.reloadData()
}
#objc func fetchPosts() {
let ref = Database.database().reference()
ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
let users = snapshot.value as! [String : AnyObject]
for (_,value) in users {
if let uid = value["uid"] as? String {
if uid == Auth.auth().currentUser?.uid {
if let followingUsers = value["following"] as? [String : String]{
for (_,user) in followingUsers{
self.following.append(user)
}
}
self.following.append(Auth.auth().currentUser!.uid)
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
let postsSnap = snap.value as! [String : AnyObject]
for (_,post) in postsSnap {
if let userID = post["userID"] as? String {
for each in self.following {
if each == userID {
let posst = Post()
if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String {
posst.author = author
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID
if let people = post["peopleWhoLike"] as? [String : AnyObject] {
for (_,person) in people {
posst.peopleWhoLike.append(person as! String)
}
}
self.posts.append(posst)
}
}
}
self.collectionview.reloadData()
}
}
})
}
}
}
})
ref.removeAllObservers()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.posts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell
cell.postImage.downloadImage(from: self.posts[indexPath.row].pathToImage)
cell.authorLabel.text = self.posts[indexPath.row].author
cell.likeLabel.text = "\(self.posts[indexPath.row].likes!) Likes"
cell.postID = self.posts[indexPath.row].postID
for person in self.posts[indexPath.row].peopleWhoLike {
if person == Auth.auth().currentUser!.uid {
cell.likeBtn.isHidden = true
cell.dislikeBtn.isHidden = false
break
}
}
return cell
}
}
Replace this
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
with
ref.child("posts").observe(of: .childAdded, with: { (snap) in
to be able to listen to every added post

With Firebase, I'd like to get data asynchronously and show on TableView but some of data are duplicated on TableView

With Firebase, I'd like to get data asynchronously, and then show it on TableView, but some of data are duplicated on TableView. I think it's because I use closure in For statement but I'm not sure where I should call it instead.
I tried to call setDataToArray in Dispatchqueue.main.async but it doesn"t work.
Class PostTableViewController: UITableViewController {
let db = Firestore.firestore()
var postArray: [Post] = [Post]()
override func viewDidLoad() {
super.viewDidLoad()
SVProgressHUD.show()
configureTableview()
setDataToArray()
}
func setDataToArray() {
retrievePost() { (posts) in
print(posts!)
for post in posts! {
self.postArray.append(post)
}
print(self.postArray)
self.tableView.reloadData()
SVProgressHUD.dismiss()
}
}
func retrievePost(completion: #escaping ([Post]?) -> Void) {
var posts = [Post]()
let postsColRef = db.collection("posts").order(by: "createdAt")
postsColRef.getDocuments() { (querySnapshot, error) in
if let error = error {
print("Document data: \(error)")
} else {
for document in querySnapshot!.documents {
let data = document.data()
let userId = data["userId"] as? String
let postImage = data["postImageURL"] as? String
let postText = data["postText"] as? String
let createdAt = data["createdAt"] as? String
let numberOfLike = data["numberOfLike"] as? Int
let docRef = self.db.collection("users").document(userId!)
docRef.getDocument() { (document, error) in
if let document = document, document.exists {
let data = document.data()!
let userName = data["userName"] as? String
let userImage = data["userImageURL"] as? String
let post = Post(
userId: userId!,
userName: userName!,
userImageURL: userImage!,
postImageURL: postImage!,
postText: postText!,
createdAt: createdAt!,
numberOfLike: numberOfLike!)
print(post)
posts.append(post)
completion(posts)
}
}
}
}
}
}
#IBAction func cameraButtonPressed(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: "homeToChooseImage", sender: nil)
}
}
extension PostTableViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return postArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! PostTableViewCell
let post = postArray[indexPath.row]
let postImageURL = URL(string: post.postImageURL)
do {
let data = try Data(contentsOf: postImageURL!)
cell.postImage.image = UIImage(data: data)
}catch let err {
print("Error : \(err.localizedDescription)")
}
let userImageURL = URL(string: post.userImageURL)
do {
let data = try Data(contentsOf: userImageURL!)
cell.userImage.image = UIImage(data: data)
}catch let err {
print("Error : \(err.localizedDescription)")
}
cell.postText.text = post.postText
cell.userName.text = post.userName
cell.createdAt.text = post.createdAt
cell.postLikeButton.titleLabel?.text = "\(post.numberOfLike) Likes"
return cell
}
}
I guess where I call "completion(posts)" is wrong.
Where should I call it instead?

How to update table view

I used search bar. It isn't updating the table view.
struct ApiResults:Decodable {
let resultCount: Int
let results: [Music]
}
struct Music:Decodable {
let trackName: String?
let artistName: String?
let artworkUrl60: String?
}
class ItunesDataViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate {
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var tableView: UITableView!
var musicArray:[Music] = []
var mArray:[Music] = []
var filteredData:[Music] = []
var isSearching = false
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
self.searchBar.delegate = self
searchBar.placeholder = "search"
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
{
print("\n\nsearchText : \(searchText)\n\n")
Search(searchTerm: "\(searchText)")
if searchBar.text == nil || searchBar.text == ""
{
isSearching = false
view.endEditing(true)
self.tableView.reloadData()
}
else
{
isSearching = true
filteredData = mArray.filter{$0.artistName == searchText}
self.tableView.reloadData()
}
}
func Search(searchTerm: String)
{
guard let url = URL(string: "https://itunes.apple.com/search?term=\(searchTerm)&attribute=actorTerm&attribute=languageTerm&attribute=allArtistTerm&attribute=tvEpisodeTerm&attribute=shortFilmTerm&attribute=directorTerm&attribute=releaseYearTerm&attribute=titleTerm&attribute=featureFilmTerm&attribute=ratingIndex&attribute=keywordsTerm&attribute=descriptionTerm&attribute=authorTerm&attribute=genreIndex&attribute=mixTerm&attribute=allTrackTerm&attribute=artistTerm&attribute=composerTerm&attribute=tvSeasonTerm&attribute=producerTerm&attribute=ratingTerm&attribute=songTerm&attribute=movieArtistTerm&attribute=showTerm&attribute=movieTerm&attribute=albumTerm") else {return}
URLSession.shared.dataTask(with: url){(data, response, error) in
guard let data = data else {return}
do
{
let apiressults = try JSONDecoder().decode(ApiResults.self, from: data)
for item in apiressults.results
{
if let track_Name = item.trackName, let artist_Name = item.artistName, let artwork_Url60 = item.artworkUrl60
{
let musics = Music(trackName: track_Name, artistName: artist_Name, artworkUrl60: artwork_Url60)
self.musicArray.append(musics)
print(musics.artistName!,"-", musics.trackName!)
}
}
DispatchQueue.main.async
{
self.tableView.reloadData()
}
}
catch let jsonError
{
print("Error:", jsonError)
}
}.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if isSearching
{
return filteredData.count
}
else
{
return mArray.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "musicCell", for: indexPath) as! ItunesDataTableViewCell
if isSearching
{
cell.lblDesc?.text = filteredData[indexPath.row].artistName
cell.lblSongDesc?.text = filteredData[indexPath.row].trackName
let imgString = filteredData[indexPath.row].artworkUrl60!
let imgUrl:URL = URL(string: imgString)!
DispatchQueue.global(qos: .userInitiated).async {
let imageData:NSData = NSData(contentsOf: imgUrl)!
DispatchQueue.main.async {
let image = UIImage(data: imageData as Data)
cell.imgArt?.image = image
}
}
}
else
{
cell.lblDesc?.text = mArray[indexPath.row].artistName
cell.lblSongDesc?.text = mArray[indexPath.row].trackName
let imgString = mArray[indexPath.row].artworkUrl60!
let imgUrl:URL = URL(string: imgString)!
DispatchQueue.global(qos: .userInitiated).async {
let imageData:NSData = NSData(contentsOf: imgUrl)!
DispatchQueue.main.async {
let image = UIImage(data: imageData as Data)
cell.imgArt?.image = image
}
}
}
return cell
}
}
Please clean up your code 😉: You have two different arrays mArray and musicArray.
You are populating musicArray in Search but mArray is used as data source.
Why do you create new Music items from Music items? You can reduce the code to
let apiressults = try JSONDecoder().decode(ApiResults.self, from: data)
self.mArray = apiressults.results
DispatchQueue.main.async {
self.tableView.reloadData()
}
Please change your code in the cellForRowAt delegate method to:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "musicCell", for: indexPath) as! ItunesDataTableViewCell
let tempArray: [Music] = isSearching ? filteredData : musicArray
cell.lblDesc?.text = tempArray[indexPath.row].artistName
cell.lblSongDesc?.text = tempArray[indexPath.row].trackName
guard let imgString = tempArray[indexPath.row].artworkUrl60,
let imgUrl = URL(string: imgString) else {
// Handle properly the fact that there's no image to display
return cell
}
// Review this code as I'm not sure about this double dispatch
// However, please, no force unwrap optionals (!)
DispatchQueue.global(qos: .userInitiated).async {
do {
let imageData = try Data(contentsOf: imgUrl)
DispatchQueue.main.async {
let image = UIImage(data: imageData)
cell.imgArt?.image = image
}
} catch let error {
print("Error with the image URL: ", error)
}
}
return cell
}
See how you don't repeat your code that way?
Furthermore you were not using the right music array, or we don't have all the information to assess what is wrong with this mix of mArray and musicArray.

When tapped on search bar, the app crashes

The app crashes with the following error when the search bar is tapped:
Not able to understand why?
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier ContactCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
This is my code:
import UIKit
class ContactViewController: UITableViewController, UISearchResultsUpdating {
var dataSource: [Contact] = []
var filteredResult = [Contact]()
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
downloadJSONUrl()
}
func downloadJSONUrl() {
let urlString = "https://us-central1-practo-contacts-sample.cloudfunctions.net/get"
let url = NSURL(string: urlString)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response , error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
if let NameArray = jsonObj?.value(forKey: "contacts") as? [[String: Any]] {
for names in NameArray {
var cont = Contact()
if let name = names["name"] as? String {
cont.name = name
}
if let ph = names["number"] as? String {
cont.phoneNumber = ph
}
self.dataSource.append(cont)
}
self.dataSource.sort {$0.name.lowercased() < $1.name.lowercased()}
}
OperationQueue.main.addOperation {
self.tableView.reloadData()
}
}
}).resume()
}
// MARK: - Table view data source
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ContactCell", for: indexPath) as UITableViewCell
let contact = self.dataSource[indexPath.row]
cell.textLabel?.text = contact.name
cell.detailTextLabel?.text = contact.phoneNumber
return cell
}
func updateSearchResults(for searchController: UISearchController) {
if searchController.searchBar.text! == "" {
filteredResult = dataSource
} else {
filteredResult = dataSource.filter { $0.name.lowercased().contains(searchController.searchBar.text!.lowercased()) }
}
self.tableView.reloadData()
}
#IBAction func unwindToContactList(segue: UIStoryboardSegue) {
guard let viewController = segue.source as? AddOrEditViewController else { return }
if let name = viewController.nameTextField.text, let phoneNumber = viewController.phoneNumberTextField.text {
let contact = Contact(name: name, phoneNumber: phoneNumber)
self.dataSource.append(contact)
tableView.reloadData()
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "contactDetailsSegue" {
guard let viewController = segue.destination as? ContactDetialsViewController else {
return
}
guard let indexPath = tableView.indexPathForSelectedRow else { return }
let contact = self.dataSource[indexPath.row]
viewController.contact = contact
}
}
}
Are you sure that you have set the identifier for the cell on the storyboard and the identifier name is the same used on the code "ContactCell"?

Firebase observeSingleEvent continuously duplicate entry being appended in my array

When I refresh the collection view the data duplicates by +1 in my. How can I avoid duplicate entries in my array when I pull to refresh this function?
Also used self.posts.removeAll() still no result.
var posts = [Post]() {
didSet {
collectionView?.reloadData()
}
}
var following = [String]()
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.tintColor = UIColor.gray
refreshControl.addTarget(self, action: #selector(fetchPosts), for: UIControlEvents.valueChanged)
collectionView?.addSubview(refreshControl)
collectionView?.alwaysBounceVertical = true
fetchPosts()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.collectionView.reloadData()
}
func fetchPosts(){
let ref = FIRDatabase.database().reference()
ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
guard let users = snapshot.value as? [String : AnyObject] else {
return
}
print(snapshot.key)
for (_,value) in users {
if let uid = value["uid"] as? String {
if uid == FIRAuth.auth()?.currentUser?.uid {
if let followingUsers = value["following"] as? [String : String]{
for (_,user) in followingUsers{
self.following.append(user)
print(user)
}
}
self.following.append(FIRAuth.auth()!.currentUser!.uid)
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
for postSnapshot in snap.children.allObjects as! [FIRDataSnapshot] {
let post = postSnapshot.value as! [String : AnyObject]
print(snap.key)
if let userID = post["userID"] as? String {
for each in self.following {
if each == userID {
print(each)
let posst = Post()
if let date = post["date"] as? Int, let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String {
posst.date = Int(date)
posst.author = author
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID
print(posst)
if let people = post["peopleWhoLike"] as? [String : AnyObject] {
for (_,person) in people {
posst.peopleWhoLike.append(person as! String)
}
}
var postExist:Bool = false
for post in self.posts {
if post.postID == posst.postID {
postExist = true
break
}
}
if !postExist {
self.posts.append(posst)
}
self.posts.sort(by: {$0.date! > $1.date!})
self.refreshControl.endRefreshing()
}
}
}
self.collectionView.reloadData()
}
}
})
}
}
}
})
ref.removeAllObservers()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PostCell.identifier, for: indexPath) as! PostCell
cell.posts = posts[indexPath.row]
let post = posts[indexPath.row]
for person in post.peopleWhoLike {
if person == FIRAuth.auth()!.currentUser!.uid {
cell.like.isHidden = true
cell.unlike.isHidden = false
break
}
}
return cell
}
}
Updated with the solutions.
I take it that when the refresh is activated, this function is called and that all the posts are downloaded. For your duplicate problem, it seems that you keep on appending data to your posts array without removing the old data.
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
let postsSnap = snap.value as! [String : AnyObject]
self.posts.removeAll() // This will remove previously downloaded posts.
// All your other code ...