我在查询PFQueryTableViewController类的表方法时有一个复合查询:
func baseQuery() -> PFQuery {
let ownedChannels = PFQuery(className: "Channel").whereKey("owner", equalTo: PFUser.currentUser()!)
let subscribedPrivateChannels = PFQuery(className: "Channel").whereKey("subscribers", equalTo: PFUser.currentUser()!)
let query = PFQuery.orQueryWithSubqueries([ownedChannels, subscribedPrivateChannels])
query.orderByDescending("createdAt")
return query
}
override func queryForTable() -> PFQuery {
return baseQuery().fromLocalDatastore()
}
然后我使用刷新功能从服务器获取数据:
func refreshLocalDataStoreFromServer() {
baseQuery().findObjectsInBackgroundWithBlock { (parseObjects, error) -> Void in
if error == nil {
print("Found \(parseObjects!.count) parseObjects from server")
// First, unpin all existing objects
PFObject.unpinAllInBackground(self.objects as? [PFObject], block: { (succeeded, error) -> Void in
if error == nil {
// Pin all the new objects
PFObject.pinAllInBackground(parseObjects, block: { (succeeded, error) -> Void in
if error == nil {
// Once we've updated the local datastore, update the view with local datastore
self.shouldUpdateFromServer = false
self.loadObjects()
} else {
print("Failed to pin objects")
}
})
}
})
} else {
print("Couldn't get objects")
}
}
}
然后刷新我使用的本地商店中的数据:
override func objectsDidLoad(error: NSError?) {
super.objectsDidLoad(error)
if self.shouldUpdateFromServer {
self.refreshLocalDataStoreFromServer()
} else {
self.shouldUpdateFromServer = true
}
}
shouldUpdateFromServer
是使用true
这适用于常规查询,在这种情况下,它成功固定ownedChannels
,但不会固定第二个查询中的频道。
在parseObjects
方法中记录refreshLocalDataStoreFromServer
时,会显示所有查询结果。
subscribers
是通道和用户对象之间的解析关系。