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

标签 ios objective-c iphone core-data

我正在尝试获取两个实体,例如: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 . It looks smth like this

我得到了预期的两个部分,但学生在不同的部分,我已经打印了来自 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/

相关文章:

ios - 带有复选标记的自定义 UITableView 编辑样式 - iOS SDK

objective-c - ld : library not found for -lIOKit. clang xcode

ios - 测量蜂窝信号强度

ios - 苹果在Facebook登录时拒绝应用程序

ios - UINavigationController忘记了自定义过渡动画后如何旋转子 Controller

objective-c - 在 block 参数内向下转换是一种不好的做法吗?

objective-c - 如何从 NSMutableArray 中删除所有对象?

iphone - 将 MySQL 结果加载到 UITableView (iPhone)

iphone - iPad 键盘不会从具有 Model View Controller 样式 UIModalPresentationFormSheet 的 View Controller 调用的 Facebook 对话框中关闭

ios - RestKit RKObjectMapping.. 嵌套和一次性对象