与Swong中的mongoDB和Parse联合

时间:2017-07-11 16:00:08

标签: swift mongodb uitableview parse-platform swift3

我正在尝试在mongoDB上创建“用户”和“评论”之间的联合,以便对我的应用发表评论。

这是我的代码:

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)
                 }
              })

我试图在cellForRowAt函数中打印usernameArray和dateArray(只是为了看看区别)。

当我启动视图时,usernameArray为空,dateArray为Not。如果我滚动了一点,就填充了usernameArray(但是我需要滚动来填充数组,这是不可接受的)。

1 个答案:

答案 0 :(得分: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