Restkit嵌套JSON mapRelationship不起作用

时间:2012-12-11 13:20:44

标签: ios restkit

我发现了我的错误,所以请滚动到更新,看看我做错了什么以及我在RestKit上遇到的其他问题:)

我遇到以下JSON问题:

[     {         “id”:1,         “latTitle”:“Aale”,         “latExamplesString”:“”,         “previewImg”:1,         “lastModified”:null,         “localeCategory”:{             “id”:1,             “catID”:1,             “langCode”:“de”,             “localeTitle”:“Aale”,             “localeExampleString”:“Gefleckter Schlangenaal,Ringelschlangenaal”         },         “elements”:null     } ]

如您所见,localeCategory是一个嵌套的属性列表,因此我已完成以下操作来映射它:

        //creating Mapping for localeCategory
    RKManagedObjectMapping *localeMapping = [RKManagedObjectMapping mappingForClass:[LocaleCategory class] inManagedObjectStore:objectStore];
    localeMapping.primaryKeyAttribute = @"lcatId";
    [localeMapping mapKeyPath:@"id" toAttribute:@"lcatId"];
    [localeMapping mapKeyPath:@"catId" toAttribute:@"catId"];
    [localeMapping mapKeyPath:@"langCode" toAttribute:@"langCode"];
    [localeMapping mapKeyPath:@"localeTitle" toAttribute:@"localeTitle"];
    [localeMapping mapKeyPath:@"localeExampleString" toAttribute:@"localeExampleString"];

    //creating Mapping for Category
    RKManagedObjectMapping *categoryMapping = [RKManagedObjectMapping mappingForClass:[Category class] inManagedObjectStore:objectStore];
    categoryMapping.primaryKeyAttribute = @"catId";
    [categoryMapping mapKeyPath:@"id" toAttribute:@"catId"];
    [categoryMapping mapKeyPath:@"latTitle" toAttribute:@"latTitle"];
    [categoryMapping mapKeyPath:@"latExamplesString" toAttribute:@"latExamplesString"];
    [categoryMapping mapKeyPath:@"previewImg" toAttribute:@"previewImg"];
    [categoryMapping mapKeyPath:@"lastModified" toAttribute:@"lastModified"];
    [categoryMapping mapKeyPath:@"elements" toAttribute:@"elements"];

    [categoryMapping mapKeyPath:@"localeCategory" toRelationship:@"localeCategory" withMapping:localeMapping];

    //[categoryMapping mapRelationship:@"localeCategory" withMapping:localeMapping];

    [self setObjectMapping:categoryMapping forResourcePathPattern:@"data/de/category"];

我的问题如下:

当我想将JSON文件中的数据列入我的TableView时,localeCategory的所有属性都为null,例如我想设置一个单元格的标题:

Category *cat = [data objectAtIndex:absoluteRow];
    LocaleCategory *loccat = [cat localeCategory];
    NSLog(@"gotCategory, check localeCat: %@", loccat.localeTitle);
cell.catTitle.text = cat.localeCategory.localeTitle;

我已经查看了RestKit的每个SampleCode以及堆栈溢出的相关主题,但我找不到我的错误!

提前感谢您的帮助:)

欢呼声, 最大

更新:使用FetchRequest时我也遇到了麻烦:

NSLog(@"fetchDataFromDataStore");
NSFetchRequest *request = [[[RKObjectManager sharedManager] mappingProvider] fetchRequestForResourcePath:self.resourcePath];
NSLog(@"request");
self.data = [Category objectsWithFetchRequest:request];

最终出现此错误:

  

'executeFetchRequest:error:获取请求必须有实体。

我也想知道的是我不能用这个:

Category *firstCategory = [Category objectWithPrimaryKeyValue:[NSNumber numberWithInt:1]];

它说选择器没有已知的类方法......

我希望有人帮助我找到我的错误,看起来我在使用Reskit和CoreData进行设置的一般过程中犯了一个错误!

我期待着你的帮助,非常感谢!

谢谢rdelmar!我已经修好了,但我认为这不是问题。我第一次使用没有CoreData的RestKit,所以我在LocaleCategory类中使用@synthesize而不是@dynamic :)我已经完成了所有其他类的切换到动态,所以是的,这是一个愚蠢的错误! :)

无论如何我仍然遇到FetchRequest的问题!如果有人有想法,我非常感谢!

最高

1 个答案:

答案 0 :(得分:0)

好的,我现在可以使用RestKit了:)

以下是我的问题的解决方案:

  

'executeFetchRequest:error:获取请求必须有一个实体。'

        [self setObjectMapping:categoryMapping forResourcePathPattern:@"category" withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {
        NSFetchRequest *fetchRequest = [Category fetchRequest];
        fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"latTitle" ascending:YES]];
        return fetchRequest;
    }];

电话:

 NSFetchRequest *request = [[[RKObjectManager sharedManager] mappingProvider] fetchRequestForResourcePath:@"category"];
self.data = [Category objectsWithFetchRequest:request];

当你知道如何:)时很容易。