我正在尝试在名为SelectorViewController的视图控制器上选择名为“Practice”的Core Data实体的名为“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];
}
答案 0 :(得分:3)
SelectorViewController *svc;
svc.managedObjectContext = self.managedObjectContext;
svc.fetchedResultsController = self.fetchedResultsController;
RegularViewController *rvc;
rvc.managedObjectContext = self.managedObjectContext;
rvc.fetchedResultsController = self.fetchedResultsController;
return YES;
不会为你设置managedObjectContexts,你没有初始化svc和rvc,你所做的只是制作对象。如果要设置managedObject上下文,请进入viewDidLoad,或者在运行获取请求之前,执行以下操作:
if (self.managedObjectContext == nil) {
self.managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}