【发布时间】:2015-10-20 14:53:55
【问题描述】:
所以我成功地将 Magical Record 与我的 Core Data/iCloud 应用程序一起使用。 这是我初始化它的方式:
- (NSString *) iCloudContainerID {
return [NSString stringWithFormat:@"iCloud.%@", [[NSBundle mainBundle] bundleIdentifier]];
}
- (NSString *) localStoreName {
return @"test.sqlite";
}
- (NSString *) iCloudContentNameKey {
return @"test";
}
- (void) setupCoreDataWithiCloud {
NSString *containerID = [self iCloudContainerID];
DDLogiCloud(@"Setting up Core Data with iCloud");
[MagicalRecord setupCoreDataStackWithiCloudContainer:containerID
contentNameKey:[self iCloudContentNameKey] // Must not contain dots
localStoreNamed:[self localStoreName]
cloudStorePathComponent:@"Documents/CloudLogs" // Subpath within your ubiquitous container that will contain db change logs
completion:^{
// This gets executed after all the setup steps are performed
// Uncomment the following lines to verify
NSLog(@"%@", [MagicalRecord currentStack]);
// NSLog(@"%i events", [Event countOfEntities]);
}];
}
使用上面的代码正确设置 Core Data 和 iCloud。除了一些通知处理程序外,我还可以在本地向核心数据添加记录,并且这些更改会通过 iCloud 传播到其他设备。
在测试期间,最好以编程方式重置所有内容。这是我一直在尝试根据 Apple Docs 重置 CoreData/iCloud 的例程:https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW33
- (bool) resetCoreDataWithiCloud {
NSString *containerID = [self iCloudContainerID];
NSURL *cloudURL = [NSPersistentStore MR_cloudURLForUbiqutiousContainer:containerID];
NSString *contentNameKey = [self iCloudContentNameKey];
NSDictionary *options = @{contentNameKey:NSPersistentStoreUbiquitousContentNameKey,cloudURL:NSPersistentStoreUbiquitousContentURLKey};
NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:[self localStoreName]];
// NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:[MagicalRecord defaultStoreName];
// [MagicalRecord cleanUp];
DDLogiCloud(@"Resetting CoreData and iCloud named %@ at %@",containerID,storeURL);
NSError *error;
bool removeResult = [NSPersistentStoreCoordinator removeUbiquitousContentAndPersistentStoreAtURL:storeURL options:options error:&error];
if (removeResult == NO) {
DDLogError(@"Could not remove iCloud Container. Reason: %@", error.localizedFailureReason);
}
return removeResult;
}
无论我尝试什么,对 removeUbiquitous... 的调用都会引发“必须传入商店名称”的错误。
任何有关这方面的指导都会有所帮助!
【问题讨论】:
标签: ios core-data icloud magicalrecord