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

时间:2014-04-21 01:25:45

标签: core-data http-post restkit restkit-0.20

我正在尝试使用rest kit将新的托管对象发布到服务器,但我不知道我做错了什么。我得到的例外情况如下:

  

由于未捕获的异常“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 个答案:

答案 0 :(得分:7)

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

RestKit有一个方便的方法,您可以使用:

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