SethG
SethG

Reputation: 41

Firebase child added in realtime not updating in collection view

After reading through a bunch of other questions on SO about reloading Firebase observeEventType observers, I am pretty confused. I am adding my observer dictionaries to a variable accessible within the entire controller. I then assign that value to the data source dictionary and it loads all of the previously added children.

However, once I try to add new values from another simulator or manually inputting within the backend, my global dictionary updates with the new value, but my data source variable does not. Once I leave the controller it will eventually update, but it defeats the purpose of using Firebase.

I think an open observer should be in viewWillAppear, but a bunch of sources online seemed to have it in viewDidLoad.

I am using a segmented control to go through each custom class, which may be causing the issue. The setup is one collection view controller whose cells are custom collection views that are cells as well.

        override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)
            self.loadFireData()
        }

    func loadFireData() {
    if let locationId = location.locationId {

        let postQuery = ref.child("Posts").child(selectedRegion).child(locationId).queryOrderedByChild("descTime")
        postQuery.observeEventType(.ChildAdded, withBlock: { (snap) in


            if snap.exists() {
                let postQuery = snap.value! as! [String: AnyObject]

                let feedPost = FeedModel()

                feedPost.value = postQuery["value"] as? String

                feedPost.key = postQuery["key"] as? Int
                feedPost.balance = postQuery["balance"] as? Double
                self.post.append(feedPost)

                dispatch_async(dispatch_get_main_queue(), {
                    self.collectionView!.reloadData()
                })
            } 

            }

        })
    }

}

     override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if indexPath.section == 1 {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(feedCellId, forIndexPath: indexPath) as! LocationFeedCell
        cell.location = location
         //this doesn't seem to be updating here 
        cell.posts = posts

        return cell
    }

Any help will be greatly appreciated

Upvotes: 2

Views: 767

Answers (1)

Emre Esen
Emre Esen

Reputation: 137

you are only adding feetpost into post array. So after reload collectionview, you should add newest feetpost in to your custom cell's label according to indexpath.row

if indexPath.section == 1 {
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(feedCellId, forIndexPath: indexPath) as! LocationFeedCell
            cell.location = location
             //Replace this below line: 
            cell.posts = post[indexpath.row] as! String

            return cell
        }

Upvotes: 1

Related Questions