Reputation: 9241
I have a strange problem when trying to upload image with RestKit. I have studied similar questions but none of them could fix my problem.
In my application user can create comment on some Task.
So I have setup both RKEntityMapping
and RKObjectMapping
mapping, like this:
- (RKEntityMapping *)commentsMapping {
RKEntityMapping *commentsMapping = [RKEntityMapping mappingForEntityForName:@"DBComments" inManagedObjectStore:objectManager.managedObjectStore];
commentsMapping.setDefaultValueForMissingAttributes = NO;
commentsMapping.identificationAttributes = @[@"id"];
[commentsMapping setModificationAttributeForName:@"updated_at"];
[commentsMapping addAttributeMappingsFromDictionary:@{
@"body" : @"body",
@"created_at" : @"created_at",
@"id" : @"id",
@"parent_id" : @"parent_id",
@"send_email" : @"send_email",
@"updated_at" : @"updated_at",
@"user.id" : @"user_id",
@"user.name" : @"user_name",
}];
[commentsMapping addRelationshipMappingWithSourceKeyPath:@"attachments" mapping:[self attachmentsMapping]];
return commentsMapping;
}
- (RKObjectMapping *)commentsRequestMapping {
RKObjectMapping *commentsRequestMapping = [RKObjectMapping requestMapping];
[commentsRequestMapping addAttributeMappingsFromDictionary:@{
@"body" : @"body",
@"created_at" : @"created_at",
@"id" : @"id",
@"parent_id" : @"parent_id",
@"send_email" : @"send_email",
@"updated_at" : @"updated_at",
@"user.id" : @"user_id",
@"user.name" : @"user_name",
}];
return commentsRequestMapping;
}
I have define, URL_COMMENTS like this:
#define URL_COMMENTS @"/comments"
To get objects, I call:
[[RKObjectManager sharedManager] getObjectsAtPath:URL_COMMENTS
parameters:@{@"item_id": parentID}
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
KLog(@"success");
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
KLog(@"fail");
}];
And it is working perfectly, without any error.
If I need to create new comment, I call:
[[RKObjectManager sharedManager] postObject:comment path:URL_COMMENTS
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
KLog(@"success");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
KLog(@"fail");
}];
It is working perfect too, without any error.
But when I need to create a comment with image attachment, I call:
NSMutableURLRequest *request =[[RKObjectManager sharedManager] multipartFormRequestWithObject:comment
method:RKRequestMethodPOST
path:URL_COMMENTS
parameters:nil
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.7)
name:@"photo"
fileName:@"photo.jpg"
mimeType:@"image/jpg"];
}];
RKObjectRequestOperation *operation = [[RKObjectManager sharedManager] objectRequestOperationWithRequest:request
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// Success handler.
KLog(@"%@", [mappingResult firstObject]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
KLog(@"fail");
}];
[operation start];
Now here is the problem, comment is created on server side successfully but application is terminated with error:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DBComments 0x11e01400> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "id".'
Here is some more Log
D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation: {
body = "Test attachment";
"created_at" = "2014-04-02T15:41:00+02:00";
id = 13379;
"parent_id" = 291160;
"updated_at" = "2014-04-02T15:41:00+02:00";
user = {
id = 12891;
name = "Rehmat Qadir";
};
}
and targetObject: (null)
D restkit.object_mapping:RKMapperOperation.m:300 Found mappable data at keyPath '': {
body = "Test attachment";
"created_at" = "2014-04-02T15:41:00+02:00";
id = 13379;
"parent_id" = 291160;
"updated_at" = "2014-04-02T15:41:00+02:00";
user = {
id = 12891;
name = "Rehmat Qadir";
};
}
CoreData: error: Failed to call designated initializer on NSManagedObject class 'DBComments'
2014-04-02 18:41:00.269 MeetingKing[14722:f03] D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
body = "Test attachment";
"created_at" = "2014-04-02T15:41:00+02:00";
id = 13379;
"parent_id" = 291160;
"updated_at" = "2014-04-02T15:41:00+02:00";
user = {
id = 12891;
name = "Rehmat Qadir";
};
} with mapping <RKEntityMapping:0xc686500 objectClass=DBComments propertyMappings=(
"<RKAttributeMapping: 0xc6877d0 user.id => user_id>",
"<RKAttributeMapping: 0xc6875c0 id => id>",
"<RKAttributeMapping: 0xc6875e0 send_email => send_email>",
"<RKAttributeMapping: 0xc687610 created_at => created_at>",
"<RKAttributeMapping: 0xc687630 parent_id => parent_id>",
"<RKAttributeMapping: 0xc6877e0 updated_at => updated_at>",
"<RKAttributeMapping: 0xc687850 user.name => user_name>",
"<RKAttributeMapping: 0xc687920 body => body>",
"<RKRelationshipMapping: 0xc688b90 attachments => attachments>"
)>
D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping operation...
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<DBComments 0x11e01400> valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "id".'
I think it might related to some URL
issue.
I am sending request at URL_COMMENTS
which is defined as #define URL_COMMENTS @"/comments"
I have tried by sending request at comments
(without beginning /) and set pathPattern
in responseDescriptor
as /comments
(with beginning /)
I also have tried by sending request /comments
(with beginning /) and set pathPattern
in responseDescriptor
as comments
(without beginning /)
[RKResponseDescriptor responseDescriptorWithMapping:[self commentsMapping]
method:RKRequestMethodGET
pathPattern:URL_COMMENTS
keyPath:@"" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)],
Also tried by setting both places same with or without /
but nothing is working.
I had previously fixed this error by some combination of /
in path whenever I needed to call AFNetworking
method. But don't know why this error is occuring.
Please help
Upvotes: 4
Views: 1396
Reputation: 119031
Because the comment is a managed object and you are asking RestKit to treat it like a plain object. So, RestKit is trying to create a new instance in which to set the response data and failing on the creation.
You should be using managedObjectRequestOperationWithRequest:managedObjectContext:success:failure:
to create the operation so that it has a reference to the MOC and can properly create the entity instance (though it should find the existing entity instance in this case).
Upvotes: 8