我正在尝试在名为 SelectorViewController 的视图控制器上选择名为“Practice”的核心数据实体的名为“selectedPractice”的对象,然后保留结果以在另一个名为 RegularViewController 的控制器中使用。两者都是 UIViewControllers,因为我试图将核心数据附加到的现有应用程序不使用 Tab 或 NavigationControllers。
我尝试使用以下代码将两个控制器的 ManagedObjectContexts 设置为与 AppDelegate.m 中的 AppDelegate 相同:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
SelectorViewController *svc;
svc.managedObjectContext = self.managedObjectContext;
svc.fetchedResultsController = self.fetchedResultsController;
RegularViewController *rvc;
rvc.managedObjectContext = self.managedObjectContext;
rvc.fetchedResultsController = self.fetchedResultsController;
return YES;
}
AppDelegate.m 这里也有一个 setupFetchedResultsController 方法(改编自 Tim Roadley 的优秀作品):
- (void)setupFetchedResultsController
{
// 1 - Decide what Entity you want
NSString *entityName = @"Practice"; // Put your entity name here
NSLog(@"AppDelegate Setting up a Fetched Results Controller for the Entity named %@", entityName);
// 2 - Request that Entity
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// 3 - Filter it if you want
//request.predicate = [NSPredicate predicateWithFormat:@"Practice.name = Blah"];
// 4 - Sort it if you want
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
// 5 - Fetch it
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self.fetchedResultsController performFetch:nil];
NSLog(@"AppDelegate setupFetchedResultsController completed");
}
这在显示所有 NSLog 时运行良好。然后在 SelectorViewController.m 中有另一个 setupFetchedResultsController 实例,但它会导致崩溃并显示以下日志。是 FetchedResultsController 重新启动的问题吗?
2012-07-12 20:49:07.652 WhiteHealth[6969:15b03] SelectorViewController 排序实体完成 2012-07-12 20:49:07.653 WhiteHealth[6969:15b03] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“ NSFetchedResultsController 的一个实例需要一个非零的 fetchRequest 和 managedObjectContext'
- (void)performFetch
{
if (self.fetchedResultsController) {
if (self.fetchedResultsController.fetchRequest.predicate) {
if (self.debug) NSLog(@"[%@ %@] fetching %@ with predicate: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName, self.fetchedResultsController.fetchRequest.predicate);
} else {
if (self.debug) NSLog(@"[%@ %@] fetching all %@ (i.e., no predicate)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), self.fetchedResultsController.fetchRequest.entityName);
}
NSError *error;
[self.fetchedResultsController performFetch:&error];
if (error) NSLog(@"[%@ %@] %@ (%@)", NSStringFromClass([self class]), NSStringFromSelector(_cmd), [error localizedDescription], [error localizedFailureReason]);
} else {
if (self.debug) NSLog(@"[%@ %@] no NSFetchedResultsController (yet?)", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
}
[pickerView reloadAllComponents];
}
- (void)setupFetchedResultsController
{
// 1 - Decide what Entity you want
NSString *entityName = @"Practice"; // Put your entity name here
NSLog(@"SelectorViewController Setting up a Fetched Results Controller for the Entity named %@", entityName);
// 2 - Request that Entity
[NSFetchedResultsController deleteCacheWithName:nil];
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
NSLog(@"SelectorViewController requesting entity complete");
// 3 - Filter it if you want
//request.predicate = [NSPredicate predicateWithFormat:@"Person.name = Blah"];
// 4 - Sort it if you want
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
NSLog(@"SelectorViewController sorting entity complete");
// 5 - Fetch it
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
}