Firebase Observe called after following command

后端 未结 1 1104
清酒与你
清酒与你 2021-01-28 06:59

I am trying to load a username form a Firebase which is than supposed to be set in an Object. But the Firebase Observe Command is getting called after the name already gets set.

相关标签:
1条回答
  • 2021-01-28 07:47

    Firebase loads data from its database asynchronously. This means that the code executes in a different order from what you may expect. The easiest way to see this is with some log statements:

    let ref = Database.database().reference().child("Users").child(currentMessage.senderId).child("name")
    print("Before attaching observer")
    ref.observeSingleEvent(of: .value, with: { (snapshot) in
        print("In completion handler")
    })
    print("After attaching observer")
    

    Unlike what you may expect, this code prints:

    Before attaching observer

    After attaching observer

    In completion handler

    This happens because loading data from Firebase (or any other web service) may take some time. If the code would wait, it would be keeping your user from interacting with your application. So instead, Firebase loads the data in the background, and lets your code continue. Then when the data is available, it calls your completion handler.

    The easiest way to get used to this paradigm is to reframe your problems. Instead of saying "first load the data, then add it to the list", frame your problem as "start loading the data. when the data is loaded, add it to the list".

    In code this means that you move any code that needs the data into the completion handler:

    let ref = Database.database().reference().child("Users").child(currentMessage.senderId).child("name")    
    ref.observeSingleEvent(of: .value, with: { (snapshot) in    
        self.username = snapshot.value as! String
        let nameModel = NameModel(name: self.username, uid: *some UID*)
        decoratedItems.append(DecoratedChatItem(chatItem: nameModel, decorationAttributes: nil))
    })
    

    For some more questions on this topic, see:

    • Swift: Wait for Firebase to load before return a function
    • Can Swift return value from an async Void-returning block?
    • Array items are deleted after exiting 'while' loop?
    • Asynchronous functions and Firebase with Swift 3
    0 讨论(0)
提交回复
热议问题