Populating an UICollectionView to Firebase

Learn2Code

I'm trying to populate a collection view with data from a Firebase Database. I'm making following call in my viewdidload function:

ref = FIRDatabase.database().reference(withPath: "Profiles")

handle = ref.queryOrdered(byChild: title!).observe(.value, with: { snapshot in

    var items: [Profiles] = []

    if snapshot.value is NSNull {

       print("NIL!!")

    } else {
       for item in snapshot.children {

           let profile = Profiles(snapshot: item as! FIRDataSnapshot)

               items.append(profile)
           }

            self.profiles = items
            self.collectionView?.reloadData()
        }
    }) { (error) in
        print(error.localizedDescription)
    }

I'm getting the following error:

fatal error: unexpectedly found nil while unwrapping an Optional value

and the compiler highlights the following piece of code:

handle = ref.queryOrdered(byChild: title!).observe(.value, with: { snapshot in

I'm not understanding what the issue is, and dont understand how to fix it?!

ThunderStruct

It sounds like you're confusing the UIViewController's title member with the Firebase child you're using the query for. If you have a child named title in your data structure and want to query based on that, just replace your query statement with this:

ref.queryOrdered(byChild: "title").observe(.value, with: { snapshot in
    // your code
})

The only way you'd get the error you're getting is if the variable you're using (title) has nil.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Populating UICollectionView in reverse order

Populating UICollectionView cells with UIImageViews in an NSArray

Populating UICollectionView with images is returning nil : Swift 2

Firebase UI delay in populating View?

Populating RecyclerView from firebase database

Populating a ListView with firebase data in android

Populating Android spinner with Firebase Data

Populating a tableviewcontroller that is an "initial view" with data from firebase

populating array with images from a Firebase storage folder

Populating Recycler View From Nested Queries in Firebase

Populating tableView with data obtained from Firebase

Flutter populating data in DropDown from firebase

Populating RecyclerView with two sets of data from Firebase

FireBase Realtime Database : Recycler View not populating

Populating listview data from firebase database

Flutter - populating syncfusion calendar with data from Firebase

Retrieving firebase children and populating them in a UITableView

Why is firebase populating my viewholder with the same image from Firebase Storage

Spread operator with React useState // Populating state from Firebase

Angular 2, Unable to validate form after populating it from firebase

Populating tableview with firebase data (array not updating within .childAdded)

Firebase populating recyclerview error: com.firebase.client.FirebaseException: Failed to bounce to type

Swift segmented contorl show/hide cells in uicollectionview depending on firebase logic

Trying to pass Image from UICollectionView loaded from Firebase, to another VC

Saving Photos from UICollectionView loaded from Firebase Database

UIScrollView or UICollectionView to swipe through images in order called from firebase?

retrieve information from firebase and set as an image and label in UICollectionView

Populating a Spinner with keys from a Firebase database, then another Spinner from the first's selection

Firebase query is not populating array for table view. How can I resolve this issue?