我在使用同一个viewcontroller进行多次查询时使用parse,我知道他们需要异步,这就是问题,但我不知道如何解决这个问题。我在这个页面上有3个按钮,一个用于显示用户的按钮,一个用于显示他的关注者,以及一个他正在关注的按钮。代码如下:
// Query for user's pods
let podQuery = Pod.query()
podQuery?.whereKey("createdBy", equalTo: currentUser as Any)
podQuery?.includeKey("audio")
podQuery?.findObjectsInBackground(block: { (objects, error) in
if error != nil {
print("Error")
} else if let pods = objects {
self.pods.removeAll()
for pod in pods {
if let pod = pod as? Pod {
self.pods.insert(pod, at: 0)
}
}
self.tableview.reloadData()
}
})
//Query for the user's subscribers
let subscribersQuery = Following.query()
subscribersQuery?.whereKey("following", equalTo: PFUser.current()?.objectId as Any)
subscribersQuery?.includeKey("following")
subscribersQuery?.findObjectsInBackground(block: {(objects, error) in
if let objects = objects {
for object in objects {
self.subscribers.insert(object as! PFUser, at: 0)
}
}
})
//Query for the users that the user is subscribed to
let subscribedQuery = Following.query()
subscribedQuery?.whereKey("follower", equalTo: PFUser.current()?.objectId as Any)
subscribersQuery?.includeKey("follower")
subscribedQuery?.findObjectsInBackground(block: { (objects, error) in
if let objects = objects {
for object in objects {
self.subscribed.insert(object as! PFUser, at: 0)
}
}
})
我在第2次和第3次查询时收到错误。
***由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'此查询具有出色的网络连接。你必须等到它完成。'
如果问题需要更多上下文/代码,请告诉我。谢谢
答案 0 :(得分:1)
我认为按照描述,你可能意味着:
let subscribersQuery = Followers.query() //Not Following.query()??
在这种情况下,进行此更改将使错误消失。如果您的意思是“关注”,那么这可能就是问题所在:
我遇到了这个问题,原因是我在连续的解析访问中使用了相同的查询对象。是否适合您,取决于PFObject.query()的行为方式。我没有发现文档足够清晰。如果它返回先前兑现的查询,则:
let subscribersQuery = Following.query()
//....
subscribersQuery?.findObjectsInBackground(block: {(objects, error) in{(
//..
let subscribedQuery = Following.query()
//....
subscribedQuery?.findObjectsInBackground(block: {(objects, error) in{(
//..
在这里,您将在同一查询上执行findObjectsInBackground,这将导致错误。 在显式创建新查询的情况下尝试此操作。这对我有用:
subscribersQuery = PFQuery(className: Following) //Again, did you mean Followers here?
subscribedQuery = PFQuery(className: Following)