[英]Core data don't return full objects, data: <fault>
我想取两个实体如:与学生email
attribute
和教授,两者具有相同的父实体如:人与attributes
entityId, firstName and lastName
我想生成他们使用两个部分NSFetchedResultsController
。 这是fetchedResultsController
getter 的fetchedResultsController
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 中,我有两个原型单元格,一个用于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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.