0

我使用 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, [...]} 的请求

4

1 回答 1

0

这一行是错误的:

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

它应该是

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

编辑:您拥有的对象映射将映射两种 JSON 格式。

1) {"categories":[{"id":7},{"id":12}]} 当你有资源路径 @"[path-to-JSON]"

2) {"news":[{"id":5, "categories":[{"id":7}]}]} 当你有资源路径 @"[path-to-JSON]"

(顺便说一下,我认为这些实际路径是不同的)

其他任何内容,例如您在问题顶部发布的 JSON,都将不起作用。您看到的问题是因为在您的 JSON 中找不到主键属性,所以当对象保存到核心数据时,会创建一个没有主键的新实体。

于 2012-06-27T14:58:32.583 回答