RestKit创建多个到多个关系

时间:2013-07-01 19:55:51

标签: ios objective-c restkit

我正在尝试解析json响应以用于核心数据模型。这是来自json的样本:

  {
      "@context": "TVSchedule",
      "ReturnCode": "0",
      "ReturnMessage": "Successful request",
      "Channel": [
        {
          "ChannelId": "http%…..0",
          "Program": [
            {
              "@programId": "http……..",
              "Title": "Divorce Court",
              "ProgramLogo": "http://00_180x101.png",
              "ProgramLogos": [
                {
                  "@size": "small",
                  "#text": "http://.png"
                },
                {
                  "@size": "large",
                  "#text": "191.png"
                }
              ],
              "ProgramDetailsURL": "http:9",
              "PublishedStartTime": "2013-07-01T19:00:00",
              "PublishedEndTime": "2013-07-01T19:30:00",
              "Duration": "00:00:30:00",
              "RatingInfo": {
                "@system": "MPAA",
                "@code": "TV-PG",
                "@age": "10",
                "Title": "Not recommended for children under 10 years",
                "Logo": "http://"
              },
              "ShortDescription": "She says she cannot trust him .",
              "Year": "2013",
              "Genres": [
                "Series",
                "Reality",
                "Public Affairs",
                "News",
                "Episodic"
              ]
            },

我需要在CoreData中得到这个,我有一个名为Channel的实体,它将使channelId与一个名为program的实体保持一对多关系,该实体将保留该级别上的属性列表,并且必须 - 与实体的多种关系ProgramLogo(来自json文件中的“ProgramLogos”)类型(类型字符串数组将被添加为多个仅包含一个字符串属性的Genre实体),RatingsInfo实体(来自相应的一对一关系) (),RatingsInfo并未出现在所有程序中......

这是我使用的RestKit代码:

 RKEntityMapping *channelMapping = [RKEntityMapping mappingForEntityForName:kCDChannelEntity inManagedObjectStore:managedObjectStore];
    channelMapping.identificationAttributes = @[ kCDChannelId ];

    [channelMapping addAttributeMappingsFromDictionary:@{
                                       kJsonChannelId : kCDChannelId
     }];



    RKEntityMapping *programMapping = [RKEntityMapping mappingForEntityForName:kCDProgramEntity inManagedObjectStore:managedObjectStore];


    [programMapping addAttributeMappingsFromDictionary:@{
                                       @"@programId" : kCDProgramId ,
                                     @"ProgramLogo" : kCDProgramLogo,
                              @"ProgramDetailsURL" : kCDProgramDetailsUrl,
                                       @"Duration"  : kCDProgramDuration,
                              @"PublishedStartTime" : kCDProgramStartTime,
                                @"PublishedEndTime" : kCDProgramEndTime,
                                           @"Title" : kCDProgramTitle,
                                            @"Year" : kCDProgramYear,
                                @"ShortDescription" : kCDProgramShortDescription
     }];

    [RKObjectMapping addDefaultDateFormatterForString:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'" inTimeZone:nil];

    [channelMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Program" toKeyPath:kCDChannelHasProgramsRel withMapping:programMapping]];// ??


    RKEntityMapping *parentalRatingMapping = [RKEntityMapping mappingForEntityForName:kCDParentalRatingEntity inManagedObjectStore:managedObjectStore];
    [parentalRatingMapping addAttributeMappingsFromDictionary:@{
                                           @"Program.RatingsInfo.@age": kCDParentalRatingAge,
                                         @"Program.RatingsInfo.@code" : kCDParentalRatingCode,
                                         @"Program.RatingsInfo.Logo" : kCDParentalRatingLogo,
                                       @"Program.RatingsInfo.@system" : kCDParentalRatingSystem,
                                        @"Program.RatingsInfo.Title" : kCDParentalRatingTitle}];

    [programMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Program.RatingsInfo" toKeyPath:kCDProgramHasParentalRatingRel withMapping:parentalRatingMapping]];// ??



    RKEntityMapping *genresMapping = [RKEntityMapping mappingForEntityForName:kCDGenreEntity inManagedObjectStore:managedObjectStore];
    [genresMapping addAttributeMappingsFromDictionary:@{
     @"Program.Genres": kCDGenreName
     }];

    [programMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"Program.Genres" toKeyPath:kCDProgramHasGenres withMapping:genresMapping]];


    RKEntityMapping *logoMapping = [RKEntityMapping mappingForEntityForName:kCDProgramLogoEntity inManagedObjectStore:managedObjectStore];
    [logoMapping addAttributeMappingsFromDictionary:@{
     @"Program.ProgramLogos.@size" : kCDLogoSize,
     @"Program.ProgramLogos.#text" : kCDLogoText
     }];

好的,现在有两个问题: 1)如何解析两个非KVO数组(类型,不同实体中的每个字符串,以及ProgramLogos(实体中的每个字典)

2)RestKit不解析programId(键“@programId”)为什么?键中的“@”是否会停止解析?

我得到了

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFDictionary 0x958f980> valueForUndefinedKey:]: this class is not key value coding-compliant for the key programId.'

1 个答案:

答案 0 :(得分:0)

看起来你的channelResponseDescriptor应该有一个@“频道”的keyPath。由于所有常数,很难判断是否有其他错误。似乎只有顶级描述符需要更多关于它应该应用的信息。


通常,您创建的所有映射都是相互关联的。基于JSON的结构,您很可能不需要这么多响应描述符,只需要通道的顶级响应描述符,然后使用所有关系来使用关系向下导航结构。


例如,您有JSON:

  "Channel": [
    {
      "ChannelId": "http%…..0",
      "Program": [
        {...

您可以在通道和程序实体之间建立关系以及映射之间的对应关系,以便在映射通道时,它将导航到JSON的程序部分,映射程序然后连接对象。你似乎有这个:

[channelMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:kJsonProgram toKeyPath:kCDChannelHasProgramsRel withMapping:programMapping]];

(代码中有拼写错误,因为它应该应用于channelMapping,而不是programMapping

因此programResponseDescriptor可能不是必需的,只会导致您的映射结果包含重复的项目。