gpt4 book ai didi

ios - CoreData 异步获取导致并发调试器错误

转载 作者:技术小花猫 更新时间:2023-10-29 10:36:51 61 4
gpt4 key购买 nike

我正在使用

-com.apple.CoreData.ConcurrencyDebug

启动时在我的 CoreData 应用程序中调试并发性的参数。

在应用启动期间,我在主线程的托管对象上下文中执行异步获取。

// set up the async request
NSError * error = nil;
[MOC executeRequest:asyncFetch error:&error];
if (error) {
NSLog(@"Unable to execute fetch request.");
NSLog(@"%@, %@", error, error.localizedDescription);
}

此代码是从主线程调用的,但是 executeRequest: 将它排入另一个线程,据我所知这是正确的行为。

并发调试器不喜欢这样,说(我估计)我在这里做错了什么。我还尝试将它包装在 [MOC performBlock:] 中,这也有效,但也会导致多线程违规。在这两种情况下,我都得到了这个:

[NSManagedObjectContext __Multithreading_Violation_AllThatIsLeftToUsIsHonor__

我是不是错误地使用了异步提取,还是并发调试器在这里出错了?

编辑:我也试过将它包装在 MOC performBlock 中,这应该确保它从主线程被调用。在任何情况下,调用都是从主线程排队,但在别处执行。

It says the fetch has been enqueued from the main thread, but it seems to actually happen on another.

编辑:这是获取请求:

 NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];

NSPredicate * pred = [NSPredicate predicateWithFormat:@"boolProperty == YES"];
fetchRequest.predicate = pred;
NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
fetchRequest.sortDescriptors = @[sort]; fetchRequest.propertiesToFetch = @[@"prop1", @"prop2", @"prop3", @"prop4"];

NSPersistentStoreAsynchronousFetchResultCompletionBlock resultBlock = ^(NSAsynchronousFetchResult *result) {
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:kFetchCompleteNotification object:result];
});
};

NSAsynchronousFetchRequest *asyncFetch = [[NSAsynchronousFetchRequest alloc]
initWithFetchRequest:fetchRequest
completionBlock:resultBlock];

然后我收到通知的结果:

- (void)fetchCompletedNote:(NSNotification *)note {
NSAsynchronousFetchResult * result = note.object;
if (![cachedResults isEqualToArray:result.finalResult]){
cacheResults = result.finalResult;
[self.collectionView reloadData];
}
}

最佳答案

我认为这是 Apple 的错误。

我提交了错误报告: https://openradar.appspot.com/30692722

并添加了重现问题的示例项目: https://github.com/jcavar/examples/tree/master/TestAsyncFetchCoreData

此外,如果您不想仅仅因为这个问题而禁用标志,您可能需要为这部分代码调整 NSManagedObjectContext 上的 __Multithreading_Violation_AllThatIsLeftToUsIsHonor__ 方法。您需要在执行请求后恢复它,以便您在实际问题上得到违规。

关于ios - CoreData 异步获取导致并发调试器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31728425/

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