2014-12-05 61 views
4

我想用神奇記錄使用restkit。Restkit +魔法記錄問題查找sql​​文件

所以我已經設定,按照以下網址: https://github.com/blakewatters/RKMagicalRecord

我的問題是,如果我試圖創建一個使用魔法記錄的記錄,我總是得到下面的錯誤它試圖在第一時間保存一切。如果我關閉了應用程序並重新啓動它,那就沒問題。

+ 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

你怎麼指導RestKit設置它? – 2014-12-05 10:07:33

+0

我在https://github.com/blakewatters/RKMagicalRecord中關注了項目 – user281300 2014-12-05 10:20:34

+0

在問題中顯示代碼,包括安裝和保存,並顯示完整的錯誤消息和堆棧跟蹤。 – Wain 2014-12-05 10:39:05

回答

0

我刪除神奇記錄並使用以下設置它:

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];