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:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '
RKRequestDescriptor
objects must be initialized with a mapping whose target class isNSMutableDictionary
, got 'Users' (see[RKObjectMapping requestMapping]
)'
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);
}];
}
Date-model setup method:
- (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];
}