gpt4 book ai didi

ios - 需要帮助设置后台托管对象上下文。在 FRC controllerDidChangeContent 上获取异常

转载 作者:行者123 更新时间:2023-11-29 03:12:20 24 4
gpt4 key购买 nike

我有一个包含两个托管对象上下文的应用程序:

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, strong) NSManagedObjectContext *backgroundContext;

我的 View Controller 之一是响应 NSFetchedResultsControllerDelegateUITableView Controller 。当我(从远程 Web 服务)获取对象时,我想在后台线程上处理数据,将更改合并到我的主 managedObjectContext 并更新 UI。

在我当前的设置中,当我尝试获取新对象时出现以下异常。

CoreData: error: Serious application error.  An exception was caught from the delegate of   NSFetchedResultsController during a call to -controllerDidChangeContent:.  *** -[__NSArrayM  insertObject:atIndex:]: object cannot be nil with userInfo (null)

此异常引发于:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {

[self.tableView endUpdates];

}

我的托管对象上下文是在名为 AppController 的单例类中创建的。目前看起来像这样:

- (id)init {    
self = [super init];
if (self) {
sharedInstance = self;

// Registers NSManagedObjectContextDidSaveNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:self.managedObjectContext];

}

return self;
}

- (void)mergeChanges:(NSNotification *)notification {

//should tell the main thread moc to run on the main thread, and merge in the changes there
[self.managedObjectContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:notification waitUntilDone:YES];

}


- (void)saveContext {
NSError *childError = nil;
[self.backgroundContext save:&childError];

[self.managedObjectContext performBlock:^{
NSError *parentError = nil;
[self.managedObjectContext save:&parentError];
}];
}


// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
self.backgroundContext = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSPrivateQueueConcurrencyType];
self.backgroundContext.parentContext = self.managedObjectContext;
}
return _managedObjectContext;
}

关于我遗漏或做错了什么的任何想法?

最佳答案

我认为您必须在后台进程中创建 backgroundContext。

并且您需要监听backgroundContext发送的通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:self.backgroundContext];

在我的例子中,runupdate 在后台进程中运行:

- (void) runUpdate {
NSManagedObjectContext *managedContext = [[NSManagedObjectContext alloc] init];
managedContext.persistentStoreCoordinator = ...;
[updateController registerBackgroundMoc:managedContext];
//do all update stuff
NSError *error;
if (![managedContext save: &error] {
...
}
[updateController unregisterBackgroundMoc:managedContext];
}

在我的 updateController 中我做了以下事情:

- (void) registerBackgroundMoc: (NSManagedObjectContext *) updateMoc {
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(mergeChanges:) name: NSManagedObjectContextDidSaveNotification object: updateMoc];
}

- (void) unregisterBackgroundMoc: (NSManagedObjectContext *) updateMoc {
[[NSNotificationCenter defaultCenter] removeObserver: self name: NSManagedObjectContextDidSaveNotification object: updateMoc];
}

关于ios - 需要帮助设置后台托管对象上下文。在 FRC controllerDidChangeContent 上获取异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22129244/

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