Objective c RestKit不';t将数据保存到数据库
我是第一次使用RestKit,但在将对象保存到数据库中时遇到了一些问题,或者它们的值没有得到存储 我有一个JSON请求,如下所示:Objective c RestKit不';t将数据保存到数据库,objective-c,ios,restkit,Objective C,Ios,Restkit,我是第一次使用RestKit,但在将对象保存到数据库中时遇到了一些问题,或者它们的值没有得到存储 我有一个JSON请求,如下所示: {"categories":{"id":1 ... RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURLString:@"[base-url]"]; RKManagedObjectStore* objectStore = [RKManagedObjectStore
{"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;
[...]
我为类别设置了如下映射:
{"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;
[...]
该类别的自定义模型对象如下所示:
{"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示例一样):
正如我所说的,当我从模拟器检查本地数据库时,它会被插入,但值为NULL,category_id始终为0
有没有办法解决这个问题?非常感谢 尝试此映射:
RKManagedObjectMapping* categoryMapping = [RKManagedObjectMapping mappingForClass: [Category class]];
categoryMapping.primaryKeyAttribute = @"categoryId";
categoryMapping.rootKeyPath = @"categories";
[categoryMapping mapKeyPathsToAttributes: @"id", @"categoryId", @"name", @"name", nil];
及
它说,在检测“类别”时使用此映射,并通过“categoryId”索引核心数据对象
或
及
谢谢,第一次映射成功了,我不需要更改
[[RKObjectManager sharedManager].mappingProvider setMapping:CategoryMappingForkeyPath:@”“代码>但我更改了以下内容:`objectManager.objectStore=[RkmanagedObjectStoreWithStoreFileName:databaseName usingSeedDatabaseName:nil managedObjectModel:nil delegate:self];`。谢谢