Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting data out of a closure that retrieves data from firebase

I am trying to retrieve data from Firebase and store that data outside of the closure that retrieves that data.

    var stringNames = [String] ()
    ref?.observeEventType(.Value, withBlock: { snapshot in
        var newNames: [String] = []
        for item in snapshot.children {
            if let item = item as? FIRDataSnapshot {
                let postDict = item.value as! [String: String]
                newNames.append(postDict["name"]!)
            }
        }
        stringNames = newNames
    })
    print(stringNames)

stringNames comes back empty, but when I print from inside the closure it has the correct data. Any help would be much appreciated, thank you!

like image 896
Ted Marchildon Avatar asked Jul 14 '16 01:07

Ted Marchildon


People also ask

Can we retrieve data from Firebase?

Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.

How do I save and recover data from Firebase?

Go to the Firebase Console, select your project and click on the Database tab. Scroll down and you will find a Realtime Database section. Click on Create database, select the Start in test mode option and then click Enable.

How do I fetch data from Firebase in react native?

First, create a file called firebase-config. js in the root directory of your project to implement Firebase configuration and initialization. Now, go to the Firebase website. Click on the Get Started button and you will be taken to a page where you can create a new project.


1 Answers

That's because when you fetch data from Firebase the call is Asynchronous. What you can do:

Option 1 - Set your logic inside the closure (Like what you have that print the var inside the closure).

Option 2 - Define your own closure that going to receive your data like:

func myMethod(success:([String])->Void){

    ref?.observeEventType(.Value, withBlock: { snapshot in
        var newNames: [String] = []
        for item in snapshot.children {
            if let item = item as? FIRDataSnapshot {
                let postDict = item.value as! [String: String]
                newNames.append(postDict["name"]!)
            }
        }
        success(newNames)
    })
}

Option 3 - Use the delegate pattern

protocol MyDelegate{
     func didFetchData(data:[String])
}

class MyController : UIViewController, MyDelegate{

    func myMethod(success:([String])->Void){
        ref?.observeEventType(.Value, withBlock: { snapshot in
           var newNames: [String] = []
           for item in snapshot.children {
               if let item = item as? FIRDataSnapshot {
                   let postDict = item.value as! [String: String]
                   newNames.append(postDict["name"]!)
               }
            }
            self.didFetchData(newNames)
        })
    }

    func didFetchData(data:[String]){
        //Do what you want
    }

}
like image 113
José Roberto Abreu Avatar answered Nov 14 '22 20:11

José Roberto Abreu