Swift 使用异步显示工具包/cloudkit进行批获取
我正在尝试实现批量抓取,以与cloudkit和异步显示工具包集成。但是,当我尝试滚动时,会出现以下错误: [NSOperationQueue addOperation::操作已完成,无法排队' 你知道这会发生吗?谢谢 以下是我的网络电话:Swift 使用异步显示工具包/cloudkit进行批获取,swift,uiscrollview,cloudkit,asyncdisplaykit,batch-fetching,Swift,Uiscrollview,Cloudkit,Asyncdisplaykit,Batch Fetching,我正在尝试实现批量抓取,以与cloudkit和异步显示工具包集成。但是,当我尝试滚动时,会出现以下错误: [NSOperationQueue addOperation::操作已完成,无法排队' 你知道这会发生吗?谢谢 以下是我的网络电话: var thisCursor : CKQueryCursor? var newQueryOP : CKQueryOperation? func pullPosts(curLocation: CLLocation, completionHandler: @es
var thisCursor : CKQueryCursor?
var newQueryOP : CKQueryOperation?
func pullPosts(curLocation: CLLocation, completionHandler: @escaping(([PostMap]) -> Bool)) {
print("------------PULLING POSTS (1ST PULL) ----------------")
let location = curLocation
var annotations : [PostMap] = [PostMap]()
//let curLocation = mapLocationManager.getCurrentLocation()
let locationPredicate = NSPredicate(format: "distanceToLocation:fromLocation:(%K, %@) < %f", "Location", location,
CKConstants.loadPinsRadius)
let query = CKQuery(recordType: "PostMap", predicate: locationPredicate)
query.sortDescriptors = [CKLocationSortDescriptor(key: "Location", relativeLocation: location)]
let queryOP = CKQueryOperation(query: query)
print("SERVER DECIDED QUERY LIMIT \(queryOP.resultsLimit)")
queryOP.recordFetchedBlock = { record in
//MAKE A NEW POST (taken out for brevity)
annotations.append(postToAdd)
}
queryOP.queryCompletionBlock = { [unowned self] (cursor, error) in
DispatchQueue.main.async {
if error == nil {
if completionHandler(annotations) {
if cursor != nil {
print("THIS CURSOR IS NOT NIL")
self.thisCursor = cursor
self.newQueryOP = CKQueryOperation(cursor: cursor!)
}
}
} else {
print(error)
print("could not pull posts")
}
}
}
queryOP.resultsLimit = 15
CKContainer.default().publicCloudDatabase.add(queryOP)
}
public func continuePullPosts(curLocation: CLLocation, queryOP: CKQueryOperation,
annotations: [PostMap], completionHandler: @escaping(([PostMap]) -> Bool)) {
var annotations = annotations
print("------------CONTINUE PULLING POSTS ----------------")
queryOP.recordFetchedBlock = { record in
//MAKE A NEW POST (taken out for brevity)
annotations.append(postToAdd)
}
queryOP.queryCompletionBlock = { [unowned self] (cursor, error) in
DispatchQueue.main.async {
if error == nil {
if completionHandler(annotations) {
if cursor != nil {
print("paging posts")
self.thisCursor = cursor!
self.newQueryOP = CKQueryOperation(cursor: cursor!)
}
}
} else {
print(error)
print("could not pull posts")
}
}
}
queryOP.resultsLimit = CKConstants.subsequentPullAmount
CKContainer.default().publicCloudDatabase.add(queryOP)
}
您正在将已完成的操作传递到
continuePullPosts
函数中,然后尝试将其重新添加到数据库中
相反,您需要保存第一次操作返回的游标,将该游标传递给continuePullPosts,然后使用initWithCursor
创建一个新的后续操作
每次收到游标时,您都需要继续使用
initWithCursor
创建新操作,直到查询最终完成并返回一个nil游标。这就成功了!我的下一个问题是,当我在提要中滚动(由纹理驱动)时,批量抓取似乎会重新定位我在提要上的位置,因为它会填充提要更多。有没有办法防止这种情况发生,使滚动过程变得平滑?这与我在什么时候开始批量提取有关吗?谢谢我不熟悉纹理。如果您创建第二个问题,在标题中指定症状,您可能会让更多人看到后续问题(因此更有可能找到熟悉该库的人)。
func shouldBatchFetch(for tableNode: ASTableNode) -> Bool {
if (pinDataManager.thisCursor == nil) {
return false
}; return true
}
func tableNode(_ tableNode: ASTableNode, willBeginBatchFetchWith context: ASBatchContext) {
var annotations : [PostMap] = [PostMap]()
pinDataManager.continuePullPosts(curLocation: locationManager.getCurrentLocation(), queryOP: pinDataManager.newQueryOP!, annotations: annotations) { (annotations) -> Bool in
self.insertNewRowsInTableNode(annotations: annotations)
context.completeBatchFetching(true)
return true
}
}
func insertNewRowsInTableNode(annotations: [MapObject]) {
let section : NSInteger = 0
let indexPaths = NSMutableArray()
let totalPosts = self.postObjects.count + annotations.count
print(self.postObjects.count)
print(annotations.count)
for index in self.postObjects.count...totalPosts - 1 {
let path = NSIndexPath(row: index, section: section)
indexPaths.add(path)
}
self.postObjects = self.postObjects + annotations
self.tableNode.insertRows(at: indexPaths as! [IndexPath], with: .none)
}