How to return firebase data using a class?

Question!

I have a UICollectionView where I populate data using a class. As of now, I am populating it with dummy data, as shown in the code below. I have trying to replace this dummy data with data from my Firebase database.

import UIKit

class MrNobody
{
// MARK: - Public API
var title = ""
var description = ""
var numberOfMembers = 0
var numberOfPosts = 0
var featuredImage: UIImage!

init(title: String, description: String, featuredImage: UIImage!)
{
    self.title = title
    self.description = description
    self.featuredImage = featuredImage
    numberOfMembers = 1
    numberOfPosts = 1
}

// MARK: - Private
// dummy data
static func createMrNobody() -> [MrNobody]
{
    return [
        MrNobody(title: "some text" , description: "some text", featuredImage: UIImage(named: "r1")!),
        MrNobody(title: "some text" , description: "some text", featuredImage: UIImage(named: "r1")!),
        MrNobody(title: "some text" , description: "some text", featuredImage: UIImage(named: "r1")!),

    ]
}
}

In order to include data, I have included import Firebase, identifying my firebase database using let ref = Firebase(url: "my app url") and reading data using

 ref.observeEventType(.Value, withBlock: { snapshot in
print(snapshot.value)
}, withCancelBlock: { error in
print(error.description)
})

I am able to read the child data, but I am not being able to use it in the return block of the function. Ideally, I would want to use enumerator = snapshot.childrenCount, and reiteratively use the return function for as many children are there in the node of interest.

Any help/direction will be greatly appreciated.



Answers

The createMrNobody() function call is synchronous and will not behave the same way when you are retrieving data from Firebase.

Firebase is asynchronous in nature so when you ask it for data, you have to wait for that data to be retrieved before processing it. Otherwise the processing code will execute before Firebase has the data ready.

It's different than a SQL query of function call where the entire process is synchronous.

Not sure if this will help, but here's a sample: Suppose we have an array that stores people and we need to populate it from Firebase.

We first define the person class

class Person {
        var title: NSString?
        var aDescription: NSString?
    }

and the array to store the people

var myArray = [Person]()

and when you are ready to load them from Firebase, this will do it

func loadMyPeople() {

    myUsersRef.observeSingleEventOfType(.Value, withBlock: { snapshot in

        for child in snapshot.children {
            let title = child.value["title"] as! String
            let description = child.value["description"] as! String
            let dude = Person()
            dude.title = title
            dude.aDescription = description
            self.myArray.append(dude)
        }

        //after all your people are loaded in the array, print it's contents
        // you could also reload a tableView here as well.
        for dude in self.myArray {
            print("email: \(dude.title!)  desc: \(dude.description!)")
        }
    })
}
By : Jay


This video can help you solving your question :)
By: admin