NSFetchRequest 还获取我的实体类型的子类
Posted
技术标签:
【中文标题】NSFetchRequest 还获取我的实体类型的子类【英文标题】:NSFetchRequest fetches also subclasses of my entity type 【发布时间】:2013-11-21 10:12:07 【问题描述】:我在表格视图中使用获取的结果控制器,一切正常,除了请求还获取我的实体类型的子类,而我不会。有没有办法做到这一点?控制器以通常的方式实例化。
- (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;
【问题讨论】:
【参考方案1】:如果有人需要:
[fetchRequest setIncludesSubentities:NO];
如果你设置了这个属性,你只会得到设置的实体类型。
【讨论】:
以上是关于NSFetchRequest 还获取我的实体类型的子类的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 NSExpression 使用自定义函数设置 NSFetchRequest propertiesToFetch
核心数据 NSFetchRequest 按类别排序方法返回值
NSFetchRequest 具有一对一关系属性 == YES 谓词错误地获取每个实体
在 NSArray 中获取 NSFetchRequest 的所有结果