2015-09-08 52 views
0

我開發了一個朋友關係應用程序,用戶可以添加存在的用戶作爲朋友。 然後我做了搜索功能的朋友,我的表結構如下圖所示: PFUser - >記錄當前用戶(標準級) 好友列表 - >用戶名(我的用戶名),朋友(點PFUser)解析查詢不匹配點pfuser

當用戶添加關係:

 var friendListObject = PFObject(className: "FriendList") 
     friendListObject.setObject(currentUser!.username!, forKey: "myusername") 
     friendListObject.setObject(result.user, forKey: "friend") //result.user = PFUser 
     friendListObject.saveInBackgroundWithBlock{ 
      (success: Bool, error: NSError?) -> Void in 
      if (success) { 

      } 
     } 
時,我想尋找新的朋友

,如果沒有在我的關係,我有下面的代碼使用的朋友,但是,不能正常工作。有什麼建議嗎?

var resultQuery = PFQuery(className: "FriendList") 
    resultQuery.whereKey("myusername", equalTo: currentUser!.username!) 
    resultQuery.selectKeys(["friend"]) 

    var query = PFUser.query() 

    query!.whereKey("username", notEqualTo: currentUser!.username!) 
    query!.whereKey("objectId", doesNotMatchKey: "friend", inQuery: resultQuery) 

    query!.findObjectsInBackgroundWithBlock { 
     (objects: [AnyObject]?, error: NSError?) -> Void in 
    } 

回答

0

我修改了我的代碼時,增加額外的現場 「friendusername」 保持PFUser.currentUser()。用戶名

 var friendListObject = PFObject(className: "FriendList") 
     friendListObject.setObject(currentUser!.username!, forKey: "myusername") 
     friendListObject.setObject(result.user, forKey: "friend") 
     friendListObject.setObject(result.user.objectForKey("username") as! String, forKey: "friendusername") 

搜索時,使用friendusername爲doesnotmatch,那麼它的工作

var resultQuery = PFQuery(className: "FriendList") 
resultQuery.whereKey("myusername", equalTo: currentUser!.username!) 
resultQuery.selectKeys(["friend"]) 

var query = PFUser.query() 

query!.whereKey("username", notEqualTo: currentUser!.username!) 
query!.whereKey("objectId", doesNotMatchKey: "friend", inQuery: resultQuery) 

query!.findObjectsInBackgroundWithBlock { 
    (objects: [AnyObject]?, error: NSError?) -> Void in 
}