2016-02-03 53 views
0

我正面臨Parse問題。如何在Swift中解析嵌套查詢

我有三類:用戶帖子喜歡

我使用指針爲喜歡用戶指向用戶類和指點到的帖子 class。

Likes class

我檢索所有的帖子,然後我想看看如果當前用戶喜歡的文章或沒有,對於我嵌套查詢。我遇到的問題是帖子沒有用像布爾值更新。我使用的代碼:

func loaddata(limit:Int, skip:Int) { 
    MainFunctions.getphones{(phones) -> Void in 
     var indxesPath:[NSIndexPath] = [NSIndexPath]() 
     let query = PFQuery(className: "Feed") 
     query.whereKey("username", containedIn: phones) 
     query.limit = limit 
     query.skip = skip 
     query.includeKey("from") 
     query.addDescendingOrder("createdAt") 
     query.findObjectsInBackgroundWithBlock { 
      (objects: [PFObject]?, error: NSError?) -> Void in 

     if error == nil { 
      if let objects = objects { 
       if objects.isEmpty { 

       } 
       else { 

        for object in objects { 
         let Date = object.createdAt 
         let post = Post(time: Date!) 
         let Type = object.objectForKey("type") as? String 
         post.Post = object 
         post.Post_message = object.objectForKey("Text") as? String 
         post.comments = object.objectForKey("commentaires") as? Int 
         post.likes = object.objectForKey("likes") as? Int 

         // See if the current user likes a post 

         let query_like = PFQuery(className:"Likes") 
         query_like.whereKey("Post", equalTo: object) 
         query_like.whereKey("User", equalTo: PFUser.currentUser()!) 
         query_like.getFirstObjectInBackgroundWithBlock{ 
          (like: PFObject?, error: NSError?) -> Void in 
          if error == nil && like != nil { 
           post.DoILike = true 
          } else { 
           post.DoILike = false 
          } 
         } 

         self.posts.append(post) 
         indxesPath.append(NSIndexPath(forRow: self.posts.count - 1, inSection: 0)) 


        } 

        self.tableView.beginUpdates() 
        self.tableView.insertRowsAtIndexPaths(indxesPath, withRowAnimation: UITableViewRowAnimation.Bottom) 
        self.tableView.endUpdates() 
        self.tableView.finishInfiniteScroll() 
        self.refreshControl.endRefreshing() 
       } 

      } 

     } else { 
      self.tableView.finishInfiniteScroll() 
      self.refreshControl.endRefreshing() 

     } 
    } 

} 



} 

回答

1

query_like.getFirstObjectInBackgroundWithBlock是asynchrone所以當你添加的崗位,以崗位陣列self.posts.append(post)post.DoILike會得到它的默認值。因此,請確保將帖子追加到已獲得的帖子中,如果用戶喜歡該帖子。

+0

是的,我知道,但我怎麼能做到這一點? –

+0

將所有代碼(在tableview等中插入行)放在回調塊內 – Coyote