gpt4 book ai didi

objective-c - RestKit不将数据保存到数据库

转载 作者:行者123 更新时间:2023-11-29 04:35:17 26 4
gpt4 key购买 nike

我是第一次使用 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。

关于如何解决这个问题有什么想法吗?将不胜感激!

最佳答案

尝试这个映射:

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:@""];

它说,当您检测“类别”时使用此映射,并通过“categoryId”索引核心数据对象。

RKManagedObjectMapping* categoryMapping = [RKManagedObjectMapping mappingForClass: [Category class]];
categoryMapping.primaryKeyAttribute = @"categoryId";
[categoryMapping mapKeyPathsToAttributes: @"id", @"categoryId", @"name", @"name", nil];

[[RKObjectManager sharedManager].mappingProvider setMapping:categoryMapping forKeyPath:@"categories"];

关于objective-c - RestKit不将数据保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11140462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com