我完全重写了我之前由 3rd 方开发的应用程序。他们为 CoreData 使用了 Magical Record,我在模拟器上的 /Library/Application Support/AppName/ 文件夹中找到了他们的 appname.sqlite 文件。
我仍在学习 CoreData 并使用 Xcode 中的 CoreData 应用程序模板,我的 sqlite 位于 /Documents/ 文件夹中。有人可以建议打开旧商店的代码以便我执行迁移吗?谢谢你。
原始代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[MagicalRecordHelpers setupCoreDataStack];
.....
return YES;
}
魔法记录:
+ (void) setupCoreDataStack
{
NSManagedObjectContext *context = [NSManagedObjectContext MR_context];
[NSManagedObjectContext MR_setDefaultContext:context];
}
+ (void) MR_setDefaultContext:(NSManagedObjectContext *)moc
{
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_defaultStoreCoordinator];
if ([MagicalRecordHelpers isICloudEnabled])
{
[defaultManageObjectContext_ MR_stopObservingiCloudChangesInCoordinator:coordinator];
}
MR_RETAIN(moc);
MR_RELEASE(defaultManageObjectContext_);
defaultManageObjectContext_ = moc;
if ([MagicalRecordHelpers isICloudEnabled])
{
[defaultManageObjectContext_ MR_observeiCloudChangesInCoordinator:coordinator];
}
}
+ (NSPersistentStoreCoordinator *) MR_defaultStoreCoordinator
{
if (defaultCoordinator_ == nil && [MagicalRecordHelpers shouldAutoCreateDefaultPersistentStoreCoordinator])
{
[self MR_setDefaultStoreCoordinator:[self MR_newPersistentStoreCoordinator]];
}
return defaultCoordinator_;
}
+ (NSPersistentStoreCoordinator *) MR_newPersistentStoreCoordinator
{
NSPersistentStoreCoordinator *coordinator = [self MR_coordinatorWithSqliteStoreNamed:[MagicalRecordHelpers defaultStoreName]];
MR_RETAIN(coordinator);
return coordinator;
}
+ (NSString *) defaultStoreName;
{
NSString *defaultName = [[[NSBundle mainBundle] infoDictionary] valueForKey:(id)kCFBundleNameKey];
if (defaultName == nil)
{
defaultName = kMagicalRecordDefaultStoreFileName;
}
if (![defaultName hasSuffix:@"sqlite"])
{
defaultName = [defaultName stringByAppendingPathExtension:@"sqlite"];
}
return defaultName;
}
编辑:
超越文件的代码:
- (void)migrateVersion1
{
NSURL *version1URL = [[[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"Bundlename/Bundlename.sqlite"];
NSManagedObjectModel *version1Model = [[NSManagedObjectModel alloc] initWithContentsOfURL:version1URL];
NSPersistentStoreCoordinator *version1PCS;
NSError *error = nil;
if (version1Model != nil) {
version1PCS = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:version1Model];
}
if (![version1PCS addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:version1URL options:nil error:&error]) {
// handle error
}
NSManagedObjectContext *version1Context;
if (version1PCS != nil) {
version1Context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
[version1Context setPersistentStoreCoordinator:version1PCS];
}
// start parsing
}