简体   繁体   English

核心数据不返回完整的对象,数据:<fault>

[英]Core data don't return full objects, data: <fault>

I'm trying to fetch two entities eg: Student with email attribute and Professor, both of have same parent entity eg: Person with attributes entityId, firstName and lastName i want to generate them in two sections using NSFetchedResultsController .我想取两个实体如:与学生email attribute和教授,两者具有相同的父实体如:人与attributes entityId, firstName and lastName我想生成他们使用两个部分NSFetchedResultsController Here is a part from getter for fetchedResultsController这是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];

All Students have the same entityId and all Professors too所有Students都有相同的entityId和所有Professors

In tableView I have two prototype cells one for Student and another for Professor .在 tableView 中,我有两个原型单元格,一个用于Student ,另一个用于Professor 它看起来像这样

I get two sections as expected but students are in different sections, i have printed all objects from fetchedResultsController in console like this, po [self.fetchedResultsController objectAtIndexPath: [NSIndexPath indexPathForItem:1 inSection:1]] all professors are printed with fault :我按预期得到两个部分,但学生在不同的部分,我已经在控制台中打印了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>)

I have forced the fetch request to return full objects using [request setReturnsObjectsAsFaults:NO] but it had no effect.我已经强制获取请求使用[request setReturnsObjectsAsFaults:NO]返回完整对象,但它没有效果。

Why is it happening so?为什么会这样?

To avoid "data fault" issue you should set this field of NSFetchRequest:为了避免“数据错误”问题,你应该设置 NSFetchRequest 的这个字段:

request.returnsObjectsAsFaults = NO;

To separate students from professors in two sections you can use multiple NSFetchedResultsControllers, as described here: https://stackoverflow.com/a/2309855/1689376要将学生与教授分为两个部分,您可以使用多个 NSFetchedResultsControllers,如下所述: https : //stackoverflow.com/a/2309855/1689376

To avoid duplication of your code, just create a method like this and call it twice:为避免重复您的代码,只需创建一个这样的方法并调用它两次:

- (NSFetchedResultsController) createFetchedResultsController: (NSString *)  entityName {
 //move your code here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM