Restkit + Magical Record问题找到sql文件

时间:2014-12-05 09:46:11

标签: ios objective-c core-data restkit magicalrecord

我正在尝试使用具有魔法记录的restkit。

所以我按照以下网址进行了设置: https://github.com/blakewatters/RKMagicalRecord

我的问题是,如果我尝试使用Magic Record创建记录,我第一次尝试保存任何内容时总是会出现以下错误。如果我关闭应用程序并重新启动它,那就没问题了。

+ MagicalRecord(ErrorHandling)defaultErrorHandler:错误:/var/mobile/Containers/Data/Application/1CECA7F7-510F-47F2-9F2D-4D0346C4E74F/Documents/WhateverModel.sqlite

这意味着它无法找到该文件。所以我检查了文件夹,是的,即使我指示restkit进行设置,它也不存在。

知道为什么要这个吗?

以下设置代码

NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Model" ofType:@"momd"]];
NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Model.sqlite"];
NSError *error = nil;
[managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
[managedObjectStore createManagedObjectContexts];

// Configure MagicalRecord to use RestKit's Core Data stack
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:managedObjectStore.persistentStoreCoordinator];
[NSManagedObjectContext MR_setRootSavingContext:managedObjectStore.persistentStoreManagedObjectContext];
[NSManagedObjectContext MR_setDefaultContext:managedObjectStore.mainQueueManagedObjectContext];

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:ROOT_URL]];
objectManager.managedObjectStore = managedObjectStore;

在下面创建和保存对象:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
    WhateverObject *obj = [WhateverObject createInContext:localContext];
}completion:^(BOOL successful, NSError *error) {

    if(successful){

    }else{

    }
}];

1 个答案:

答案 0 :(得分:0)

我删除了Magical Record并使用以下内容进行设置:

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:ROOT_URL]];

NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;
/**
 Complete Core Data stack initialization
 */
[managedObjectStore createPersistentStoreCoordinator];

NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"WhateverModel.sqlite"];

NSError *error;

NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil  withConfiguration:nil options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} error:&error];

NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];

// Configure a managed object cache to ensure we do not create duplicate objects
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];