0
我第一次使用RestKit,並且在將對象保存到數據庫或者它們的值沒有被打斷時遇到了一些問題。RestKit不會將數據保存到數據庫
我有一個JSON請求,看起來是這樣的:
{"categories":{"id":1 ...
我設置映射的分類是這樣的:
RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURLString:@"[base-url]"];
RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"App.sqlite"];
objectManager.objectStore = objectStore;
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
RKManagedObjectMapping *categoryMapping = [RKManagedObjectMapping mappingForClass:[Category class] inManagedObjectStore:objectManager.objectStore];
[objectManager.mappingProvider setObjectMapping:categoryMapping forResourcePathPattern:@"[path-to-json].json"];
[[RKObjectManager sharedManager].mappingProvider setMapping:categoryMapping forKeyPath:@"categories"];
// Maps keys between the database and costom model object.
categoryMapping.primaryKeyAttribute = @"categoryId";
[categoryMapping mapKeyPath:@"categories.id" toAttribute:@"categoryId"];
[categoryMapping mapKeyPath:@"categories.name" toAttribute:@"name"];
[...]
我該類別的自定義模型對象看起來是這樣的:
@interface Category : NSManagedObject
@property (nonatomic, retain) NSNumber *categoryId;
@property (nonatomic, retain) NSString *name;
[...]
@implementation Category
@dynamic categoryId;
@dynamic name;
[...]
我加載這樣的對象(很像Twitter示例):
// Loads from the database
- (void)loadObjectsFromDataStore {
NSFetchRequest *request = [Category fetchRequest];
_categories = [Category objectsWithFetchRequest:request];
}
- (void)loadCategories {
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:@"[path-to-json].json" delegate:self];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
// The array is always empty.
NSLog(@"Objects: %@", objects);
[[NSUserDefaults standardUserDefaults] synchronize];
[self loadObjectsFromDataStore];
}
我說過,當我從它被插入模擬器檢查本地數據庫但值是NULL和CATEGORY_ID總是0
上的任何想法如何解決這個問題?將非常感激!通過 「的categoryId」
RKManagedObjectMapping* categoryMapping = [RKManagedObjectMapping mappingForClass: [Category class]];
categoryMapping.primaryKeyAttribute = @"categoryId";
categoryMapping.rootKeyPath = @"categories";
[categoryMapping mapKeyPathsToAttributes: @"id", @"categoryId", @"name", @"name", nil];
和
[[RKObjectManager sharedManager].mappingProvider setMapping:categoryMapping forKeyPath:@""];
它說,使用該地圖,當你發現 「類別」,和索引核心數據對象,具有:
謝謝,第一個映射的工作,我也沒必要改變'[RKObjectManager sharedManager] .mappingProvider setMapping:categoryMapping forKeyPath:@ 「」] ;'但我改變了這個:'objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName usingSeedDatabaseName:nil managedObjectModel:nil delegate:self];'。謝謝。 – Anders