And*_*rea 0 iphone core-data nsfetchrequest ios
我在表视图中使用了一个获取的结果控制器,一切正常,除了请求还获取我的实体类型的子类这一事实,我不会.有没有办法做到这一点?控制器以通常的方式实例化.
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
[NSFetchedResultsController deleteCacheWithName:@"Events"];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Events"];
aFetchedResultsController.delegate = (id)self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
Run Code Online (Sandbox Code Playgroud)
如果有人需要:
[fetchRequest setIncludesSubentities:NO];
Run Code Online (Sandbox Code Playgroud)
如果设置此属性,则只获得set实体类型.
| 归档时间: |
|
| 查看次数: |
410 次 |
| 最近记录: |