Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c RestKit不';t将数据保存到数据库_Objective C_Ios_Restkit - Fatal编程技术网

Objective c RestKit不';t将数据保存到数据库

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

我是第一次使用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;
[...]
我为类别设置了如下映射:

{"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];`。谢谢