2

我需要创建两个持久存储,每个存储都有自己的实体和一个持久存储协调器。困难的部分是,我希望一个持久存储链接到 iCloud,但另一个只能是本地存储。我已阅读有关为托管对象模型进行不同配置的信息,但是如何从本地存储而不是启用 iCloud 的存储中获取实体?到目前为止,这是我的代码,我是否朝着正确的方向前进?:

    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);
    }

在我的托管对象模型中,我有一个名为“下载”的实体的配置,我只想保存在本地,但它也在默认配置中,我想链接到 iCloud。如何确保以正确的配置保存实体?

4

0 回答 0