2015-12-08 69 views
0

我正在使用解析來嘗試創建一個instagram風格的應用程序,用戶可以關注其他用戶。下面的代碼嘗試查詢我的分析數據庫,並檢查登錄的用戶'是否'追隨'另一個用戶。它目前進入你可以看到的地方「輸入這裏但不是APPEND」。兩個問題: 1.它正在爲所有用戶輸入此位置,而不僅僅是當前登錄的用戶遵循的位置 2. append.isfollowing不起作用。最初,isfollowing.count是0,並且當我再次打印它時,它仍然是0.即使它進入if語句它包含在,附加不起作用 代碼如下 - 謝謝!多個查詢返回不正確的結果

var usernames = [""] 
var userids = [""] 
var isFollowing = [Bool]() 

override func viewDidLoad() { 
    super.viewDidLoad() 


    var query = PFUser.query() 
    query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 

     //check if we have any objects 
     if let users = objects { 

      //for each element in the users array 
      for object in users { 

       //convert to PFuser 
       if let user = object as? PFUser { 

         //check if the current user is following each user. if so update isfollowing array 
         var query = PFQuery(className: "followers") 

         query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!) 
         query.whereKey("following", equalTo: user.objectId!) 


         query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in 
          //if objects returns something 
          if let objects = objects { 

           //if we enter here we know that the logged in user is following the curr user. Update the isfollowing array 
           //ENTERS HERE BUT DOES NOT APPEND 
           self.isFollowing.append(true) 

          } else { 
           self.isFollowing.append(false) 
          } 
         }) 
       } 
      } 
     } 
     //reload the table data 
     self.tableView.reloadData() 
    }) 

     print(self.isFollowing.count) 

回答

0
  1. 它進入這個地點的所有用戶,而不僅僅是當前登錄的

條件由內部查詢測試的,if let objects = objects測試如果數組被定義(非零)。你想要的是測試如果數組是非零和非空,如:

if objects?.count > 0 { // we found at least one 
  • 的append.isfollowing不工作
  • 附加到isFollowing的代碼異步運行。 viewDidLoad結尾處的print語句立即運行,之前查詢已完成。這就是爲什麼它似乎不工作(它只是沒有工作)。

    相關問題