【问题标题】:Magical Record and iCloud enabling/disablingMagical Record 和 iCloud 启用/禁用
【发布时间】:2014-04-20 18:20:51
【问题描述】:

如何使用Magical Record处理正确的以下流程?假设我的应用启用了开启/关闭 iCloud 同步的功能。

  1. 用户安装应用程序。在启动时,他被问及 iCloud 偏爱。他的选择:不使用 iCloud。
  2. 用户在应用程序中创建了一些数据。然后他决定把它存起来 在 iCloud 中并启用 iCloud。
  3. 后来由于某种原因,用户在应用程序中禁用了 iCloud。数据 应该留在本地。

如何正确设置魔法唱片?

更新:

Source code

【问题讨论】:

  • 你设法让它工作了吗?链接中的代码是否相关?
  • @Dvole,不,我必须切换到另一个功能。是的,这是相关的。

标签: ios core-data icloud magicalrecord


【解决方案1】:

在 iOS 7 中,在您的应用中实现启用或禁用 iCloud 的开关也容易得多,尽管对于大多数应用来说这可能不是必需的。因为当 iCloud 选项在创建时传递给 NSPersistentStore 时,API 现在会自动创建一个单独的文件结构,所以我们可以在本地和 iCloud 存储之间拥有相同的存储 URL 和许多相同的选项。这意味着从 iCloud 存储切换到本地存储可以通过将 iCloud 持久存储迁移到具有相同选项的相同 URL 以及 NSPersistentStoreRemoveUbiquitousMetadataOption 来完成。此选项将取消普遍存在的元数据与存储的关联,并且专为此类迁移或复制场景而设计。这是一个示例:

- (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 的选项进行迁移,然后将具有相同选项的持久存储添加到协调器。

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

【讨论】:

  • 是的,当我开始实现此功能时,我已经阅读了这篇文章。好的,当我迁移 iCloud -> 本地时,它就像一个魅力。但是,当我尝试迁移本地 -> iCloud 时,本地数据会被清除并替换为 iCloud 数据。我想你最好检查我的来源CODE
猜你喜欢
  • 2012-12-15
  • 2012-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
相关资源
最近更新 更多