Restkit映射问题 - 将新创建的托管对象发布到服务器 [英] Restkit mapping issue - post newly created managed object to the server

查看:262
本文介绍了Restkit映射问题 - 将新创建的托管对象发布到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过使用休息包发布新的管理对象到服务器,但我不知道我做错了什么。我得到如下异常:

I'm trying to post new managed object to the server by using rest kit, but I don't know what I'm doing wrong. I'm getting exception like the following:


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

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'RKRequestDescriptor objects must be initialized with a mapping whose target class is NSMutableDictionary, got 'Users' (see [RKObjectMapping requestMapping])'

我在寻找堆栈溢出帖子的解决方案,例如这一个
这是我的MappingProvider类的实体映射方法:

I was looking for solution in stack overflow posts like this one This is my entity mapping method from MappingProvider class:

+(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;

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

This is the method called when user fill all the textfields and click the register button:

-(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];

}

推荐答案

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

The purpose of the request descriptor is to convert your custom object into an NSMutableDictionary so that it can be serialised and sent. The mapping you're currently using is for converting into a Users object, so you need to use a different mapping.

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

RestKit has a convenience method that you can use:

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

这篇关于Restkit映射问题 - 将新创建的托管对象发布到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆