我有一个名为 Contact
的实体(在 Core Data
上),每个联系人都有一个 Neighborhood
、User
以及与其相关的 ContactOrigin
实体。
当我从服务器检索 JSON 数据时,我得到如下内容:
{
"contact" : [
{
"prospect" : {
"pkProspect" : 21,
"name" : "Test Contact",
"email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="126677616652767d7f737b7c3c717d7f" rel="noreferrer noopener nofollow">[email protected]</a>",
"phone" : "5555555555",
"address" : "Test Avenue",
"media" : {
"pkMedia" : 3,
"name" : "Referência Entrevista You Move"
},
"neighborhood" : {
"pkNeighborhood" : 15857,
"name" : "Donwtown"
}
},
"user" : {
"firstName" : "John",
"pkPerson" : 5,
"lastName" : "Doe"
}
}
]
}
正如您所看到的,我有一个联系人数组,每个联系人都有一个用户节点、一个邻居节点和一个媒体节点(我将其用作数据库的源节点)。 RestKit 似乎用以下代码以正确的方式映射了这一点:
// Getting the object manager.
RKObjectManager *objectManager = [RKObjectManager sharedManager];
// Relationship mappings.
RKManagedObjectMapping *neighborhoodMapping = [RKManagedObjectMapping mappingForClass:[Neighborhood class] inManagedObjectStore:objectManager.objectStore];
neighborhoodMapping.primaryKeyAttribute = @"identifier";
[neighborhoodMapping mapKeyPath:@"pkNeighborhood" toAttribute:@"identifier"];
RKManagedObjectMapping *originMapping = [RKManagedObjectMapping mappingForClass:[ContactOrigin class] inManagedObjectStore:objectManager.objectStore];
originMapping.primaryKeyAttribute = @"identifier";
[originMapping mapKeyPath:@"pkMedia" toAttribute:@"identifier"];
RKManagedObjectMapping *userMapping = [RKManagedObjectMapping mappingForClass:[User class] inManagedObjectStore:objectManager.objectStore];
userMapping.primaryKeyAttribute = @"identifier";
[userMapping mapKeyPath:@"pkPerson" toAttribute:@"identifier"];
// Creating the contact mapping for the request.
RKManagedObjectMapping *contactMapping = [RKManagedObjectMapping mappingForClass:[Contact class] inManagedObjectStore:objectManager.objectStore];
contactMapping.primaryKeyAttribute = kContactFieldNameId;
[contactMapping mapKeyPath:@"prospect.neighborhood" toRelationship:@"addressNeighborhood" withMapping:neighborhoodMapping];
[contactMapping mapKeyPath:@"prospect.media" toRelationship:@"origin" withMapping:originMapping];
[contactMapping mapKeyPath:@"user" toRelationship:@"user" withMapping:userMapping];
[contactMapping connectRelationship:@"addressNeighborhood" withObjectForPrimaryKeyAttribute:@"identifier"];
[contactMapping connectRelationship:@"origin" withObjectForPrimaryKeyAttribute:@"identifier"];
[contactMapping connectRelationship:@"user" withObjectForPrimaryKeyAttribute:@"identifier"];
[contactMapping mapKeyPath:@"prospect.pkProspect" toAttribute:@"identifier"];
[contactMapping mapKeyPath:@"prospect.name" toAttribute:@"name"];
[contactMapping mapKeyPath:@"prospect.email" toAttribute:@"email"];
[contactMapping mapKeyPath:@"prospect.phone" toAttribute:@"phone"];
[contactMapping mapKeyPath:@"prospect.address" toAttribute:@"address"];
// Setting the result mapping.
[objectManager.mappingProvider setObjectMapping:contactMapping forKeyPath:@"contact"];
// Loading data.
[objectManager loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader) {
loader.delegate = self;
loader.userData = delegate;
}];
映射完成后,我收到以下消息(我仅显示 User
错误):
2012-07-06 18:20:23.016 Contact Manager[8254:10c03] E restkit.core_data:RKManagedObjectStore.m:250 Core Data Save Error
NSLocalizedDescription: The operation couldn’t be completed. (Cocoa error 1570.)
NSValidationErrorKey: user
NSValidationErrorPredicate: (null)
NSValidationErrorObject:
<Contact: 0x917dff0> (entity: Contact; id: 0x917e070 <x-coredata:///Contact/t9D9EE4AE-142A-45EC-88C1-E97833729D0C2> ; data: {
address = "Test Avenue";
addressNeighborhood = "0x918b5b0 <x-coredata://6E5AB6F3-AFF4-460E-A37D-285A105178F0/Neighborhood/p21>";
email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4c0d1c7c0f4d0dbd9d5ddda9ad7dbd9" rel="noreferrer noopener nofollow">[email protected]</a>";
identifier = 21;
name = "Test Contact";
origin = nil;
phone = 5555555555;
user = nil;
})
2012-07-06 18:20:23.024 Contact Manager[8254:10c03] E restkit.core_data:RKManagedObjectLoader.m:167 Failed to save managed object context after mapping completed: The operation couldn’t be completed. (Cocoa error 1560.)
如您所见,origin
和 user
均为 null,当我尝试将其保存在 Core Data
上时,这会导致错误因为这些字段是必需的,但是如果我们查看 RestKit 的日志,我们会看到这些对象已被正确解析,并且实际上它们存在于数据库中。
2012-07-06 18:20:22.938 Contact Manager[8254:10c03] D restkit.object_mapping:RKObjectMappingOperation.m:636 Starting mapping operation...
2012-07-06 18:20:22.940 Contact Manager[8254:10c03] T restkit.object_mapping:RKObjectMappingOperation.m:637 Performing mapping operation: RKObjectMappingOperation for 'User' object. Mapping values from object {
firstName = John;
lastName = Doe;
pkPerson = 5;
} to object <User: 0x5ca9d60> (entity: User; id: 0xc5aa7a0 <x-coredata://6E5AB6F3-AFF4-460E-A37D-285A105178F0/User/p1> ; data: {
contacts = "<relationship fault: 0xc5ab580 'contacts'>";
email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a0cacfc8cee0c4cfcdc1c9ce8ec3cfcd" rel="noreferrer noopener nofollow">[email protected]</a>";
identifier = 5;
ip = "0.0.0.0";
isLoggedIn = 1;
language = "en";
name = "John Doe";
password = 4badaee57fed5610012a296273158f5f;
}) with object mapping <RKManagedObjectMapping:0x5ca78b0 objectClass=User keyPath mappings => (
"RKObjectKeyPathMapping: pkPerson => identifier"
)>
2012-07-06 18:20:22.942 Contact Manager[8254:10c03] T restkit.object_mapping:RKObjectMappingOperation.m:330 Mapping attribute value keyPath 'pkPerson' to 'identifier'
2012-07-06 18:20:22.943 Contact Manager[8254:10c03] D restkit.object_mapping:RKObjectPropertyInspector.m:107 Cached property names and types for Class 'User': {
accessibilityHint = NSString;
accessibilityLabel = NSString;
accessibilityLanguage = NSString;
accessibilityValue = NSString;
contacts = NSSet;
email = NSString;
identifier = NSNumber;
ip = NSString;
isLoggedIn = NSNumber;
language = NSString;
name = NSString;
password = NSString;
}
2012-07-06 18:20:22.944 Contact Manager[8254:10c03] T restkit.object_mapping:RKObjectMappingOperation.m:347 Skipped mapping of attribute value from keyPath 'pkPerson to keyPath 'identifier' -- value is unchanged (5)
2012-07-06 18:20:22.946 Contact Manager[8254:10c03] D restkit.object_mapping:RKObjectMappingOperation.m:643 Finished mapping operation successfully...
2012-07-06 18:20:22.966 Contact Manager[8254:10c03] T restkit.object_mapping:RKObjectMappingOperation.m:597 Mapped relationship object from keyPath 'user' to 'user'. Value: <User: 0x5ca9d60> (entity: User; id: 0xc5aa7a0 <x-coredata://6E5AB6F3-AFF4-460E-A37D-285A105178F0/User/p1> ; data: {
contacts = "<relationship fault: 0xc5ab580 'contacts'>";
email = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcb6b3b4b29cb8b3b1bdb5b2f2bfb3b1" rel="noreferrer noopener nofollow">[email protected]</a>";
identifier = 5;
ip = "0.0.0.0";
isLoggedIn = 1;
language = "pt_BR";
name = "John Doe";
password = 4badaee57fed5610012a296273158f5f;
})
2012-07-06 18:20:22.968 Contact Manager[8254:10c03] D restkit.object_mapping:RKObjectMappingOperation.m:643 Finished mapping operation successfully...
所以,我的问题是,如果解析器在数据库中找到这些对象,为什么 user
和 origin
为空?映射有问题吗?
最佳答案
经过大量调试,我找到了修复它的方法。首先,我对每个对象使用相同的主键 (@"identifier"
),这一事实导致映射困惑。
之后,我删除了 [contactMapping connectRelationship:withObjectForPrimaryKeyAttribute:]
因为不需要它,现在一切似乎都正常。
关于objective-c - Restkit对象映射给出空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11370399/