1

JSON 文件如下所示:

{
  "status":"success",
  "result":[
     {
        "id":"1",
        "name":"Wigwam Motel",
        "latitude":34.106785,
        "longitude":-117.349399,
        "tag1_id":123,
        "tag2_id":2,
        "tag3_id":3,
        "address1":"Some Place Str.",
        "address2":"123",
        "city":"San Francisco",
        "state":"CA",
        "zip":94103,
        "total_photos":"4",
        "total_checkins":"5",
        "status":"approved",
        "hero_checkin": 
    {
        "id":12345,
        "type":"photo",
        "comment":"Lorm Ipsum Etc.",
        "created":"2012-04-02",
           "photo_name":"http://crashtestdata.sandbox.whoisstudio.com/crashtestdata.sandbox.whoisstudio.com//images/thumbs/abbotkinneymural.jpg",
        "attraction":
            {
            "id":"12345",
            "name":"Attraction 1"
            },
        "user": 
            {
            "id":"1234",
            "username":"john.smith",
            "facebook_uid":"false",
            "twitter_username":"false",
            "email":"jhon.smith@example.org",
            "first_name":"John",
            "last_name":"Smith",
            "avatar":"http://crashtestdata.sandbox.whoisstudio.com/crashtestdata.sandbox.whoisstudio.com//images/thumbs/abbotkinneymural.jpg"
            }
    }
     },    

映射如下所示:

 RKManagedObjectMapping* checkInMapping = [RKManagedObjectMapping mappingForClass:[CheckIn class]
                                                            inManagedObjectStore:           [RKObjectManager sharedManager].objectStore];

checkInMapping.primaryKeyAttribute = @"checkInId";

[checkInMapping mapKeyPathsToAttributes:
        @"id",                  @"checkInId",
        @"comment",             @"comment", 
        @"facebook_share_id",   @"facebookShareId", 
        @"photo_name",          @"photoName",
        @"twitter_share_id",    @"twitterShareId",
        @"account_id",          @"accountId",
        @"attraction_id",       @"attractionId",
        @"created",             @"createdTimestamp",
        @"updated",             @"updatedTimestamp",
        @"type",                @"type",
 nil];

RKManagedObjectMapping *mapping = [RKManagedObjectMapping mappingForClass:[Attraction class]
                                                 inManagedObjectStore:[RKObjectManager sharedManager].objectStore];

 mapping.primaryKeyAttribute = @"attractionId";

[mapping mapKeyPath:@"hero_checkin" toRelationship:@"heroCheckIn" withMapping:checkInMapping];

[mapping mapKeyPathsToAttributes:
        @"id",          @"attractionId",
        @"name",            @"name",
        @"description",     @"description",
        @"longitude",       @"longitude",
        @"latitude",        @"latitude",
        @"tag1_id",         @"tag1Id",
        @"tag2_id",         @"tag2Id",
        @"tag3_id",         @"tag3Id",
        @"city",            @"city",
        @"state",           @"state",
        @"zip",             @"zip",
        @"total_photos",    @"totalPhotos",
        @"total_checkins",  @"totalCheckIns",
        @"status",          @"status",
        @"founder_user_id", @"founderId",
        @"founder_user",    @"founderUser",
        @"region_id",       @"regionId",
        @"region",          @"region",
        @"region_checkin_id",@"regionCheckInId",
        @"region_checkin",  @"regionCheckIn",
 nil];


// Just another try to map the relationship it didn't work
//    [mapping mapRelationship:@"heroCheckIn" withMapping:checkInMapping];
//    [[RKObjectManager sharedManager].mappingProvider setMapping:checkInMapping 
//    forKeyPath:@"response.hero_checkin"];
//    [checkInMapping hasOne:@"heroCheckInId" withMapping:mapping];
//    [checkInMapping connectRelationship:@"heroCheckIn" 
//   withObjectForPrimaryKeyAttribute:@"heroCheckId"];

[[RKObjectManager sharedManager].mappingProvider setObjectMapping:mapping forKeyPath:@"result"];

这就是我得到的:

<Attraction: 0xf19d080> (entity: Attraction; id: 0xf198060 <x-coredata://4C997AF5-D806-46C8-AD32-80220D2E9D97/Attraction/p7> ; data: {
address1 = nil;
address2 = nil;
attractionId = 1;
city = "San Francisco";
founder = nil;
founderAccountId = 0;
heroCheckIn = nil;
heroCheckInId = 0;
latitude = "34.106785";
longitude = "-117.349399";
name = "Wigwam Motel";
region = nil;
regionId = 0;
state = CA;
status = approved;
tag1Id = 123;
tag2Id = 2;
tag3Id = 3;
totalCheckIns = 5;
totalPhotos = 4;
zip = 94103;

正如你所看到的 heroCheckIn 为零,我一直在阅读很多论坛,在这里发帖和 RestKit 文档,但似乎我遗漏了一些东西,所以欢迎任何帮助,或者至少是朝着正确的方向推动. 顺便说一句,我有 CoreData 模型,其中包含关系和所有字段,相应地具有 Attraction 和 CheckIn 中的属性。

提前致谢

4

0 回答 0