これが私の状況です:
アプリの初期ビューでCoreDataからテーブルを読み込むアプリがあります。それをView1TVCと呼びましょう。AppDelegate.mが設定されているため、View1TVCが初期ビューになります。
View2TVCで、同じテーブル(View1TVCでフィルター処理されたもの)をロードしようとしていますが、アプリがクラッシュして次のエラーが発生します。
キャッチされなかった例外'NSInvalidArgumentException'が原因でアプリを終了しています、理由:' NSFetchedResultsControllerのインスタンスには、nil以外のfetchRequestとmanagedObjectContextが必要です
View1TVCとView2TVCの両方のテーブルのロードに関連するコードは同じです!! View1TVCでは機能しますが、View2TVCでは機能しません。View2TVCコードは次のとおりです。
-(void)setupFetchedResultsController
{
NSString *entityName = @"Task";
NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);
// REQUEST:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// SORT:
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor
sortDescriptorWithKey:@"name"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)]];
// FETCH:
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self setupFetchedResultsController];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
Task *task = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = task.name;
return cell;
}
アプリは、View2TVCにセグエし、setupFetchedResultsControllerメソッドのSORT行に到達すると、特にハングします。
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]];
誰か考えがありますか?エラーは、AppDelegate.mでView1TVCをデフォルトのアプリとして識別することに関係していますか?AppDelegate.mのコードは次のとおりです。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
View1TVC *controller = (View1TVC *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;
}
誰もが貸してくれる洞察に感謝します!ありがとう。