简体   繁体   English

表格视图行数功能不返回数组项是否正确计数?

[英]Table View Number of Rows Function Not returning array items count correctly?

I am adding a bunch of data into any array like this: 我将一堆数据添加到任何这样的数组中:

for (index, location) in locations.enumerate() {

    FIRDatabase.database().reference().child("users").child(location.key as! String).observeEventType(.ChildAdded, withBlock: { (snapshot: FIRDataSnapshot) in
        if(snapshot.exists()){
            print(snapshot)
            if let dictionary = snapshot.value  as? [String: AnyObject] {
                let user = User()
                user.id = snapshot.key
                user.setValuesForKeysWithDictionary(dictionary)

                self.users.append(user)

                print(user)

                dispatch_async(dispatch_get_main_queue(), {
                    self.tableView.reloadData()
                })

            }
        }
    }, withCancelBlock: nil)
}

All of this data is being added into an array called users . 所有这些数据都被添加到名为users的数组中。 I then return users.count like this: 然后,我像这样返回users.count

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return users.count
}

For some random reason though whenever my data appends to the array of users, the number of rows function stops being called. 尽管出于某种随机原因,但是每当我的数据追加到用户数组时,行数函数就会停止调用。

This picture is some print output stuff. 这张图片是一些打印输出的东西。 It shows you the the count of the items in the array and how it stops right when all the data of the users is appended: 它显示了数组中项目的数量,以及在附加用户的所有数据后如何立即停止:

图片

Does anyone know why this may be happening? 有谁知道为什么会这样? Any help would be appreciated! 任何帮助,将不胜感激!

In for loop FMDatabase block ,If you use your code like 在for循环FMDatabase块中,如果您使用如下代码

for (index, location) in locations.enumerate(){
 // add element to user array  in anotherBlock 
} 

and then 接着

[tableview reloadData]

then you may get count 0 or nil in Users Array. 那么您可能在Users Array中获得计数0或nil。 Because In loop you try to get object using completion , and if Database Block in another thread then current thread execute next code rather than wait for completion . 因为在In循环中,您尝试使用完成来获取对象,并且如果Database Block在另一个线程中,则当前线程将执行下一个代码,而不是等待完成。

thats why you get nil, count 0 in users array. 那就是为什么你得到零,在用户数组中计数0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM