.observe firebase方法swift4之后代码未执行 [英] Code not executing after .observe firebase method swift4

查看:73
本文介绍了.observe firebase方法swift4之后代码未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结构,其中用户正在注册事件,注册的事件存储如下:

I have a structure in which users are registering for events, and the registered events are stored as the following:

我正在尝试访问registeredEvents并在表视图上显示子级.到目前为止,我已经做到了:

I am trying to access the registeredEvents and display the children on a table view. So far, I have done this:

 override func viewDidLoad() {
    super.viewDidLoad()
    let id = Auth.auth().currentUser?.uid
    let ref = Database.database().reference()
    ref.child("users").child(id!).child("registeredEvents").observeSingleEvent(of: .value, with: { snapshot in
           print(snapshot.childrenCount)
        for rest in snapshot.children.allObjects as! [DataSnapshot] {
               print(rest.value!)
               self.eventsArray.append(rest.value!)
            }
    })

    print(eventsArray)
    self.tblEvents.dataSource = self
    self.tblEvents.delegate = self
}

它会像预期的那样打印所有子代的值,但是在观察到关闭后将不会执行任何操作.它不会打印eventsArray或我在打印语句中放入的任何内容.另外,eventsArray仅包含关闭之前的内容.您能否让我知道为什么会这样,并且需要做些什么来修复它,特别是以代码的形式?

It prints all the children's values like it is supposed to, but it wont do anything after that observe closure. It will not print eventsArray, or anything that I put in a print statement. Also, eventsArray only contains things it had before the closure. Could you please let me know why this is happening, and what needs to be done to fix it, specifically in the form of code?

推荐答案

Firebase是异步的-代码的运行速度比互联网快得多,因此关闭后的代码

Firebase is asynchronous - code runs much faster than the internet so the code following the closure

print(eventsArray)

实际上将在闭包中的代码之前运行.此外,Firebase数据仅在该闭包内部有效,因此请确保您首先在闭包中使用数据.在这种情况下,要纠正它,请尝试

will actually run before the code in the closure. Also, Firebase data is only valid inside that closure so ensure you're working with data in the closure first. In this case to correct it try this

 override func viewDidLoad() {
    super.viewDidLoad()

    self.tblEvents.dataSource = self
    self.tblEvents.delegate = self

    let id = Auth.auth().currentUser?.uid
    let ref = Database.database().reference()
    ref.child("users").child(id!).child("registeredEvents").observeSingleEvent(of: .value, with: { snapshot in
        print(snapshot.childrenCount)
        for rest in snapshot.children.allObjects as! [DataSnapshot] {
            print(rest.value!)
            self.eventsArray.append(rest.value!)
        }
        print(self.eventsArray) //or refresh your tableView here
    })
}

代码将一堆字典中的.value对象传递给eventsArray.您可能需要考虑设计对象来保存该词典数据,然后将这些对象添加到数组中.

The code is passing a bunch of .value objects, which are dictionaries, to your eventsArray. You may want to consider crafting objects to hold that dictionary data and adding those objects to the array instead.

    for restSnap in snapshot.children.allObjects as! [DataSnapshot] {
       let event = EventClass(withSnap: restSnap)
       self.eventsArray.append(event)
    }

dataSource数组看起来像这样

the dataSource array would look like this

var eventsArray = [EventClass]()

和EventClass将是

and the EventClass would be

class EventClass {
    var first_name = ""
    var event_key = ""

    init(withSnap: DataSnapshot) {
        self.event_key = withSnap.key
        self.first_name = withSnap.childSnapshot(forPath: "First name").value ?? "No first name"
        let registeredEventsSnap = withSnap.childSnapshot(forPath: "registeredEvents")
        //iterate over the registeredEventsSnap to get all the events
    }
}

这篇关于.observe firebase方法swift4之后代码未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆