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
. I then return users.count
like this:
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 (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. 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 .
thats why you get nil, count 0 in users array.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.