【问题标题】:Restkit + Magical Record issue finding sql fileRestkit + Magical Record问题查找sql​​文件
【发布时间】:2015-02-03 10:50:16
【问题描述】:

我正在尝试使用带有神奇记录的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{

    }
}];

【问题讨论】:

  • 你是如何指示 RestKit 设置它的?
  • 我在github.com/blakewatters/RKMagicalRecord关注了这个项目
  • 在问题中显示您的代码,用于设置和保存,并显示完整的错误消息和堆栈跟踪。
  • 您好,基本上,我认为问题在于我使用魔术记录来创建对象并保存。我为此使用了保存块。[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext)
  • 您可能需要调用 [managedObjectStore createPersistentStoreCoordinator];在您创建 managedObjectStore 之后立即

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


【解决方案1】:

我删除了 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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多