2015-12-02 43 views
1

我正在做一個Facebook圖形調用,以獲得正在使用我的應用程序的用戶的朋友。我從圖形調用中獲得用戶朋友的Facebook ID。下面是我試圖從Parse獲得的那個ID,但是它沒有讓所有的用戶回來,我相信它是一個異步調用。如何保存使用該應用程序的用戶fb朋友的指針數組?提前致謝!!如何創建從Facebook的解析PFUser指針ID

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

這可以通過使用whereKey:containedIn:

let facebookIDs = dataArray.map { $0.objectForKey("id")! } 
let query = PFUser.query()! 
query.whereKey("facebookID", containedIn: facebookIDs) 

查詢現在將包含其Facebook的帳號是傳遞給查詢陣列中的所有用戶都可以簡化成一個單一的查詢。

(swift語法可能不正確沒有仔細檢查)