2017-07-11 35 views
0

我試圖在mongoDB上的「用戶」和「註釋」之間創建一個關節,以便對我的應用程序進行評論。mongoDB和Swift中的解析

這是我的代碼:

let query = PFQuery(className: "comments") 
    query.whereKey("to", equalTo: commentuuid.last!) 
    query.skip = count - self.page 
    query.addAscendingOrder("createdAt") 
    query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in 

     if error == nil { 
      // Clean up 
      self.usernameArray.removeAll(keepingCapacity: false) 
      self.avaArray.removeAll(keepingCapacity: false) 
      self.commentArray.removeAll(keepingCapacity: false) 
      self.dateArray.removeAll(keepingCapacity: false) 

      // find related objects 
      for object in objects! { 

       let infoQuery = PFQuery(className: "_User") 
       infoQuery.getObjectInBackground(withId: object.object(forKey: "id") as! String, block: { (test: PFObject?, error: Error?) in 
       if error == nil { 
        print("YES") 
        self.usernameArray.append(test!.object(forKey: "username") as! String) 

       } else { 
        print(error!.localizedDescription) 
       } 
       }) 

       self.commentArray.append(object.object(forKey: "comment") as! String) 
       self.dateArray.append(object.createdAt) 
       self.tableView.reloadData() 

       // Scroll to bottom 
       self.tableView.scrollToRow(at: IndexPath(row: self.commentArray.count - 1, section: 0), at: .bottom, animated: true) 
      } 

     } else { 
      print(error!.localizedDescription) 
     } 


    }) 

這些線路很好的執行:

self.commentArray.append(object.object(forKey: "comment") as! String) 
       self.dateArray.append(object.createdAt) 

但是一個都沒有:

let infoQuery = PFQuery(className: "_User") 
       infoQuery.getObjectInBackground(withId: object.object(forKey: "id") as! String, block: { (test: PFObject?, error: Error?) in 
       if error == nil { 
        print("YES") 
        self.usernameArray.append(test!.object(forKey: "username") as! String) 

       } else { 
        print(error!.localizedDescription) 
       } 
       }) 

我試圖打印usernameArray和dateArray (只是爲了看到差異)在cellForRowAt函數。

當我啓動視圖時,usernameArray爲空,dateArray爲Not。如果我滾動一下,usernameArray被填充(但我需要滾動來填充數組,這是不可接受的)。

回答

0

可能是因爲你是打電話來UI相關的方法中的數據被檢索之前:

  self.commentArray.append(object.object(forKey: "comment") as! String) 
      self.dateArray.append(object.createdAt) 

      let infoQuery = PFUser.query()! 
      infoQuery.getObjectInBackground(withId: object.object(forKey: "id") as! String, block: { (test: PFObject?, error: Error?) in 
      if error == nil { 
       print("YES") 
       self.usernameArray.append(test!.object(forKey: "username") as! String) 

      } else { 
       print(error!.localizedDescription) 
      } 

       // These two methods were the issue. 
       self.tableView.reloadData() 
       // Scroll to bottom 
       self.tableView.scrollToRow(at: IndexPath(row: self.commentArray.count - 1, section: 0), at: .bottom, animated: true) 

      }) 



你應該考慮:

  • 使用子類PFObject避免訪問原始字典可能導致錯誤的錯誤。
  • 有一個更好的更換關鍵字object(根據你的情況下,comment