- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个简单的应用程序,它是一个 UITableView
,由用户在不同的 View 中填写一些 UITextFields
来填充。此信息被保存到 CoreData
并且 UITableView
使用 NSFetchedResultsController
得到更新。
我现在正在将 iCloud 同步引入我的环境。引用之前提出的问题:Existing Core Data Database Not Uploading to iCloud When Enabling iCloud On My App ,我仍然有问题,但是用我的新代码更新那个问题太麻烦了。我特别指的是 WWDC 2013 CoreData
session ,以及 this .因为我的应用程序只有 iOS 7。我是 iCloud 开发的新手,是一名中级程序员。
当我从 App Store(非 iCloud)版本更新到开发(iCloud)版本时,我在将现有数据迁移到 iCloud 时遇到了一些很大的困难。新数据完美同步。
我知道我基本上需要在没有 iCloud 的情况下使用我现有的商店并将其迁移到 iCloud,但这对我来说并不奏效。
这里有一些代码来说明我正在做什么:
更新
我已经仔细考虑过这个问题,并删除了多余的代码。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Planner.sqlite"];
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *url = [fm URLForUbiquityContainerIdentifier:nil];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (url)
{
NSLog(@"iCloud is enabled");
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES,
};
NSPersistentStore *localStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error];
[_persistentStoreCoordinator migratePersistentStore:localStore toURL:[self iCloudURL] options:[self iCloudStoreOptions] withType:NSSQLiteStoreType error:&error];
}
else
{
NSLog(@"iCloud is not enabled");
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES
};
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[self iCloudURL] options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"Reload" object:self userInfo:nil];
});
return _persistentStoreCoordinator;
}
我不再调用 migrateLocalStoreToiCloud 方法,但它在这里供引用:
/*-(void)migrateLocalStoreToiCloud
{
//assuming you only have one store.
NSPersistentStore *store = [[_persistentStoreCoordinator persistentStores] firstObject];
NSPersistentStore *newStore = [_persistentStoreCoordinator migratePersistentStore:store
toURL:[self iCloudURL]
options:[self iCloudStoreOptions]
withType:NSSQLiteStoreType
error:nil];
[self reloadStore:newStore];
}
*/
storeURL
、storeOptions
、iCloudURL
和 iCloudStoreOptions
代码是:
- (NSURL *)iCloudURL
{
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *url = [fm URLForUbiquityContainerIdentifier:nil];
if (url) {
NSLog(@"iCloud access at %@", url);
} else {
NSLog(@"No iCloud access");
}
return url;
}
-(NSURL *)storeURL{
return [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Planner.sqlite"];
}
-(NSDictionary *)storeOptions{
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
return options;
}
-(NSDictionary *)iCloudStoreOptions{
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
[options setObject:@"PlannerStore" forKey:NSPersistentStoreUbiquitousContentNameKey];
return options;
}
问题
当我按原样运行这段代码时,控制台中根本没有显示 Using Local Storage : 1
和 0
,尽管数据在那里.
如果我更改 persistentStoreCoordinator
,它首先添加 persistentStore 以使用 [self iCloudStoreOptions]
作为选项,Using Local Storage 1
和 0
显示,但数据不在设备上。
我遵循此处的指导并且尝试了多种代码变体,包括不删除 reloadStore 方法中的任何 persistentStore,但我完全卡住了。
我已经阅读了很多帖子,但似乎我就是找不到将数据从非 iCloud 版本迁移到 iCloud 版本的人,并且根据我找到的示例,没有代码。
我调查了this虽然它是一个很棒的代码集,但我已经下载了代码并遵循了那些不会删除任何 persistentStore 的指南,但它仍然对我不起作用。我无法想象我要实现的目标非常复杂。我只需要获取现有的非 iCloud 数据并将其迁移到 iCloud。
更新
阅读 Apple 指南:https://developer.apple.com/LIBRARY/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW2我可以看到我的 migrateLocaltoiCloud
方法目前在删除 persistentStore
等方面是错误的,但我不知道如何修复它。如果我从 persistentStoreCoordinator
调用它,它会以某种方式干扰 DidChangeNotification
吗?
我真的坚持这一点,我将不胜感激任何指向正确方向的指导
最佳答案
我在这里提供我的问题的答案,因为它比更新问题更清楚,并且经过大量调试后,我在某种程度上可以正常工作。
通过调试 addPersistentStore
和 migratePersistentStore
行代码,我想出了:
NSPersistentStore *localStore = [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error];
NSLog(@"The localStore is %@", localStore);
NSPersistentStore *migratedStore = [_persistentStoreCoordinator migratePersistentStore:localStore toURL:[self iCloudURL] options:[self iCloudStoreOptions] withType:NSSQLiteStoreType error:&error];
NSLog(@"The migrationStore is %@", migratedStore);
The NSLog value of localStore is: The localStore is <NSSQLCore: 0x13451b100> (URL: file:///var/mobile/Applications/DDB71522-C61D-41FC-97C7-ED915D6C46A4/Documents/Planner.sqlite)
The NSLog value of migratedStore is: (null)
通过调试,我可以看到基本相同的东西,即 migrateStore 始终为 null。
我稍微更改了代码:
NSPersistentStore *migratedStore = [_persistentStoreCoordinator migratePersistentStore:localStore toURL:storeURL options:[self iCloudStoreOptions] withType:NSSQLiteStoreType error:&error];
为了将 URL 设为 storeURL,突然之间,一切都开始正常工作,其中 storeURL:
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Planner.sqlite"];
我的输出是:
The localStore is <NSSQLCore: 0x14ee09b50> (URL: file:///var/mobile/Applications/CC0F7DA0-251F-46D6-8E43-39D63062AED2/Documents/Planner.sqlite)
[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](771): CoreData: Ubiquity: mobile~5BD59259-7758-42FB-986F-DDD173F75ED3:EnStor
The migrationStore is <NSSQLCore: 0x157e260b0> (URL: file:///var/mobile/Applications/A08D183D-ECD4-4E72-BA64-D21727DE7A3E/Documents/CoreDataUbiquitySupport/mobile~CB6C3699-8BF1-47DB-9786-14BCE1CD570A/EnStor/771D2B0B-870A-428E-A9A8-5DAE1295AF53/store/Planner.sqlite)
“使用本地商店”从 1 变为 0,并且在迁移到新版本的过程中,我的应用程序商店版本的数据继续存在。这是我在升级后第一次运行该应用程序时的输出。
这是个好消息。
虽然我随后每次运行该应用程序时,数据都会继续迁移,因此我最终会得到重复的结果。
问题还在于,添加新设备时,它不会将信息下拉到新设备。
我不太明白为什么使用 storeURL 作为 migratePersistentStore 的 url 有效?
我也不希望每个新设备基本上都添加一个新的持久性存储(如果已经存在的话),我不知道如何解决这个问题,而且我也不希望迁移一次又一次地发生。它应该只发生一次。
我快到了,但还有很多工作要做!
关于ios - 将现有的核心数据迁移到 iCloud,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23674870/
我有一个如下图所示的情节。对于这个情节,我想在情节(右下角或左下角)的某处添加类似的线图。我正在使用的子图的命令是 plot( 1:121, sample(1:121),type='l' ) 它绘制在
我有一个单表数据库,我继承并迁移到 SQL Server,然后通过创建、链接和填充一大堆表示主表中项目的查找类型表来规范化它。我现在想用它们的外键替换原始表中的那些项目。我是不是一直在写一堆查询或 U
我有一个 Web 应用程序,它当前正在从服务器获取 PDF 的 base64 表示。我可以使用 Mozilla 的 pdf.js 在 上显示它并使用下拉菜单切换页面。 根据我所能找到的一切和Can
在 DB2 上运行的 Moodle 2 安装中,删除用户不成功,返回从数据库读取错误: Debug info: [IBM][CLI Driver][DB2/LINUXX8664] SQL0206N "
我在grails项目的RH包中添加了一个名为Authorization的新域类。 然后,我从grails菜单自动生成了 Controller 和 View 。 但是当我尝试输入 Controller
今天,我发现了一个有趣的plunker,经过谷歌大量搜索后一无所获,希望我能在这里找到答案。我只是想要那个笨蛋的副本。我不想使用复制和粘贴技术。有什么方法可以获取已建立的 plunk 的副本吗?我如何
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Migrate normal sqlite3 database to core data? 是否可以将现有的 sql
我正在尝试在我的应用程序上添加启动画面。我干净地构建了程序,但我选择了错误的文件。现在我第二次编辑了 VM 选项并再次干净构建,现在我收到此错误: C:\Users\User\Documents\Ne
我已经查看了很多问题,我不相信这是重复使用单元格的结果,因为新的单元格图像是正确的,但是现有的单元格图像不正确并且曾经是正确的。我会先发布图片,以便更容易理解问题。 我有一个图像单元的 Collect
我在来自 Vaadin 的 ContainerHierarchicalWrapper 的这段代码中有一个非常奇怪的错误: for (Object object : children.keySet())
到目前为止,我正在使用 Globalize用于我的 JavaScript 应用程序的 i18n 和 l10n(使用 jQuery UI 构建)。这行得通,但它将我的代码与另一个特定的库联系在一起。现在
我正在创建一个 JHipster 应用程序,现在确定了 full text search 的必要性.我知道 JHipster 与 Elasticseach 集成,但我在创建项目时没有启用它。有没有一种
我一直在寻找堆栈中的建议,但我仍然不能 100% 确定改进它的最佳方法。我有一个存储大约 130K 条记录的 mysql INNODB“产品”表。杂项产品数据等大约有 80 个字段,然后我们一直在为每
我在一本书上看到,它说:当我们使用另一个初始化新创建的对象时 - 使用复制构造函数创建一个临时对象,然后使用赋值运算符将值复制到新对象! 后来在书中我读到:当使用另一个对象初始化新对象时,编译器创建一
我第一次安装现有的 Django 项目时遇到了启动服务器 python manage.py runserver 的问题 这是我做的 1.克隆仓库, 2.制作虚拟环境 3.pip安装要求.txt 4.生
我有一个网站,还有一个登录表单。我不想使用 PHP 来检查我的 MySQL 数据库,因此我正在寻找一种方法来检查用户凭据以查看是否已有 Linux 用户。我知道 PAM,但我还没有找到任何有关如何从网
我有一个现有的 Umbraco 项目在 IIS 服务器上运行。当我开始这个项目时,我基本上是将 Umbraco 直接安装到服务器上,并通过管理界面进行编码,直到网站启动并上线。 现在,客户想要一些更改
我是 Android 开发新手,目前正在学习一些教程。当我在 Eclipse 中设置一个新的 Android 项目,并选择 Windows -> Android SDK and AVD Manager
我有这个注册页面可以正常工作,但对于电子邮件字段,我需要确保电子邮件正确有效1:正确2 : 有效 为了正确添加电子邮件,我正在使用 Java 脚本验证来维护abc@def.com 很好用 但我的问题是
首先让我说我不熟悉 COM 引用,并且我在 Windows 7 64 位计算机上使用 VS2010。今天早上,我从 TFS 中删除了一个现有项目。然后我尝试构建项目并收到此错误: The type o
我是一名优秀的程序员,十分优秀!