Reputation: 1213
I'm trying to pull data from Firebase sorted by timestamp and display it in a collection view.
Here's most of the code I'm using:
static func loadPic(picture: String, loadSuccessBlock: @escaping (_ img: UIImage) -> Void) {
let requested = URLRequest(url: URL(string: picture)!)
URLSession.shared.dataTask(with: requested) {data, response, err in
if err != nil {
print(picture, err)
} else {
DispatchQueue.main.async {
if let imageToCache = UIImage(data: data!) {
MainVC.imageCache.setObject(imageToCache, forKey: picture as NSString)
loadSuccessBlock(imageToCache)
}
}
}
}.resume()
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollVCCell", for: indexPath) as! CollViewCell
let post = initiationsArray[indexPath.row]
let picture = NSString(string: post.imageURL!)
if let imageFromCache = MainVC.imageCache.object(forKey: picture as NSString) {
cell.cellImage.image = imageFromCache
} else {
MainVC.loadPic(picture: picture as String, loadSuccessBlock: { img in
// to prevent case of updating wrong cell
// we request to reload cell data
if (nil != self.collectionView.cellForItem(at: indexPath)) {
self.collectionView.reloadItems(at: [indexPath])
}
})
}
return cell
}
firstRef.queryOrdered(byChild: "timestamp").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
secondRef.child(snap.key).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
if let dict = snapshot.value as? Dictionary<String, Any>{
let key = snapshot.key
let post = Post(postKey: key, postData: dict)
DispatchQueue.main.async {
self.initiationsArray.insert(post, at: 0)
self.collectionView.reloadData()
}
}
})
}
}
So the first bit is how I'm handling downloading/pulling from the cache, second bit is my cellForRow, and third bit is my call to Firebase.
What makes this hard to debug is that sometimes the posts are in order, and sometimes they're not. Usually when I completely clear out all of my firebase data and start fresh and then start adding posts, they load in at the correct position. When I come back the next day or I close out of the app and reload it, the posts normally start to load in either randomly in the list or start inserting at the end of the list..which doesn't make sense because I'm explicitly saying to insert at 0.
100% of the time I fully reset my data and start fresh the posts load in correctly though.
Any ideas how to fix this or what might be causing this?
Upvotes: 0
Views: 100
Reputation: 131481
The order in which collection views and table views ask their data sources for cells is not defined. When you first display a table view it usually asks for cells from lowest section to highest section, and lowest row to highest row in each section, but you should not assume that. I haven't played with collection views enough to know in what order they typically fetch cells. Again, though, the order is not defined and subject to change, and you should not assume any particular order.
Upvotes: 1