Magical Record和iCloud启用/禁用 [英] Magical Record and iCloud enabling/disabling

查看:198
本文介绍了Magical Record和iCloud启用/禁用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用魔法记录处理以下流程?假设我的应用程序启用功能可以打开/关闭iCloud同步。

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


  1. 用户安装应用程序。在启动时,他被问及iCloud
    的偏好。他的选择:不使用iCloud。

  2. 用户在应用程序中创建一些数据。然后他决定将它
    存储在iCloud中并启用iCloud。

  3. 由于某种原因,用户在应用程序中禁用iCloud。数据
    应保留在本地。

如何正确设置Magical Record?

How to setup Magical Record correctly?

源代码

推荐答案

在iOS 7中实现一个开关来启用或禁用iCloud更容易, t大多数应用所必需的。因为当iCloud选项在创建时传递到NSPersistentStore时,API现在自动创建单独的文件结构,所以我们可以在本地和iCloud存储之间具有相同的存储URL和许多相同的选项。这意味着可以通过将iCloud持久存储迁移到具有相同选项的相同URL和NSPersistentStoreRemoveUbiquitousMetadataOption来完成从iCloud存储切换到本地存储。此选项将从商店中解除无处不在的元数据,并专门为这些迁移或复制场景设计。这里有一个例子:

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

从本地商店切换回iCloud同样简单;只需使用支持iCloud的选项进行迁移,并向协调器添加具有相同选项的持久存储。

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

这篇关于Magical Record和iCloud启用/禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆