您好,这是我的 NSFetchedResultsController 实现:
-(NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSLog(@"context - %@",self.context);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Categories" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:@[sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.context sectionNameKeyPath:nil
cacheName:@"root"];
_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
// [[self context] fetchObjectsForEntityName:@"Categories" withPredicate:
// nil] ;
return _fetchedResultsController;
}
fetchedResultsController.fetchedObjects 始终为零。这是我添加对象的方式:
-(void)addCategoryWithName:(NSString *)name
{
NSLog(@"context - %@",self.context);
Categories *category = (Categories *)[NSEntityDescription insertNewObjectForEntityForName:@"Categories" inManagedObjectContext:self.context];
category.name = name;
NSLog(@"category name - %@",category.name);
category.displayOrder = [NSNumber numberWithFloat:1.0f];
// site.displayOrder = displayOrder;
NSError __block *error;
if (![self.context save:&error]){
NSLog(@"Error saving - %@", [error localizedDescription]);
}
}
没有错误,什么都没有。如果我在 Documents 目录中看到我的 .sqlite 文件,它已被修改,但我无法获取任何对象。真的很奇怪,我对我的其他应用程序使用基本相同的代码,它工作得很好。
有任何想法吗?