我第一次使用 RestKit,我在获取对象以保存到数据库时遇到了一些问题,或者它们的值没有得到 stetted。
我有一个看起来像这样的 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。
关于如何解决这个问题的任何想法?将不胜感激!