RKObjectMapping和RKEntityMapping之间的关系 [英] Relationship between RKObjectMapping and RKEntityMapping
问题描述
说我有 JSON
:
{
"hits": 4056,
"books": [
{
"name": "Book 1"
},
{
"name": "Book 2"
},
{
"name": "Book 3"
}
]
}
我想将此响应映射到 BooksResponse
object: p>
I would like to map this response to BooksResponse
object:
@interface BooksResponse
@property (nonatomic, assign) NSInteger hits;
@property (nonatomic, strong) NSArray* books;
@end
其中books属性是 CoreData
预订对象。我使用 RKObjectEntity
映射 BooksResponse
,如下所示:
Where books property is an array of CoreData
Book objects. I'm using RKObjectEntity
to map BooksResponse
like this:
RKObjectMapping* booksResponseMapping = [RKObjectMapping mappingForClass:[BooksResponse class]];
[booksResponseMapping addAttributeMappingsFromDictionary:@{
@"hits" : @"hits"
}];
[booksResponseMapping addPropertyMapping:[RKRelationshipMapping
relationshipMappingFromKeyPath:@"books"
toKeyPath:@"books"
withMapping:[self booksMapping]]];
并且booksMapping实现如下:
And booksMapping is implemented like this:
RKEntityMapping* bookMapping = [RKEntityMapping mappingForEntityForName:@"Book"
inManagedObjectStore:[self managedObjectStore]];
[bookMapping addAttributeMappingsFromDictionary:@{
@"name" : @"name"
}];
但是每当 RestKit
是崩溃:
CoreData: error: Failed to call designated initializer on NSManagedObject class 'Book'
在执行期间检查 ManagedObjectStore
存在于 RKObjectManager
实例。应用程序在 RKMappingOperation:355
上挂起
I checked, during execution ManagedObjectStore
exists on RKObjectManager
instance. Application hangs in class RKMappingOperation:355
on line
id currentValue = [self.destinationObject valueForKeyPath:keyPath];
有一种方法可以使plain objectiveC对象和 NSManagedObject
,以便 CoreData
?
Is there a way one can make relationship between plain objectiveC object and NSManagedObject
so that only part of JSON
response is persisted in CoreData
?
推荐答案
这是 RKObjectRequestOperation
和 RKManagedObjectRequestOperation
。因为你只有一个响应描述符,它匹配一个对象(不是一个托管对象),RestKit将使用 RKObjectRequestOperation
。当这样做时,映射期间不能使用托管对象库,因此不能创建托管对象。
This is the difference between RKObjectRequestOperation
and RKManagedObjectRequestOperation
. Because you only have 1 response descriptor and it matches an object (not a managed object), RestKit will use RKObjectRequestOperation
. When it does this, the managed object store is not available during the mapping so no managed objects can be created.
解决方法:
而不是使用1响应描述符,使用2.第一个只是创建 BooksResponse
并将 hits
。第二个创建 Book
并映射名称
。
Instead of using 1 response descriptor, use 2. The first just creates the BooksResponse
and maps the hits
. The second creates the Book
s and maps the name
.
操作完成后,您将获得一个映射响应,其字典包含两个键:null和books。 null键保存 BooksResponse
实例,books键保存 Book
实例。您现在可以更新 BooksResponse
实例来填充关系。
Once the operation is complete, you will be provided with a mapping response whose dictionary contains 2 keys: null and "books". The null key holds the BooksResponse
instance and the "books" key holds the Book
instances. You can now update the BooksResponse
instance to populate the relationship.
请注意,映射响应中的键对应于在2个响应描述符上指定的关键路径。
Note, the keys in the mapping response correspond to the key paths specified on the 2 response descriptors.
这篇关于RKObjectMapping和RKEntityMapping之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!