我有一个存储在Parse中的数组,朋友和朋友都有用户名,我需要查询每个用户名并从用户那里获取信息并将这些信息存储在不同的数组中,这就是我现在所拥有的。它工作但它真的很慢,因为它由于某种原因它查询7次。提前谢谢!
var friends: [AnyObject] = user["friends"] as Array
var bioArray = [""]
var statusArray = [""]
var pictureArray = [] as NSMutableArray
var pictureUrl = [""]
for(var i = 0; i < friends.count; i++){
var query = PFUser.query()
query.whereKey("username", equalTo:friends[i])
var friendsArray = query.findObjects()
var bios : AnyObject = friendsArray[0]["bio"] as String
var status : AnyObject = friendsArray[0]["status"] as NSObject
var profilePicture: AnyObject = friendsArray[0]["profilePicture"] as PFFile
bioArray.insert("\(bios)", atIndex: bioArray.count-1)
statusArray.insert("\(status)", atIndex: statusArray.count-1)
pictureArray.addObject(profilePicture)
pictureUrl.insert(pictureArray[0].url, atIndex: pictureUrl.count-1)
}
答案 0 :(得分:0)
这是因为每次循环播放数组时都会创建一个新查询。
尝试这样
var friends: [AnyObject] = user["friends"] as Array
var bioArray = [""]
var statusArray = [""]
var pictureArray = [] as NSMutableArray
var pictureUrl = [""]
for var object in friends {
if friend is objects as? PFUser() {
var bios = friend["bio"]
// add it to the array etc
}
}
如果您没有获得所有用户信息,可以调用函数fetchIfNeeded
friend.fetchIfNeeded()