2015-03-30 58 views
0

我試圖使用一個NSFetchedResultsController與另一個NSManagedObjectContext不同於我在我的應用程序中使用的主要MOC。儘管我可以使用額外的MOC保存和檢索數據,但是每次嘗試創建NSFetchedResultsController時,都會返回一個帶有零fetchedObjects的對象。無法獲取使用NSFetchedResultsController與多個上下文的對象

我使用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

我是我的錯,我錯過的是事實,我使用MagicalRecord這實際上提供了獲取一個NSFetchedResultsController的方法。

NSManageObject MR_fetchAll ...或朋友將完成這項工作。在我的情況下,我用:

_fetchedResultController = [Appointment MR_fetchAllSortedBy:@"start" 
                 ascending:YES 
                withPredicate:nil 
                 groupBy:@"day" 
                 delegate:nil 
                 inContext:cacheContext]; 
相關問題