• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将 iCloud 数据迁移到本地存储并阻止 iCloud 仍然响应

[复制链接]
菜鸟教程小白 发表于 2022-12-12 11:01:55 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我非常接近在我的应用中完成 iCloud 和 Core Data 的实现。这是一个仅限 iOS 7 的应用程序。

我在应用程序中为用户提供了一个选项,以 UISwitch 的形式说使用 iCloud 开启或关闭。该应用程序启动并询问用户是否要使用 iCloud,然后可以在应用程序中进行更改。

我将数据从本地存储(如果存在)迁移到 iCloud。我正在努力解决的一个方面是将数据从 iCloud 存储迁移到本地存储。

场景: - 用户 A 有一部装有我的应用程序和一些数据的 iPhone - 用户 A 将应用程序下载到 iPad 上,选择使用 iCloud,但后来决定他们不想在 iPad 上同步。所以他们关闭了应用程序内的 iPad iCloud 同步。

在这一点上,我的理解是用户希望数据仍然存在,但从幕后看,它现在是“本地”而不是基于“iCloud”的数据,这意味着来自其他人的条目设备不会同步到此设备,反之亦然

所以我正在努力实现这一目标。

问题

我可以将数据从 iCloud 迁移到本地存储,但问题是由于设备上仍然启用了 iCloud,下次运行应用程序时,我可以看到应用程序内禁用了 iCloud但是控制台仍然显示 Using Local Storage 1 然后 0 并且来自 iPhone 的任何数据都会同步到 iPad,即使 Use iCloud UISwitch 非常明显关闭。

这是进行迁移的主要代码:

- (void)migrateiCloudStoreToLocalStore {

    NSLog(@"Migrate iCloudToLocalStore");
    NSPersistentStore *store = self.persistentStoreCoordinator.persistentStores.lastObject;

    //NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent"Envylope.sqlite"];
    NSURL *storeURL = [self.persistentStoreCoordinator.persistentStores.lastObject URL];
    NSLog(@"Current Store URL (before iCloud to Local migration): %@", [storeURL description]);

    NSDictionary *localStoreOptions = nil;
    localStoreOptions = @{ NSPersistentStoreRemoveUbiquitousMetadataOption : @YES,
                           NSMigratePersistentStoresAutomaticallyOption : @YES,
                           NSInferMappingModelAutomaticallyOption : @YES};

    NSPersistentStore *newStore = [self.persistentStoreCoordinator migratePersistentStore:store
                                                                                     toURL:storeURL
                                                                                   options:localStoreOptions
                                                                                  withType:NSSQLiteStoreType error:nil];

    [self reloadStore:newStore];
}

- (void)reloadStoreNSPersistentStore *)store {
    NSLog(@"Reload Store");        
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent"Envylope.sqlite"];
    NSDictionary *localStoreOptions = nil;
    localStoreOptions = @{ NSPersistentStoreRemoveUbiquitousMetadataOption : @YES,
                           NSMigratePersistentStoresAutomaticallyOption : @YES,
                           NSInferMappingModelAutomaticallyOption : @YES};

    if (store) {
        [self.persistentStoreCoordinator removePersistentStore:store error:nil];
    }

    [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                  configuration:nil
                                                            URL:storeURL
                                                        options:localStoreOptions
                                                          error:nil];
    storeURL = [self.persistentStoreCoordinator.persistentStores.lastObject URL];
    NSLog(@"Current Store URL (after iCloud to Local migration): %@", [storeURL description]);

    NSLog(@"Done reloading");

    [[NSUserDefaults standardUserDefaults]setBool:YES forKey"MigratedFromiCloudToLocal"];
    [[NSUserDefaults standardUserDefaults]synchronize];

}

当然,里面有一些冗余代码,但是 NSLog 之前的 iCloud 到本地迁移显示:

 Current Store URL (before iCloud to Local migration): file:///var/mobile/Applications/62CAFCEE-450E-410D-9D56-4918DD70EC07/Documents/CoreDataUbiquitySupport/mobile~98457667-A467-4095-BB7F-EF36EB2B0FE1/EnvyCloud/CD08912E-C135-4056-9557-6B47C84ECEF9/store/Envylope.sqlite

数据迁移后的NSLog显示:

Current Store URL (after iCloud to Local migration): file:///var/mobile/Applications/62CAFCEE-450E-410D-9D56-4918DD70EC07/Documents/Envylope.sqlite

这清楚地表明数据已迁移到本地存储。但是,此时在控制台中,我不确定会发生什么,但理想情况下,此过程应该与设备上根本没有启用 iCloud 具有相同的效果(如果我删除我的应用程序,请删除我的 iCloud 帐户在设备上运行应用程序,我得到了预期 - 运行应用程序没有任何 iCloud 选项)。这不会发生,因为它继续插入 iCloud 更新。在不再次运行应用程序的情况下,添加到其他设备中的数据会在此处同步。再次运行应用程序,使用本地存储 1 和 0 出现在控制台中。

因此,数据可能已经迁移到本地存储,但在应用内禁用 iCloud 似乎没有任何效果。

我真的很感激对此的一些想法。

更新:已找到以下两个答案的组合解决方案。对于任何关注此问题以查看下一个场景的人,请参阅此问题:Migrating an iCloud Store to a Local Store and making sure the data is there through each app launch



Best Answer-推荐答案


这是一个两步过程:就像@wottle 建议的那样,从您的应用程序中删除所有三个观察者。将 iCloud 数据迁移到新的本地存储文件后执行此操作:

- (void)removeCloudObservers {

[[NSNotificationCenter defaultCenter]removeObserver:self name:NSPersistentStoreCoordinatorStoresWillChangeNotification object:self.managedObjectContext.persistentStoreCoordinator];
[[NSNotificationCenter defaultCenter]removeObserver:self name:NSPersistentStoreCoordinatorStoresDidChangeNotification object:self.managedObjectContext.persistentStoreCoordinator];
[[NSNotificationCenter defaultCenter]removeObserver:self name:NSPersistentStoreDidImportUbiquitousContentChangesNotification object:self.managedObjectContext.persistentStoreCoordinator];
}

接下来,如果您绝对确定要从 iCloud 中删除整个无处不在的容器(就像我们之前手动所做的那样),您可以调用 NSPersistentStoreCoordinator 类的单例方法 (removeUbiquitousContentAndPersistentStoreAtURL)。获取有问题的 iCloud 存储文件(我将其称为 yourStore),然后使用以下内容:

- (void)removeCloudContainerNSPersistentStore *)yourStore {

NSError *error = nil;
if (![NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:yourStore.URL options:yourStore.options error:&error]) {
    NSLog(@"That didn't work so well, and here's why: %@", error.localizedFailureReason);
}
}

这将删除整个容器,其他设备将无法再访问 iCloud 数据。一旦从协调器中删除了 iCloud 商店,并且应用程序已经在本地商店中正常运行时,请执行此操作。

从 UI 的角度来看,在所有设备上的所有数据都已迁移到本地存储之后让用户做出此选择可能是个好主意,而不是将其集成到“关闭 iCloud”例程中 - 只是因为它与仍然期望 iCloud 商店存在的同行打交道可能会很棘手。

关于ios - 将 iCloud 数据迁移到本地存储并阻止 iCloud 仍然响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25186787/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap