非法访问 Core Data 上下文:AppDelegate 中提供的上下文的所有者是什么线程?
Illegal access to Core Data context: what thread is the owner of the context provided in AppDelegate?
我有一个 class 方法如下:
- (void)getEntities
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = appDelegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
NSArray *entities = [mainContext executeFetchRequest:fetchRequest error:nil];
for (NSManagedObject *item in entities) {
NSLog(@"Name: %@", ((MyEntity *)item).name);
}
}
我启用了 com.apple.CoreData.ConcurrencyDebug 1
,当我调用此方法(在主线程中)时出现错误:
CoreData: error: The current thread is not the recognized owner of this NSManagedObjectContext(0x16d40160). Illegal access during executeFetchRequest:error:
但是,如果我这样做:
- (void)getEntities
{
NSManagedObjectContext *privateContext = [CoreDataStack getPrivateContext];
if (privateContext != nil) {
[privateContext performBlock: ^{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
NSArray *entities = [privateContext executeFetchRequest:fetchRequest error:nil];
for (NSManagedObject *item in entities) {
NSLog(@"Name: %@", ((MyEntity *)item).name);
}
}];
}
}
而且我也在主线程中调用它,它似乎不会导致这样的核心数据错误...为什么?我一直假设 AppDelegate
中默认提供的 NSManagedObjectContext
和我正在检索的内容属于主线程...我做错了什么或遗漏了什么?
我尝试使用来自 AppDelegate
的上下文获取的实体,该上下文之前是从在私有队列中创建的上下文中保存的(类似于我在第二个代码片段中创建的上下文)。这可能是错误的原因吗?这样的私有上下文不是任何其他上下文的子上下文,我保存它时没有错误。
我需要通过在主线程中使用私有上下文来获取我已经存储的实体,以便在 UIViewController
.[=19= 的实现中将它们保存在 NSArray
中]
提前致谢。
似乎问题不在方法中,而是我在 AppDelegate
中对上下文的初始化:在调用问题中的方法之前,在某些情况下,我第一次从中检索上下文不是主线程的队列,似乎导致上下文在另一个队列中初始化。
我有一个 class 方法如下:
- (void)getEntities
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *mainContext = appDelegate.managedObjectContext;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
NSArray *entities = [mainContext executeFetchRequest:fetchRequest error:nil];
for (NSManagedObject *item in entities) {
NSLog(@"Name: %@", ((MyEntity *)item).name);
}
}
我启用了 com.apple.CoreData.ConcurrencyDebug 1
,当我调用此方法(在主线程中)时出现错误:
CoreData: error: The current thread is not the recognized owner of this NSManagedObjectContext(0x16d40160). Illegal access during executeFetchRequest:error:
但是,如果我这样做:
- (void)getEntities
{
NSManagedObjectContext *privateContext = [CoreDataStack getPrivateContext];
if (privateContext != nil) {
[privateContext performBlock: ^{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"MyEntity"];
NSArray *entities = [privateContext executeFetchRequest:fetchRequest error:nil];
for (NSManagedObject *item in entities) {
NSLog(@"Name: %@", ((MyEntity *)item).name);
}
}];
}
}
而且我也在主线程中调用它,它似乎不会导致这样的核心数据错误...为什么?我一直假设 AppDelegate
中默认提供的 NSManagedObjectContext
和我正在检索的内容属于主线程...我做错了什么或遗漏了什么?
我尝试使用来自 AppDelegate
的上下文获取的实体,该上下文之前是从在私有队列中创建的上下文中保存的(类似于我在第二个代码片段中创建的上下文)。这可能是错误的原因吗?这样的私有上下文不是任何其他上下文的子上下文,我保存它时没有错误。
我需要通过在主线程中使用私有上下文来获取我已经存储的实体,以便在 UIViewController
.[=19= 的实现中将它们保存在 NSArray
中]
提前致谢。
似乎问题不在方法中,而是我在 AppDelegate
中对上下文的初始化:在调用问题中的方法之前,在某些情况下,我第一次从中检索上下文不是主线程的队列,似乎导致上下文在另一个队列中初始化。