mongoDB和Swift中的解析

问题描述:

我试图在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被填充(但我需要滚动来填充数组,这是不可接受的)。

可能是因为你是打电话来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