gpt4 book ai didi

objective-c - CKQueryOperation 处理大批量

转载 作者:行者123 更新时间:2023-12-04 02:20:37 32 4
gpt4 key购买 nike

我在使用大批量数据创建 CKQuery 操作时遇到问题。我的查询适用于 100 个结果,但在更多结果查询失败后,因为一个线程调度错误或其他原因(libdispatch.dylib`dispatch_group_leave:) 我迷路了……有什么想法吗?

+ (void) fetchAnswersWithRecordId:(CKRecordID *)recordId completionHandler:(CloudKitCompletionHandler)handler {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANSrecordID == %@", recordId];
CKQuery *query = [[CKQuery alloc] initWithRecordType:ckAnswers predicate:predicate];

CKQueryOperation *operation = [[CKQueryOperation alloc] initWithQuery:query];
CKQueryOperation * __weak weakSelf = operation;
operation.resultsLimit = 300;
NSMutableArray *tmp = [[NSMutableArray alloc] init];


operation.recordFetchedBlock = ^(CKRecord *record) {
[tmp addObject:record];
};

operation.queryCompletionBlock = ^(CKQueryCursor *cursor, NSError *error) {
if (!handler)
return;

NSArray *array = [NSArray arrayWithArray:tmp];
if(cursor != nil) {
CKQueryOperation *newOperation = [[CKQueryOperation alloc] initWithCursor:cursor];
newOperation.recordFetchedBlock = weakSelf.recordFetchedBlock;
newOperation.completionBlock = weakSelf.completionBlock;
[[self publicCloudDatabase] addOperation:newOperation];
} else {
NSLog(@"Results: %lu", [array count]);
dispatch_async(dispatch_get_main_queue(), ^{
handler(array, error);
});
}
};

[[self publicCloudDatabase] addOperation:operation];}

最佳答案

我认为您的问题在于 __weak 操作以及您在另一个操作中创建操作的方式。这是一个示例(快速),说明我如何做类似的事情,即获得额外的结果,但在获取而不是查询中。注意第一次使用实例变量初始化,通过GCD dispatch_aync使用半递归:

private func _fetchRecordChangesFromCloud() {
if !_fetching {
// this is the first and only time this code is called in GCD recusion

// we clean the caches we use to collect the results of the fetch
// so we can then save the record in the correct order so references can be created
_fetchedModifiedRecords = []
_fetchedDeletedRecordIDs = []

// mark fetching has started
_fetching = true
}

let operation = CKFetchRecordChangesOperation(recordZoneID: _customRecordZoneID, previousServerChangeToken: _serverChangeToken)

operation.recordChangedBlock = { (record: CKRecord?) in
if let record = record {
println("Received record to save: \(record)")
self._fetchedModifiedRecords.append(record)
}
}

operation.recordWithIDWasDeletedBlock = { (recordID: CKRecordID?) in
if let recordID = recordID {
println("Received recordID to delete: \(recordID)")
self._fetchedDeletedRecordIDs.append(recordID)
}
}

operation.fetchRecordChangesCompletionBlock = {
(serverChangeToken: CKServerChangeToken?, clientChangeToken: NSData?, error: NSError?) -> Void in

if let error = error {
println("Error in fetching record changes: \(error)")
// try again next sync
self._fetchAfterNextSuccessfullSync = true
self._fetching = false
return
}

// fetched records successfuly
println("fetched records successfuly")

if let serverChangeToken = serverChangeToken {
self._serverChangeToken = serverChangeToken
}

if operation.moreComing {
// we need to create another operation object and do it again
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
self._fetchRecordChangesFromCloud()
}
} else {
// we are finally done

// process the fetched records
self._processFetchedRecords()

// save all changes back to persistent store
self._saveBackgroundContext()

// we are done
self._fetching = false
}
}

self._privateDatabase.addOperation(operation)
}

关于objective-c - CKQueryOperation 处理大批量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30179107/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com