0

JSON を RestKit アプリケーションにマッピングするのに苦労しています。core-dataにも保存したいです。したがって、ここに画像への2つのリンクがあります。最初の画像、私の JSON がどのように見えるか。

画像1

2 番目の画像は、コア データベースがどのように見えるかです。

画像2

最初に、コアデータレイヤーなしでアプリケーションを作成しました。したがって、データを読み取って操作するだけです。その時点で、すべてが機能しました。しかし、コアデータを使用すると、このようなマッピング エラーが発生しました。

ログ

 2013-01-10 10:46:02.663 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Person'
2013-01-10 10:46:02.664 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Function'
2013-01-10 10:46:02.664 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Department'
2013-01-10 10:46:02.665 Offitel2[27978:6813] D restkit.object_mapping:RKMappingOperation.m:635 Did not find mappable relationship value keyPath 'Company'

2013-01-10 10:46:02.672 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'user' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
2013-01-10 10:46:02.673 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'function' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
2013-01-10 10:46:02.674 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'department' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.
2013-01-10 10:46:02.676 Offitel2[27978:164f] W restkit.core_data:RKManagedObjectRequestOperation.m:555 Caught undefined key exception for keyPath 'company' in mapping result: This likely indicates an ambiguous keyPath is used across response descriptor or dynamic mappings.

これは、コードで関係マッピングを行う方法です。

 RKRelationshipMapping* relationUserMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Person"toKeyPath:@"user"withMapping:personMapping];
    RKRelationshipMapping* relationFunctionMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Function"toKeyPath:@"function"withMapping:functionMapping];
    RKRelationshipMapping* relationDepartmentMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Department"toKeyPath:@"department"withMapping:departmentMapping];
    RKRelationshipMapping* relationCompanyMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Company"toKeyPath:@"company"withMapping:companyMapping];
      NSLog(@"till here7");
    [dataMapping addPropertyMapping:relationUserMapping];
    [dataMapping addPropertyMapping:relationFunctionMapping];
    [dataMapping addPropertyMapping:relationDepartmentMapping];
    [dataMapping addPropertyMapping:relationCompanyMapping];

誰でもこれで私を助けてくれませんか。コアデータのマッピングで何日も苦労しています。

敬具

4

1 に答える 1

1

これには RKManagedObjectMapping を使用してみてください。RKManagedObjectMapping オブジェクトを次のように構成します。

RKURL *baseURL = [RKURL URLWithBaseURLString:serverUrl];

    RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];

    objectManager.client.baseURL = baseURL;

    objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;

    objectManager = [RKObjectManager objectManagerWithBaseURL:baseURL];

    RKManagedObjectStore *objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:DB_NAME];

    objectManager.objectStore = objectStore;

次に、各データ クラスとその関係のマッピングを設定する必要があります。ここでは、私が使用した例を 1 つだけ紹介します。

    /** specify the base mapping of reservation class */
    RKManagedObjectMapping *getReservationMapping = [RKManagedObjectMapping mappingForClass:[Reservation class] inManagedObjectStore:objectManager.objectStore];
    /** generate mapping for Reservation class */
    [self setReservationMapping:getReservationMapping];
    getReservationMapping.primaryKeyAttribute=@"reservationId";
    [objectManager.mappingProvider setMapping:getReservationMapping forKeyPath:@""];


    /** specify the base mapping of ReservationsType class */
    RKManagedObjectMapping *resTypeMapping = [RKManagedObjectMapping mappingForClass:[ReservationsType class] inManagedObjectStore:objectManager.objectStore];
    /** generate mapping for ReservationsType class */
    [self setReservationsTypeMapping:resTypeMapping];
    resTypeMapping.primaryKeyAttribute=@"reservationTypeId";
    [getReservationMapping mapRelationship:@"reservationsType" withMapping:resTypeMapping];
    [objectManager.mappingProvider setMapping:resTypeMapping forKeyPath:@".reservationsType"];

私のsetReservationMappingは次のとおりです

    - (void)setReservationMapping:(RKManagedObjectMapping *)object
    {
     [object mapKeyPathsToAttributes:
       @"id", @"reservationId",
       @"comment", @"comment",
       @"date", @"date",
       @"email", @"email",nil];
    }

これが私のJSONサンプルです。私の予約ノードには空白の識別子がないため、keyPath:@"" を指定したことに注意してください。

     [{
    id: 5644,
    comment: "",
    email: "danish@abc.com",
    firstName: "Danny",
    reservationsType: 
{
    id: 1,
    name: "Reservation",
    }
    }]

これがお役に立てば幸いです。

于 2013-01-17T05:11:24.087 に答える