本文介绍了在执行以下命令后调用Firebase Observe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Firebase加载用户名,而该用户名不应该在对象中设置.但是,在设置名称后,将调用Firebase Observe Command.有什么问题,我该如何解决?

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. What is the problem and how can I fix it?

let ref = Database.database().reference().child("Users").child(currentMessage.senderId).child("name")

ref.observeSingleEvent(of: .value, with: { (snapshot) in

    // This is supposed to be called first
    self.username = snapshot.value as! String
    print(self.username)
})

// This somehow gets called first
let nameModel = NameModel(name: self.username, uid: *some UID*)
decoratedItems.append(DecoratedChatItem(chatItem: nameModel, decorationAttributes: nil))

推荐答案

Firebase从其数据库异步加载数据.这意味着代码以与您期望的顺序不同的顺序执行.最简单的方法是使用一些日志语句:

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:

附加观察者之后

在完成处理程序中

之所以发生这种情况,是因为从Firebase(或任何其他Web服务)加载数据可能需要一些时间.如果代码等待,它将阻止您的用户与您的应用程序进行交互.因此,Firebase会在后台加载数据,并让您的代码继续.然后,当数据可用时,它将调用您的完成处理程序.

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

这篇关于在执行以下命令后调用Firebase Observe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:54