SelectorViewController というビュー コントローラーで「Practice」という Core Data エンティティの「selectedPractice」というオブジェクトを選択し、RegularViewController という別のコントローラーで使用するために結果を保持しようとしています。コア データを追加しようとしている既存のアプリは、Tab または NavigationControllers を使用しないため、どちらも UIViewControllers です。
次のコードを使用して、2 つのコントローラーの 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");
}
これは、すべての NSLogs が表示された状態で正常に実行されます。次に、ここで SelectorViewController.m に setupFetchedResultsController の別のインスタンス化がありますが、次のログでクラッシュが発生します。FetchedResultsController が再開しているという問題ですか?
2012-07-12 20:49: 07.652 WhiteHealth [6969:15b03] SelectorViewController 並べ替えエンティティの完了NSFetchedResultsController のインスタンスには、nil 以外の 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];
}