与Swong中的mongoDB和Parse联合

问题描述 投票:0回答:1

我正在尝试在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为Not。如果我滚动了一点,就填充了usernameArray(但是我需要滚动来填充数组,这是不可接受的)。

swift mongodb uitableview parse-platform swift3
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.