2014-11-05 81 views
0

我有一個像instagram這樣的應用程序,我必須在用戶之間建立關係。 爲此,我決定使用連接表方法。 這只是一個名爲「活動」的表,其中我創建了「fromUser」,「toUser」行和活動類型「type」的行,在這種情況下設置爲「followingAction」。Swift + Parse.com:如何使用連接表獲取用戶關係

在這裏,我如何設置表:

var activity = PFObject(className:"Activity") 

     let currentUser = PFUser.currentUser() 

     let follow : String = "followingAction" 

     activity.setObject(currentUser, forKey: "fromUser") 
     activity.setObject(user, forKey: "toUser") // user is a another PFUser in my app 

     activity.setObject(follow, forKey: "type") 

     activity.saveEventually() 

好了,現在我想獲取所有用戶,即時通訊目前正在跟蹤(currentUser),並在的tableView

顯示他們繼DOC https://parse.com/docs/relations_guide#manytomany-jointables

我做了這個,但它只給我一個只包含用戶objectId的用戶數組,我不能在普通用戶表中設置電子郵件名稱:

 func loadUser() { 

    followingUserList.removeAllObjects() 

    let findUserObjectId =  PFQuery(className: "Activity") 
    findUserObjectId.whereKey("fromUser", equalTo: userPassed) 
    findUserObjectId.whereKey("type", equalTo: "followingAction") 


    findUserObjectId.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in 
     if error == nil { 
      // The find succeeded. 
      println("succesfully loaded the fromUser in Activity class") 
      // Do something with the found objects 
      for object in objects { 

       let user : PFUser = object["toUser"] as PFUser 



         self.followingUserList.addObject(user) 
         println("User added to following user list : \(user)") 
         println("followingUserlist = \(self.followingUserList)") 

         self.tableView.reloadData() 


      } } else { 
      // Log details of the failure 
      println("error loadind user ") 
      println(error) 
     } 
    } 
} 

如果我打印followingUserList這是我把獲取用戶的陣列,這裏是我有:

followingUserlist = (
"<PFUser: 0x7d9b93c0, objectId: DQJihBpW5E, localId: (null)> {\n}" 
) 

當我進行常規查詢(PFUser.query())的定期用戶表,我有更多的細節:

例如:

<PFUser: 0x7db5af80, objectId: niwftRrB5x, localId: (null)> { 
backgroundImage = "<PFFile: 0x7db5f080>"; 
email = "[email protected]"; 
emailVerified = 0; 
profileImage = "<PFFile: 0x7db62910>"; 
username = kiki; 
} 

在這裏我們可以看到我有完整的PFUser用它發送電子郵件,用戶名等等等等

因爲我的「Activity」表中的「fromRow」是一個指向我常規用戶表的指針,爲什麼loadUser()方法中的獲取結果不完整?

回答

1

好吧,我終於找到了解決辦法,我只是用getObjectInBackgroundWithId方法是這樣的:

func loadUser() { 

    followingUserList.removeAllObjects() 

    let findUserObjectId =  PFQuery(className: "Activity") 
    findUserObjectId.whereKey("fromUser", equalTo: userPassed) 
    findUserObjectId.whereKey("type", equalTo: "followingAction") 


    findUserObjectId.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in 
     if error == nil { 
      // The find succeeded. 
      println("succesfully loaded the fromUser in Activity class") 
      // Do something with the found objects 
      for object in objects { 

       let user : PFUser = object["toUser"] as PFUser 

       let queryUsers = PFUser.query() 
       queryUsers.getObjectInBackgroundWithId(user.objectId, block: { (userGet :PFObject!,error : NSError!) -> Void in 
        self.followingUserList.addObject(userGet) 
        self.tableView.reloadData() 

       }) 


      } } else { 
      // Log details of the failure 
      println("error loadind user ") 
      println(error) 
     } 
    } 
} 
+0

是「followingUserList」你初始化代碼中的某個地方的陣列? – kareem 2015-01-16 08:45:54

相關問題