查询用户名或解析数组,Swift

时间:2015-03-11 09:34:01

标签: ios swift parse-platform

我有一个存储在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)
    }

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()