Ios FetchedResultController中没有节
我插入并保存托管对象:Ios FetchedResultController中没有节,ios,core-data,nsmanagedobject,nsfetchedresultscontroller,Ios,Core Data,Nsmanagedobject,Nsfetchedresultscontroller,我插入并保存托管对象: MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegate managedObjectContext]; MyCity* city = (MyCity*)[NSEntityDescription insertNewObjectForEntity
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
MyCity* city = (MyCity*)[NSEntityDescription insertNewObjectForEntityForName:@"City" inManagedObjectContext:context];
city.cityId = dict[@"id"];
city.cityName = dict[@"name"];
NSError *error;
if (![context save:&error]) {
NSLog(@"error: %@", [error localizedDescription]);
}
我执行FRC请求:
_fetchedResultsController = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
这是我的财务报告准则:
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"City"
inManagedObjectContext: context];
NSSortDescriptor *sd1 = [[NSSortDescriptor alloc] initWithKey:@"cityName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sd1, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:10];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath: @"cityName"
cacheName: @"cache"];
[self setFetchedResultsController: theFetchedResultsController];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
在performFetch
之后,我看到:
_fetchedResultsController.fetchedObjects //array of MyCity objects
_fetchedResultsController.sections //empty array
为什么fetchedResultsController.sections为空
当我不使用[context save:&error]
时,我可以在_fetchedResultsController.sections中看到对象数组
在我的代码中保存上下文是否有问题?如何保存nsmanagedobject和nsmanagedcontext?可能
dict
为空,因此cityName
为空,并且没有节标题
此外,您应该持有对托管对象上下文的引用,而不是反复转到应用程序委托获取它
我还建议您在不使用fetchBatchSize
和cache:nil
的情况下测试代码,并在以后进行优化
还要注意的是,通常的模式是在返回FRC之前懒洋洋地创建FRC时进行获取。然后,您可以nil
it out来生成获取,或者对现有实例调用performFetch