我正在尝试使用NSFetchedResultsController以及与我在App中使用的主MOC不同的其他NSManagedObjectContext。虽然我能够使用额外的MOC保存和检索数据,但每次尝试创建NSFetchedResultsController时,它都会返回一个带有nil fetchedObjects的对象。
我正在使用MagicalRecord,这是我正在使用的代码:
- (id) init
{
if(SINGLETON){
return SINGLETON;
}
if (isFirstAccess) {
[self doesNotRecognizeSelector:_cmd];
}
self = [super init];
if (self) {
NSManagedObjectModel *model = [NSManagedObjectModel MR_newManagedObjectModelNamed:@"ConnectorCache.momd"];
[NSManagedObjectModel MR_setDefaultManagedObjectModel:model];
storeCoordinator = [NSPersistentStoreCoordinator MR_coordinatorWithAutoMigratingSqliteStoreNamed:@"ConnectorCache.sqlite"];
cacheContext = [NSManagedObjectContext MR_contextWithStoreCoordinator:storeCoordinator];
[MagicalRecord setShouldAutoCreateManagedObjectModel:YES];
}
return self;
}
获取fetchedResultsController的代码是:
- (NSFetchedResultsController *) fetchedResultsController {
if (_fetchedResultController != nil) {
return _fetchedResultController;
}
NSFetchRequest *request = [Appointment MR_requestAllSortedBy:@"start" ascending:YES inContext:cacheContext];
_fetchedResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:cacheContext sectionNameKeyPath:@"day" cacheName:nil];
return _fetchedResultController;
}
如果我使用请求获取数据,它可以正常工作:
(lldb) po [cacheContext executeFetchRequest:request error:nil];
<_PFBatchFaultingArray 0x7fd9ca576710>(
<Appointment: 0x7fd9ca4c7220> (entity: Appointment; id: 0xd00000000004000a <x-coredata://36BACA76-7C2B-413C-8782-F92BBC7C1AA7/Appointment/p1> ; data: {
attendees = "<relationship fault: 0x7fd9ca518400 'attendees'>";
attendeesOmitted = nil;
created = "2015-03-10 16:41:31 +0000";
creator = nil;
end = "2015-03-18 18:45:00 +0000";
eventDescription = "Doctor Garc\U00eda Villaran";
eventID = "_8gs3ecpi8h0jab9i6op3ib9k6gp3iba18kojiba68d1j8c2260pj4e1o64";
lastUpdated = "2015-03-10 16:42:13 +0000";
location = "Centro M\U00e9dico Quir\U00f3n Sevilla Este, Sevilla, ES";
start = "2015-03-18 17:45:00 +0000";
status = 0;
title = "Cita otorrino";
})
)
我看不出我错过了什么......
亲切的问候
答案 0 :(得分:0)
我是我的错,我错过的是我使用的是MagicalRecord,它实际上提供了获取NSFetchedResultsController的方法。
NSManageObject MR_fetchAll ...或朋友将完成这项工作。就我而言,我用过:
_fetchedResultController = [Appointment MR_fetchAllSortedBy:@"start"
ascending:YES
withPredicate:nil
groupBy:@"day"
delegate:nil
inContext:cacheContext];