Restkit 映射问题 - 将新创建的托管对象发布到服务器

Posted

技术标签:

【中文标题】Restkit 映射问题 - 将新创建的托管对象发布到服务器【英文标题】:Restkit mapping issue - post newly created managed object to the server 【发布时间】:2014-04-21 01:25:45 【问题描述】:

我正在尝试使用 rest 工具包将新的托管对象发布到服务器,但我不知道我做错了什么。我收到如下异常:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“RKRequestDescriptor 对象必须使用目标类为 NSMutableDictionary 的映射进行初始化,获得了“用户”(参见 [RKObjectMapping requestMapping])”

我在 this one 这样的堆栈溢出帖子中寻找解决方案 这是我在 MappingProvider 类中的实体映射方法:

+(RKMapping *)usersMapping

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Users" inManagedObjectStore:[[DateModel sharedDataModel]objectStore]];

[mapping addAttributeMappingsFromDictionary:@
                                              @"id": @"user_id",
                                              @"address1": @"address1",
                                              @"address2": @"address2",
                                              @"created_at":@"created_at",
                                              @"updated_at": @"updated_at",
                                              @"email": @"email",
                                              @"name":@"name",
                                              @"password_digest": @"password_digest",
                                              @"phone_no": @"phone_no",
                                              @"postcode":@"postcode",
                                              @"remember_token":@"remember_token",
                                              @"user_type": @"user_type",
                                              @"apns_token":@"apns_token"
                                              
 ];

[mapping addRelationshipMappingWithSourceKeyPath:@"admins" mapping:[MappingProvider adminsMapping]];
[mapping addRelationshipMappingWithSourceKeyPath:@"carers" mapping:[MappingProvider carersMapping]];
[mapping addRelationshipMappingWithSourceKeyPath:@"customers" mapping:[MappingProvider customersMapping]];
[mapping addRelationshipMappingWithSourceKeyPath:@"userWearers" mapping:[MappingProvider customersMapping]];

return mapping;

这是当用户填写所有文本字段并单击注册按钮时调用的方法:

-(void)registerUser
RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider usersMapping] method:RKRequestMethodPOST pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
//here Xcode return exception
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[MappingProvider usersMapping] objectClass:[Users class] rootKeyPath:nil method:RKRequestMethodPOST];
[[DateModel sharedDataModel]addResponseDescriptor:userResponseDescriptor];
[[DateModel sharedDataModel]addRequestDescriptor:requestDescriptor];

RKManagedObjectStore *objectStore = [[DateModel sharedDataModel]objectStore];

Users *user = [NSEntityDescription insertNewObjectForEntityForName:@"Users" inManagedObjectContext:objectStore.mainQueueManagedObjectContext];
user.email = _email;
user.password_digest  =_password;
user.name = _name;
user.address1 = _address;
user.postcode = [NSNumber numberWithInteger:[_postcode integerValue]];
user.phone_no = [NSNumber numberWithInteger:[_mobileNumber integerValue]];



[[RKObjectManager sharedManager] postObject:user path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
    NSLog(@"Success saving user");
 failure:^(RKObjectRequestOperation *operation, NSError *error) 
    NSLog(@"Failure saving user: %@", error.localizedDescription);
];

日期模型设置方法:

- (void)setup 
self.objectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:[self managedObjectModel]];

NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Data.sqlite"];
NSLog(@"Setting up store at %@", path);
[self.objectStore addSQLitePersistentStoreAtPath:path
                          fromSeedDatabaseAtPath:nil
                               withConfiguration:nil
                                         options:[self optionsForSqliteStore]
                                           error:nil];

[self.objectStore createManagedObjectContexts];


//Configure a managed object cache to ensure we do not create duplicate objects

 self.objectStore.managedObjectCache =[[RKInMemoryManagedObjectCache alloc]initWithManagedObjectContext:self.objectStore.persistentStoreManagedObjectContext];

// Set the default store shared instance
[RKManagedObjectStore setDefaultStore:self.objectStore];

【问题讨论】:

【参考方案1】:

请求描述符的目的是将您的自定义对象转换为NSMutableDictionary,以便可以对其进行序列化和发送。您当前使用的映射是用于转换为Users 对象,因此您需要使用不同的映射。

RestKit 有一个方便的方法供您使用:

... requestDescriptorWithMapping:[[MappingProvider usersMapping] inverseMapping] ...

【讨论】:

以上是关于Restkit 映射问题 - 将新创建的托管对象发布到服务器的主要内容,如果未能解决你的问题,请参考以下文章

如何从 RestKit 托管对象存储中删除对象

Restkit 请求动态映射

RestKit 响应未正确映射

当响应包含在数组中时,RestKit POSTed 托管对象变得重复

RestKit 更新对象映射空属性

restkit,如何在没有对象映射的情况下访问对象