我正在尝试获取两个实体,例如:Student with email
attribute
和教授,两者都有相同的父实体,例如:人 attributes
entityId, firstName and lastName
我想使用 NSFetchedResultsController
分两部分生成它们.
这是 fetchedResultsController
setter/getter 的一部分 |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setFetchBatchSize:20];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSEntityDescription *description = [NSEntityDescription entityForName:@"Person"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:description];
NSSortDescriptor *firstNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES];
NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
[fetchRequest setSortDescriptors:@[firstNameDescriptor, lastNameDescriptor]];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@"entityId"
cacheName:nil];
全部Students
有相同的entityId
和所有 Professors
也是
在 tableView 中,我有两个原型(prototype)单元格,一个用于 Student
另一个是 Professor
.
我得到了预期的两个部分,但学生在不同的部分,我已经打印了来自 fetchedResultsController
的所有对象在这样的控制台中,po [self.fetchedResultsController objectAtIndexPath: [NSIndexPath indexPathForItem:1 inSection:1]]
所有教授都印有fault
:
<Professor: 0x6080000befc0> (entity: Professor; id: 0xd0000000001c0002 <x-coredata://03A3ECAD-CCA7-424E-86F9-258D25372BA1/Professor/p7> ; data: <fault>)
我已使用 [request setReturnsObjectsAsFaults:NO]
强制获取请求返回完整对象但是没有效果。
为什么会这样?
最佳答案
为避免“数据错误”问题,您应该设置 NSFetchRequest 的这个字段:
request.returnsObjectsAsFaults = NO;
要将学生和教授分成两部分,您可以使用多个 NSFetchedResultsControllers,如下所述: https://stackoverflow.com/a/2309855/1689376
为避免代码重复,只需创建一个这样的方法并调用它两次:
- (NSFetchedResultsController) createFetchedResultsController: (NSString *) entityName {
//move your code here
}
关于ios - 核心数据不返回完整对象,数据 : <fault>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39922268/