0

我很难将 JSON 映射到我的 RestKit 应用程序中。我也想将它保存在核心数据中。因此,这里有两个图像链接。第一张图片,我的 JSON 的样子。

图像1

第二张图片是我的核心数据库的样子。

图像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;

然后你必须为你的每个数据类和它拥有的关系设置映射。我将尝试在这里举一个我用过的例子。

    /** 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 回答