使用一个数组中的解析服务器加载数据

时间:2018-01-12 15:20:37

标签: arrays swift parse-platform

我正在尝试使用解析服务器加载数据,但我不知道我使用的是哪种数组。我想构建类似于Snapchat的应用程序 - 用户拥有的所有帖子应该在一个数组中。我希望你明白我在说什么。

这是一个例子:

userArr = [
        ["name" : "Keila Maney", "pro-image" : "pro-img-3",
             "items": [["content" : "image", "item" : "img-3"], ["content" : "video", "item" : "output"], ["content" : "video", "item" : "output2"]]],

        ["name" : "Gilberto", "pro-image" : "pro-img-1",
             "items": [["content" : "video", "item" : "output3"], ["content" : "image", "item" : "img-4"], ["content" : "image", "item" : "img-5"], ["content" : "video", "item" : "output"]]],
        ["name" : "Jonathan", "pro-image" : "pro-img-2",

             "items": [["content" : "image", "item" : "img-1"], ["content" : "video", "item" : "output2"]]],

        ["name" : "Delmer", "pro-image" : "pro-img-4",
             "items": [["content" : "image", "item" : "img-2"], ["content" : "video", "item" : "output"], ["content" : "image", "item" : "img-3"]]],

        ["name" : "Carolyne", "pro-image" : "pro-img-3",
             "items": [["content" : "video", "item" : "output"], ["content" : "image", "item" : "img-4"], ["content" : "video", "item" : "output3"], ["content" : "image", "item" : "img-3"]]],

        ["name" : "Sabine", "pro-image" : "pro-img-5",
             "items": [["content" : "video", "item" : "output2"], ["content" : "image", "item" : "img-5"], ["content" : "video", "item" : "output3"]]],
    ]
}

1 个答案:

答案 0 :(得分:2)

嗨,第一步是获取朋友列表,然后是所有朋友的帖子。

您必须使用PFQuery对象和方法findObjectsInBackground请求当前用户的Friend列表。在答案区,您将收到一组PFUser。只需运行for循环并获取所有朋友的帖子。就是这样。

func getProfileTimeLine(completion:@escaping(_ posts: [Post]?, _ error:Error?)->Void){

 let query = PFQuery(className: Post.className)

 query.order(byDescending:"updatedAt")
    query.whereKey("author", equalTo: Friend)
    query.includeKey("author")
    query.limit = 20

    // fetch data asynchronously
    query.findObjectsInBackground { (friendPosts, error) in
        if error == nil{
            if let postsObjects = friendPosts{
                completion(postsObjects, error)
            }
        }else{
            completion(nil, error)
        }
    }
}