2

I need to create two persistent stores, each with their own entities, with one persistent store coordinator. The hard part is, I want one persistent store to be linked to iCloud, but the other to only be a local store. I have read about making different configurations for the managed object model, but how do I fetch entities from the local store, rather than the iCloud enabled store? Here is my code so far, Am I headed in the right direction?:

    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    [options setValue:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setValue:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];

    NSURL *cloudURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
    if (cloudURL)
    {
        NSLog(@"iCloud enabled: %@", cloudURL);
        cloudURL = [cloudURL URLByAppendingPathComponent:@"FSListen"];
        [options setValue:kICloudContentNameKey forKey:NSPersistentStoreUbiquitousContentNameKey];
        [options setValue:cloudURL forKey:NSPersistentStoreUbiquitousContentURLKey];
    }
    else
    {
        NSLog(@"iCloud is not enabled");
    }

    // create the persistent store that will be connected to iCloud for favorites
    NSURL *iClouldSoreURL= [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    iClouldSoreURL = [iClouldSoreURL URLByAppendingPathComponent:@"FSListen-iCloud.sqlite"];
    NSError *error = nil;
    NSPersistentStoreCoordinator *coordinator = [self.managedObjectContext persistentStoreCoordinator];
    NSPersistentStore *store = [coordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                         configuration:@"Default"
                                                                   URL:iClouldSoreURL
                                                               options:options
                                                                 error:&error];
    if (!store)
    {
        NSLog(@"Error adding persistent store to coordinator %@\n%@", [error localizedDescription], [error userInfo]);
        //Present a user facing error
    }

    // create the persistent store that will not be connected to iCloud for downloads
    NSError *downloadsStoreError = nil;
    NSURL *downloadsStoreURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    downloadsStoreURL = [downloadsStoreURL URLByAppendingPathComponent:@"FSListen-Downloads.sqlite"];
    NSPersistentStore *downloadsStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                                 configuration:@"Downloads"
                                                                           URL:downloadsStoreURL
                                                                       options:nil
                                                                         error:&downloadsStoreError];
    if (!downloadsStore)
    {
        NSLog(@"ERROR CREATING DOWNLOADS STORE %@", downloadsStoreError.localizedDescription);
    }

In my managed object model, I have one configuration with an entity called 'downloads' that I only want to be saved locally, but it is also in the default configuration, which I want to link to iCloud. How do I make sure I am saving my entities in the correct configuration?

Chandler De Angelis
  • 2,646
  • 6
  • 32
  • 45

0 Answers0