1

我有一个非常奇怪的错误,我发现它很难调试。我有一个一对多的关系,没有为获取的一个对象设置。我正在使用 Restkit 和核心数据。这是我下载的json:

{ "chapterExerciseFiles" : [  ],
    "chapterExerciseUrls" : [ { "description" : "",
          "id" : 43,
          "name" : "Webbˆvning",
          "title" : "Webbˆvningar",
          "type" : "WEB_EXERCISE_LINK",
          "url" : "http://..."
        } ],
    "fromPage" : 1,
    "id" : 43,
    "index" : 1,
    "isbn" : "9789127434400",
    "pageExercises" : [  ],
    "title" : "Echo 5 Short Stories",
    "toPage" : 5
  } //...

该模型如下所示:

模型截图

映射:

+ (RKEntityMapping *)chapterExerciseMapping
{
    RKEntityMapping *chapterExerciseMapping = [RKEntityMapping mappingForEntityForName:@"ChapterExercise" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];

    [chapterExerciseMapping addAttributeMappingsFromArray:@[
     @"index",
     @"title"
     ]];
    [chapterExerciseMapping addAttributeMappingsFromDictionary:@{
     @"id": @"chapterExerciseID",
     @"toPage": @"toPageNumber",
     @"fromPage": @"fromPageNumber"
     }];

    [chapterExerciseMapping addRelationshipMappingWithSourceKeyPath:@"chapterExerciseFiles" mapping:[IBResponseMapping exerciseFileMapping]];
    [chapterExerciseMapping addRelationshipMappingWithSourceKeyPath:@"chapterExerciseUrls" mapping:[IBResponseMapping exerciseUrlMapping]];
    [chapterExerciseMapping addRelationshipMappingWithSourceKeyPath:@"pageExercises" mapping:[IBResponseMapping pageExerciseMapping]];

    return chapterExerciseMapping;
}

+ (RKEntityMapping *)exerciseUrlMapping
{
    RKEntityMapping *exerciseUrlMapping = [RKEntityMapping mappingForEntityForName:@"ExerciseUrl" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];
    [exerciseUrlMapping addAttributeMappingsFromArray:@[
     @"type",
     @"url"
     ]];
    [exerciseUrlMapping addAttributeMappingsFromDictionary:@{
     @"description": @"urlDescription",
     @"id" : @"urlID",
     @"name" : @"title",
     @"title" : @"sectionTitle"
     }];

    return exerciseUrlMapping;
}

奇怪的是“一些”chapterExercisesUrls确实被设置了,但不是第一个。

记录 Restkit 在获取时显示chapterExerciseUrls为空:

<IBChapterExercise: 0x16f2f6b0> (entity: ChapterExercise; id: 0x16f2f6f0 <x-coredata:///ChapterExercise/t529D851C-D8EF-4669-B197-9FF24B5CAE862267> ; data: {
    bookData = nil;
    chapterExerciseFiles =     (
    );
    chapterExerciseID = 43;
    chapterExerciseUrls =     (
    );
    fromPageNumber = 1;
    index = 1;
    pageExercises =     (
    );
    title = "Echo 5 Short Stories";
    toPageNumber = 5;
}),

这是另一个获得正确映射的对象:

<IBChapterExercise: 0x14c73920> (entity: ChapterExercise; id: 0x14c8bae0 <x-coredata:///ChapterExercise/t529D851C-D8EF-4669-B197-9FF24B5CAE862295> ; data: {
    bookData = nil;
    chapterExerciseFiles =     (
    );
    chapterExerciseID = 45;
    chapterExerciseUrls =     (
        "0x14ca2f60 <x-coredata:///ExerciseUrl/t529D851C-D8EF-4669-B197-9FF24B5CAE862296>"
    );
    fromPageNumber = 16;
    index = 3;
    pageExercises =     (
        "0x14cbea40 <x-coredata:///PageExercise/t529D851C-D8EF-4669-B197-9FF24B5CAE862300>",
        "0x14c98af0 <x-coredata:///PageExercise/t529D851C-D8EF-4669-B197-9FF24B5CAE862297>",
        "0x16f30000 <x-coredata:///PageExercise/t529D851C-D8EF-4669-B197-9FF24B5CAE862305>"
    );
    title = "Right up to the Limits";
    toPageNumber = 25;
}),

在 OSX 和 iOS 上都可以看到该问题。关于如何进一步调试的任何想法?

4

1 回答 1

0

您需要向映射添加唯一标识符信息,以允许 RestKit 查找现有项目并防止创建重复项。

于 2013-10-03T09:42:10.127 回答