RestKit多对多关系在联接表中保存新行,在主表中保存空值 [英] RestKit many to many relationship saves new rows in both join table and null values in main table

查看:103
本文介绍了RestKit多对多关系在联接表中保存新行,在主表中保存空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RestKit在本地缓存来自远程服务器的数据.在其中,类别<<->>新闻之间存在多对多的关系.尽管映射也将空值保存在我的类别"表中(它也保存了正确的类别),但映射似乎正常工作.如下图所示:

I use RestKit to cache data from a remote server locally. In it I have a many to many relationship between Category <<-->> News. The mapping seems to work properly, despite that it also saves null values in my Category table (it saves the correct categories too). Like the image below:

似乎保存了30个空行,我的联接表中也有30个(非空)行,因此这里可能存在关联.

It seems to save 30 null rows, I also have 30 (not null) rows in my join table so there might be a correlation here.

我得到的JSON看起来像这样:"categories":[{"category_id":1},{"category_id":4}]

The JSON that I get looks like this: "categories":[{"category_id":1},{"category_id":4}]

我有两个自NSManagedObject继承的自定义模型对象.

I have two custom model objects that inherits from 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示例):

I fetch the data like this (much like the Twitter RestKit example):

- (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];
}

有什么主意我做错了吗?

Any ideas what I'm doing wrong?

注意: 当有15条新闻时,它似乎还可以将30行保存到联接表中,我不知道这是否是联接表的正常行为.

Note: It also seems to save 30 rows to the join table when there are 15 News, I don't know if this is the normal behavior for join tables.

更新:它还会在连接表中映射错误的类别ID(即类别66,为空行).

Update: It also maps the the wrong category id's in the join table (ie. category 66, a null row).

更新2: JSON获取我的类别{"categories":{[...],"id":1,[...]}

update 2: JSON get request for my Category {"categories":{[...], "id":1, [...]}

推荐答案

此行是错误的:

[categoryMapping mapKeyPath:@"id" toAttribute:@"categoryId"];

应该是

[categoryMapping mapKeyPath:@"category_id" toAttribute:@"categoryId"];

您拥有的对象映射将映射两种JSON格式.

The object mapping you have will map two JSON formats.

1)当您拥有资源路径@"[JSON的路径]"

1) {"categories":[{"id":7},{"id":12}]} when you have the resource path @"[path-to-JSON]"

2)当您拥有资源路径@"[JSON的路径]"时,{新闻":[{"id":5,类别":[{"id":7}]}]}}

2) {"news":[{"id":5, "categories":[{"id":7}]}]} when you have the resource path @"[path-to-JSON]"

(我想这些实际路径是不同的)

(I presume those actual paths are different by the way)

其他任何内容(例如,您在问题顶部发布的JSON)均无效.您看到的问题是因为在JSON中找不到主键属性,因此当对象保存到核心数据时,将创建一个没有主键的新实体.

Anything else, such as the JSON you have posted at the top of your question, will not work. The problem you are seeing is because the primary key attribute can not be found in your JSON, so when the object is saved to core data a new entity is created with no primary key.

这篇关于RestKit多对多关系在联接表中保存新行,在主表中保存空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆