Collection-View-in-a-Table-View-Cell icon indicating copy to clipboard operation
Collection-View-in-a-Table-View-Cell copied to clipboard

Count is performed before data is loaded

Open ArvinDaCoda opened this issue 7 years ago • 2 comments

Hi,

I have implemented collectionview a similar way, but are having issues when getting the data from and external database (Google Firebase). The function that counts the collectionview cells are performed before the data is stored in the episodesArray.

I guess I have to perform some delay or run the functions in the same thread, but are pretty newbie at this, so any help is much appreciated. THANKS!!!!

I have separated the code into 2 swift files:

  1. UIViewController.swift

... func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! EpisodesRow cell.categoryID = "siibMVy0vuiXPVW2IZsz" //Temp static solution for testing cell.getEpisodes() return cell } ...

  1. CategoryRow.swift

... //Connect to Firebase and Datamodel let db = Firestore.firestore() var episodesArray : [Episodes] = Episodes

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { print("Count: (episodesArray.count)") return episodesArray.count }

func getEpisodes() { //print(categoryID) db.collection("Episodes").whereField("EpisodeCatID", isEqualTo: categoryID).getDocuments { (snapshot, error) in if error != nil { //error handling print(error!) } else { //Success for document in (snapshot?.documents)! { let episodeData = Episodes() episodeData.EpisodeName = (document.data()["EpisodeName"] as? String)! episodeData.EpisodeImage = (document.data()["EpisodeImage"] as? String)! self.episodesArray.append(episodeData)

                print(" \(self.episodesArray.count)")
                print(" \(self.episodesArray)")
            }
        }
    }
}

...

ArvinDaCoda avatar Jan 30 '18 13:01 ArvinDaCoda

Hey! It looks like this repo hasn't been updated for a while. That probably means the repo's not a high-priority for @ashfurrow. He'll answer this issue if he can, but just a head's up.

If you're using this project, you have the skills to improve it. If you've reported a bug, you are encouraged to open a pull request that fixes it. And of course, you're welcome to discuss with other developers in this repository's issues and pull requests. Have a great day!

This is debug info for @ashfurrow. See https://github.com/ashfurrow/peril-settings/issues/6.
    result.data.pushed_at: 2016-11-09T16:57:54Z
    Date.parse(result.data.pushed_at): 1478710674000
    sixMonthsAgo: 1501420124359
    entire object: {"data":{"id":45502673,"name":"Collection-View-in-a-Table-View-Cell","full_name":"ashfurrow/Collection-View-in-a-Table-View-Cell","owner":{"login":"ashfurrow","id":498212,"avatar_url":"https://avatars3.githubusercontent.com/u/498212?v=4","gravatar_id":"","url":"https://api.github.com/users/ashfurrow","html_url":"https://github.com/ashfurrow","followers_url":"https://api.github.com/users/ashfurrow/followers","following_url":"https://api.github.com/users/ashfurrow/following{/other_user}","gists_url":"https://api.github.com/users/ashfurrow/gists{/gist_id}","starred_url":"https://api.github.com/users/ashfurrow/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ashfurrow/subscriptions","organizations_url":"https://api.github.com/users/ashfurrow/orgs","repos_url":"https://api.github.com/users/ashfurrow/repos","events_url":"https://api.github.com/users/ashfurrow/events{/privacy}","received_events_url":"https://api.github.com/users/ashfurrow/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell","description":"Sample code for my tutorial","fork":false,"url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell","forks_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/forks","keys_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/teams","hooks_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/hooks","issue_events_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/issues/events{/number}","events_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/events","assignees_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/assignees{/user}","branches_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/branches{/branch}","tags_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/tags","blobs_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/git/refs{/sha}","trees_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/statuses/{sha}","languages_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/languages","stargazers_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/stargazers","contributors_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/contributors","subscribers_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/subscribers","subscription_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/subscription","commits_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/commits{/sha}","git_commits_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/git/commits{/sha}","comments_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/comments{/number}","issue_comment_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/issues/comments{/number}","contents_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/contents/{+path}","compare_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/merges","archive_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/downloads","issues_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/issues{/number}","pulls_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/pulls{/number}","milestones_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/milestones{/number}","notifications_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/labels{/name}","releases_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/releases{/id}","deployments_url":"https://api.github.com/repos/ashfurrow/Collection-View-in-a-Table-View-Cell/deployments","created_at":"2015-11-03T23:30:24Z","updated_at":"2018-01-30T07:50:16Z","pushed_at":"2016-11-09T16:57:54Z","git_url":"git://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell.git","ssh_url":"[email protected]:ashfurrow/Collection-View-in-a-Table-View-Cell.git","clone_url":"https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell.git","svn_url":"https://github.com/ashfurrow/Collection-View-in-a-Table-View-Cell","homepage":"http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell-in-swift/","size":15,"stargazers_count":261,"watchers_count":261,"language":"Swift","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":62,"mirror_url":null,"archived":false,"open_issues_count":22,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":62,"open_issues":22,"watchers":261,"default_branch":"master","permissions":{"admin":false,"push":false,"pull":false},"network_count":62,"subscribers_count":14},"meta":{"x-ratelimit-limit":"12500","x-ratelimit-remaining":"12496","x-ratelimit-reset":"1517321320","x-github-request-id":"C472:0C4D:3AF441:92F799:5A706E5C","x-github-media-type":"github.machine-man-preview; format=json","last-modified":"Tue, 30 Jan 2018 07:50:16 GMT","etag":"\"4606d689658f839511ccd7b46d5e0ab1\"","status":"200 OK"}}

    </details>

Generated by :no_entry_sign: dangerJS

ashfurrow-peril[bot] avatar Jan 30 '18 13:01 ashfurrow-peril[bot]

I notice one thing: you put the network code inside the (reusable) table view cell. The view cell can be reused or disposed when scrolling. Therefore, it is not proper way. Instead, the network code shall be put inside view controller, which is a singleton (compare to those table view cells) until users dismiss it.

In your view controller, put up the code to get data. If you prefer to delay the loading till the table view cell appears (thus the collection view is displayed), then you'll need a way to remember if that row is loaded, loading or load failed. When you've got your data ready (or updated), you shall call self.tableView.reloadData().

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! EpisodesRow
    cell.categoryID = "siibMVy0vuiXPVW2IZsz" //Temp static solution for testing
    if (!self.hasLoaded[cell.categoryID]) {
        self.getEpisodes(cell.categoryID)  // Lazy downloading
    }
    cell.collectionView.reloadData()
    return cell
}

Then implement the datasource and delegate functions for collection view inside view controller as usual.

johncpang avatar Sep 06 '19 14:09 johncpang