Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate a collection view with firebase data

I'm trying to recreate a new firebase project where you populate a table view with data from firebase realtime database that contain links to images in firebase storage.

I can populate the tutorial project which is a table view with firebase data. But with my current project it is a collection view inside an extension.

I've narrowed down the issue to my variables

  var ref: FIRDatabaseReference!
  var messages: [FIRDataSnapshot]! = []
  var msglength: NSNumber = 10
  private var _refHandle: FIRDatabaseHandle!

specifically

var messages: [FIRDataSnapshot]! = []

Which I think is an array of my data I get from firebase

I then call a function that should populate that array in my viewdidload()

func loadPosts(){
    self.messages.removeAll()
    // Listen for new messages in the Firebase database
    _refHandle = self.ref.child("messages").observeEventType(.ChildAdded, withBlock: { (snapshot) -> Void in
        //print("1")
        self.messages.append(snapshot)
        //print(self.messages.count)
    })
}

The issue happens when I try to populate my collections view since I want horizontal scrolling I use an extension. In the extension I find that my array of values is always 0, but in my loadPosts() function the count of my >array is the same value as the amount of posts I have in firebase.

extension HomeViewController : UICollectionViewDataSource
{

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return messages.count
}

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

    print(messages.count)

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(StoryBoard.CellIdentifier, forIndexPath: indexPath) as! InterestCollectionViewCell

    // Unpack message from Firebase DataSnapshot
    let messageSnapshot: FIRDataSnapshot! = self.messages[indexPath.row]
    let message = messageSnapshot.value as! Dictionary<String, String>
    let name = message[Constants.MessageFields.name] as String!
    if let imageUrl = message[Constants.MessageFields.imageUrl] {
        if imageUrl.hasPrefix("gs://") {
            FIRStorage.storage().referenceForURL(imageUrl).dataWithMaxSize(INT64_MAX){ (data, error) in
                if let error = error {
                    print("Error downloading: \(error)")
                    return
                }
                cell.featuredImageView?.image = UIImage.init(data: data!)
            }
        } else if let url = NSURL(string:imageUrl), data = NSData(contentsOfURL: url) {
            cell.featuredImageView?.image = UIImage.init(data: data)
        }
        cell.interestTitleLabel?.text = "sent by: \(name)"
    }
        return cell
    }
}

Should I not be using FIRDataSnapshot? If so which is the correct one to use? Or should I approach the project in another form not using extensions?

like image 365
Mark Avatar asked May 20 '16 10:05

Mark


1 Answers

You are correctly inserting the items into your array within the completion block, but you are missing a call to reload your collectionView.

like image 92
Mac Bellingrath Avatar answered Oct 23 '22 06:10

Mac Bellingrath