我使用 RestKit 在本地缓存来自远程服务器的数据。在其中,我在类别 <<-->> 新闻之间有多对多的关系。映射似乎工作正常,尽管它还在我的类别表中保存了空值(它也保存了正确的类别)。如下图所示:
它似乎保存了 30 个空行,我的连接表中也有 30 个(非空)行,所以这里可能存在相关性。
我得到的 JSON 如下所示:"categories":[{"category_id":1},{"category_id":4}]
我有两个继承自NSManagedObject
.
@interface News : NSManagedObject
[...]
@property (nonatomic, retain) NSSet *categories;
@end
@interface Category : NSManagedObject
[...]
@property (nonatomic, retain) NSSet *news;
@end
我@dynamic
两个都用。
我的映射如下所示:
RKManagedObjectMapping *categoryMapping = [RKManagedObjectMapping mappingForClass:[Category class] inManagedObjectStore:objectManager.objectStore];
categoryMapping.primaryKeyAttribute = @"categoryId";
categoryMapping.rootKeyPath = @"categories";
[categoryMapping mapKeyPath:@"id" toAttribute:@"categoryId"];
[...]
RKManagedObjectMapping* newsMapping = [RKManagedObjectMapping mappingForClass:[News class] inManagedObjectStore:objectManager.objectStore];
newsMapping.primaryKeyAttribute = @"newsId";
newsMapping.rootKeyPath = @"news";
[newsMapping mapKeyPath:@"id" toAttribute:@"newsId"];
[...]
// Categories many-to-many (not fully working yet).
[newsMapping mapKeyPath:@"categories" toRelationship:@"categories" withMapping: categoryMapping];
// Register the mappings with the provider
[objectManager.mappingProvider setObjectMapping:newsMapping forResourcePathPattern:@"[path-to-JSON]"];
[objectManager.mappingProvider setObjectMapping:categoryMapping forResourcePathPattern:@"[path-to-JSON]"];
我像这样获取数据(很像 Twitter RestKit 示例):
- (id)init
{
self = [super init];
if (self) {
[self loadCategories];
[self loadCategoriesFromDataStore];
[self loadNews];
[self loadNewsFromDataStore];
}
return self;
}
- (void)loadCategoriesFromDataStore
{
NSFetchRequest* request = [Category fetchRequest];
NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"categoryId" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:descriptor]];
_categories = [Category objectsWithFetchRequest:request];
}
- (void)loadNewsFromDataStore
{
NSFetchRequest* request = [News fetchRequest];
NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"createdAt" ascending:NO];
[request setSortDescriptors:[NSArray arrayWithObject:descriptor]];
_news = [News objectsWithFetchRequest:request];
}
- (void)loadCategories
{
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:@"[link-to-JSON]" delegate:self];
}
- (void)loadNews
{
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager loadObjectsAtResourcePath:@"[link-to-JSON]" delegate:self];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
[[NSUserDefaults standardUserDefaults] synchronize];
[self loadCategoriesFromDataStore];
[self loadNewsFromDataStore];
}
任何想法我做错了什么?
注意: 当有 15 条新闻时,似乎也将 30 行保存到连接表中,我不知道这是否是连接表的正常行为。
更新:它还映射了连接表中的错误类别 ID(即类别 66,空行)。
更新 2: JSON 获取我的类别 {"categories":{[...], "id":1, [...]} 的请求