Magical Record and iCloud enabling/disabling

前端 未结 1 702
星月不相逢
星月不相逢 2021-02-10 00:13

How to handle correct the following flow using Magical Record? Assume that my app enable feature to switch iCloud sync on/off.

  1. The user installs th
1条回答
  •  再見小時候
    2021-02-10 01:05

    Implementing a switch to enable or disable iCloud in your app is also much easier in iOS 7, although it probably isn’t necessary for most applications. Because the API now automatically creates a separate file structure when iCloud options are passed to the NSPersistentStore upon creation, we can have the same store URL and many of the same options between both local and iCloud stores. This means that switching from an iCloud store to a local store can be done by migrating the iCloud persistent store to the same URL with the same options, plus the NSPersistentStoreRemoveUbiquitousMetadataOption. This option will disassociate the ubiquitous metadata from the store, and is specifically designed for these kinds of migration or copying scenarios. Here’s a sample:

    - (void)migrateiCloudStoreToLocalStore {
        // assuming you only have one store.
        NSPersistentStore *store = [[_coordinator persistentStores] firstObject]; 
    
        NSMutableDictionary *localStoreOptions = [[self storeOptions] mutableCopy];
        [localStoreOptions setObject:@YES forKey:NSPersistentStoreRemoveUbiquitousMetadataOption];
    
        NSPersistentStore *newStore =  [_coordinator migratePersistentStore:store 
                                                                      toURL:[self storeURL] 
                                                                    options:localStoreOptions 
                                                                   withType:NSSQLiteStoreType error:nil];
    
        [self reloadStore:newStore];
    }
    
    - (void)reloadStore:(NSPersistentStore *)store {
        if (store) {
            [_coordinator removePersistentStore:store error:nil];
        }
    
        [_coordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                   configuration:nil 
                                             URL:[self storeURL] 
                                         options:[self storeOptions] 
                                           error:nil];
    }
    

    Switching from a local store back to iCloud is just as easy; simply migrate with iCloud-enabled options, and add a persistent store with same options to the coordinator.

    (c) http://www.objc.io/issue-10/icloud-core-data.html

    0 讨论(0)
提交回复
热议问题