我尝试遍历id列表以获取值。在迭代时,我想将其存储在另一个列表中。迭代后,我尝试使用新列表开始下一个方法。不幸的是,大小始终为0,但是有诅咒值。如果我在循环中将其打印出来,则其大小正确,但之后不正确。这是我的代码:
func getUsersForFences(geofenceObject: [GeofenceObject]){
for fence in geofenceObject{
let ref = Database.database().reference().child("UserInFences").child(fence.fenceName!)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.childrenCount > 0{
for users in snapshot.children.allObjects as! [DataSnapshot]{
let userItem = users.value as? [String: AnyObject]
let userid = users.key as! String
let timestamp = userItem?["timestamp"] as! Int
let timestampValidation = userItem?["timestampValidation"] as! Int
if fence.timestamp! <= timestamp && fence.timestampValidation! <= timestampValidation && timestamp <= fence.timestampValidation! {
let userObjecti = UserFenceObject(timestamp: timestamp, timestampValidation: timestampValidation, userId: userid)
self.userObjects.append(userObjecti)
}else if fence.timestamp! >= timestamp && fence.timestampValidation! >= timestampValidation && fence.timestamp! <= timestampValidation {
let userObject = UserFenceObject(timestamp: timestamp, timestampValidation: timestampValidation, userId: userid)
self.userObjects.append(userObject)
}
}
}
});
}
// Here its always 0
self.getUsersInterest( userObject: userObjects)
}