内部の最初の呼び出しviewWillAppear
は機能しません。ブロックコールバックの2番目の呼び出しは機能します。何も害はありませんが、なぜだろうか?
私の理解では、performFetch
後でNSFRCに変更が加えられない限り、呼び出しは1回限りのことである必要があります。の読み込みが遅延frc
しているため、変更される可能性はありません。performFetch
およびを呼び出した後reloadData
、NSFRCはコンテキストの監視を開始します。また、コンテキストの変更はすべてテーブルビューに自動的に入力されます。
しかし、私のコードではreloadData
、2番目の後に削除performFetch
しても、テーブルビューには何も起こりません。performFetch
それに基づいて、最初のものは完全には効果がないと思います。
明らかな何かが欠けていますか?
-(NSFetchedResultsController *)frc
{
if (!_frc) {
[_frc setDelegate:self];
NSFetchRequest *freq = [[NSFetchRequest alloc] initWithEntityName:@"Item"];
NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
[freq setSortDescriptors:@[sd]];
_frc = [[NSFetchedResultsController alloc] initWithFetchRequest:freq managedObjectContext:[[BNRStore sharedStore]context] sectionNameKeyPath:nil cacheName:nil];
}
return _frc;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// first time
NSError *storeError = nil;
if (![self.frc performFetch:&storeError]) {
NSLog(@"%@",[storeError localizedDescription]);}
[self.tableView reloadData];
if ( [[self.frc fetchedObjects] count] == 0) {
[[BNRStore sharedStore] fetch:^(NSError *error){
if (error) {
UIAlertView *av = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[av show];
} else {
// second time
NSError *storeError = nil;
if (![self.frc performFetch:&storeError]) {
NSLog(@"%@",[storeError localizedDescription]);}
[self.tableView reloadData];
}
}];
}
}