1

I'm sharing a Core Data Controller class between versions of my apps.

The code works fine for an iPhone version but stops short on the iPad version.

Here's the code called from the RootVC:

    #pragma mark - NSFetchedResultsController

    - (NSFetchedResultsController *)fetchedResultsController {
         NSLog(@"%s", __FUNCTION__);
        if (_fetchedResultsController != nil) {
            return _fetchedResultsController;
        }


         // Create the fetch request for the entity   
        NSLog(@"1");
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:self.managedObjectContext];

 // Set up the fetched results controller

        NSLog(@"2");
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSLog(@"3");

        [fetchRequest setEntity:entity];
       NSLog(@"4");
        // Set the batch size to a suitable number
        [fetchRequest setFetchBatchSize:20];

        // Edit the sort key as appropriate.
        NSSortDescriptor *titleSortDesc = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
        NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:titleSortDesc, nil];

        [fetchRequest setSortDescriptors:sortDescriptors];

        // Edit the section name key path and cache name if appropriate,
        // nil for section name key path means "no sections"
        NSFetchedResultsController *aFetchedResultsController =
            [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                managedObjectContext:self.managedObjectContext
                                                  sectionNameKeyPath:nil
                                                           cacheName:nil];

        aFetchedResultsController.delegate = self;
        self.fetchedResultsController = aFetchedResultsController;

        return _fetchedResultsController;
    }    

Log entry 1 shows. Log entry 2 doesn't show. I get this crash instead:

2012-11-19 08:37:26.402  I'm an iPad
2012-11-19 08:37:26.400  -[CoreDataController asyncLoadPersistentStores]
2012-11-19 08:37:26.403  -[CoreDataController loadiCloudStore:]
2012-11-19 08:37:26.404  moc is <NSManagedObjectContext: 0x1e03e4f0>
2012-11-19 08:37:26.421  -[CoreDataController iCloudStoreURL]
2012-11-19 08:37:26.454  -[CoreDataController folderForUbiquityToken:]
2012-11-19 08:37:26.460  -[RootViewController viewDidLoad]
2012-11-19 08:37:26.469   RVC moc is (null)
2012-11-19 08:37:26.470  -[RootViewController fetchedResultsController]
2012-11-19 08:37:26.472  1
2012-11-19 08:37:26.474  Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Person''
*** First throw call stack:

I'm passing the managed object context from the App Delegate to the RootVC like this:

rootViewController.managedObjectContext = self.coreDataController.mainThreadContext;

I could use some help figuring this out. I dont get whats different between the way it works for iPhone and OSX but not for iPad.. Same share data controller.

UPDATE:

These are the results from logs suggested in a comment by Phillip, below...

RVC is <RootViewController: 0x1f88d1a0>
-[CoreDataController loadiCloudStore:]
MOC is <NSManagedObjectContext: 0x1eda3630>
-[CoreDataController iCloudStoreURL]
-[RootViewController viewDidLoad]
-[CoreDataController folderForUbiquityToken:]
-[RootViewController fetchedResultsController]
1
SELF is <RootViewController: 0x1f8859d0>
4

1 回答 1

2

从评论中......

记录rootViewController应用程序委托中的地址和self控制器自己的方法中的相应地址表明,不知何故,您有两个不同的 RootViewController 实例(不同的对象地址),其中您将 moc 设置到的实例不是尝试使用的实例它产生 fetchedResultsController。

于 2012-11-19T16:46:44.113 回答