如何从Facebook ID创建指向Parse PFUser的指针

时间:2015-12-02 23:52:04

标签: ios swift facebook-graph-api parse-platform

我正在进行Facebook图表调用,以获取正在使用我的应用的用户的朋友。我从图表调用中获取用户朋友的Facebook ID。以下是我试图通过该ID从Parse获取的内容,但是它没有让所有用户都回来,我相信,因为它是异步调用。如何保存用户使用该应用程序的朋友的一系列指针?在此先感谢!!

 graphConnection.addRequest(requestFriends, completionHandler: { (connection: FBSDKGraphRequestConnection!, result: AnyObject!, error: NSError!) -> Void in

                     if result.objectForKey("friends") != nil {

                        // parsing dictionary to get results
                        let firstDict = result.objectForKey("friends")
                        let dataArray = firstDict!.objectForKey("data")

                        let myFriendsUsingTheAppCount = dataArray!.count
                        print("\(myFriendsUsingTheAppCount)")

                        let friendsArray:NSMutableArray = []

                        for var i = 0; i < dataArray!.count; i++ {

                           let friend = dataArray![i]

                           let friendFbObjectID = friend.objectForKey("id")!

                           let query = PFUser.query()
                           query!.whereKey("facebookID", equalTo: friendFbObjectID)

                           query!.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

                              if error == nil {

                                 // this is where I was going to save this specific user as a pointer

                              } else {

                                 // some error
                              }
                           })
                           // this is saving just the friend's name/fb id to an array, but I want an array of pointers to the PFUser
                           friendsArray.addObject(friend)
                        }

                     } else {

                        // fb friends is nil
                        print("FB friends came back nil")
                     }
                  })

1 个答案:

答案 0 :(得分:1)

可以使用whereKey:containedIn:

将其简化为单个查询
let facebookIDs = dataArray.map { $0.objectForKey("id")! }
let query = PFUser.query()!
query.whereKey("facebookID", containedIn: facebookIDs)

查询现在将包含其facebook id在数组中传递给查询的所有用户。

(快速语法可能不正确,没有仔细检查)