ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -
this error has shown on many times, seems. prominent answer issue seems restkit 0.20 — coredata: error: failed call designated initializer on nsmanagedobject class
however, solution posted doesn't work me. let's start beginning:
i want upload file server. want parse response make sure uploaded. if not, try again later. model has boolean variable called nsnumber *senttoserver
purpose (in coredata, bool
saved nsnumber
).
because don't know whether sending going successful, save entity coredata model first before sending via restkit.
when post request successful, server returns json string this:
{"id":14,"created":true}
notice that, of course, id not set on coredata model yet, both id
, created
new informations model.
here's model:
@interface recording : nsmanagedobject @property (nonatomic, retain) nsstring * audiofilepath; @property (nonatomic, retain) nsdate * createdat; @property (nonatomic, retain) nsstring * deviceid; @property (nonatomic, retain) nsstring * devicetype; @property (nonatomic, retain) nsnumber * recordingid; @property (nonatomic, retain) nsnumber * senttoserver; @end
and here's how set up:
nsurl *modelurl = [nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:@"htrecording" oftype:@"momd"]]; nsmanagedobjectmodel *managedobjectmodel = [[[nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl] mutablecopy]; rkmanagedobjectstore *managedobjectstore = [[rkmanagedobjectstore alloc] initwithmanagedobjectmodel:managedobjectmodel]; nsstring *storepath = [rkapplicationdatadirectory() stringbyappendingpathcomponent:@"audiomodel.sqlite"]; nserror *error = nil; nsdictionary *options = [nsdictionary dictionarywithobjectsandkeys: [nsnumber numberwithbool:yes], nsmigratepersistentstoresautomaticallyoption, [nsnumber numberwithbool:yes], nsinfermappingmodelautomaticallyoption, nil]; if (![managedobjectstore addsqlitepersistentstoreatpath:storepath fromseeddatabaseatpath: nil withconfiguration:nil options:options error:&error]) { nsassert(no, @"managed object store failed create persistent store coordinator: %@", error); } [managedobjectstore createmanagedobjectcontexts]; // configure magicalrecord use restkit's core data stack [nspersistentstorecoordinator mr_setdefaultstorecoordinator:managedobjectstore.persistentstorecoordinator]; [nsmanagedobjectcontext mr_setrootsavingcontext:managedobjectstore.persistentstoremanagedobjectcontext]; [nsmanagedobjectcontext mr_setdefaultcontext:managedobjectstore.mainqueuemanagedobjectcontext]; nsstring *databaseurl = @"http://localhost:3000"; rkobjectmanager *objectmanager = [rkobjectmanager managerwithbaseurl:[nsurl urlwithstring:databaseurl]]; objectmanager.managedobjectstore = managedobjectstore; objectmanager.requestserializationmimetype = rkmimetypejson; // setup mapping rkentitymapping* recordingrequestmapping = [rkentitymapping mappingforentityforname:@"recording" inmanagedobjectstore:managedobjectstore]; [recordingrequestmapping addattributemappingsfromdictionary:@{ @"device_id": @"deviceid", @"device_type": @"devicetype" }]; rkrequestdescriptor *requestdescriptor = [rkrequestdescriptor requestdescriptorwithmapping:[recordingrequestmapping inversemapping] objectclass:[recording class] rootkeypath:@"recording"]; [objectmanager addrequestdescriptor:requestdescriptor]; rkentitymapping* recordingresponsemapping = [rkentitymapping mappingforentityforname:@"recording" inmanagedobjectstore:managedobjectstore]; [recordingresponsemapping addattributemappingsfromdictionary:@{ @"created": @"senttoserver", @"id": @"recordingid"}]; recordingresponsemapping.identificationattributes = @[@"recordingid"]; rkresponsedescriptor *responsedescriptor = [rkresponsedescriptor responsedescriptorwithmapping:recordingresponsemapping pathpattern:@"/audio.json" keypath:nil statuscodes:rkstatuscodeindexsetforclass(rkstatuscodeclasssuccessful)]; [objectmanager addresponsedescriptor:responsedescriptor];
firstly, surprisingly lengthy setup code should common (just init restkit , magicalrecord). digress...
the whole upload process works fine. request mappings good, stored on server, including attachments. notice request mappings not contain files, since sent via multipart post methods. again, part works fine. here's code uploading files:
rkobjectmanager *manager = [rkobjectmanager sharedmanager]; nsmutableurlrequest *request; request = [manager multipartformrequestwithobject:recording method:rkrequestmethodpost path:@"audio.json" parameters:nil constructingbodywithblock:^(id<afmultipartformdata> formdata) { [formdata appendpartwithfiledata:[[nsfilemanager defaultmanager] contentsatpath:recording.audiofilepath] name:@"recording[audio]" filename:[recording.audiofilepath lastpathcomponent] mimetype:@"audio/m4a"]; }]; rkobjectrequestoperation *operation = [manager objectrequestoperationwithrequest:request success:^(rkobjectrequestoperation *op, rkmappingresult *mapping) { nslog(@"successfully saved file on server"); } failure:^(rkobjectrequestoperation *op, nserror *error){ nslog(@"some weird error"); }]; [manager enqueueobjectrequestoperation:operation];
mapping response object becomes difficult. without matching response mapping, log outputs contain error couldn't handle response (surprise). with matching response mapping, however, app crashes. here stack trace:
#11 0x2f6482d6 in -[nsobject(nskeyvaluecoding) valueforkeypath:] () #12 0x00113b9a in -[rkmappingoperation shouldsetvalue:forkeypath:usingmapping:] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/objectmapping/rkmappingoperation.m:436 #13 0x00114e40 in -[rkmappingoperation applyattributemapping:withvalue:] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/objectmapping/rkmappingoperation.m:536 #14 0x00115fd4 in -[rkmappingoperation applyattributemappings:] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/objectmapping/rkmappingoperation.m:577 #15 0x0011c988 in -[rkmappingoperation main] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/objectmapping/rkmappingoperation.m:948 #16 0x2f655d0a in -[__nsoperationinternal _start:] () #17 0x0010cd5a in -[rkmapperoperation maprepresentation:toobject:atkeypath:usingmapping:metadata:] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/objectmapping/rkmapperoperation.m:256 #18 0x0010b6aa in -[rkmapperoperation maprepresentation:atkeypath:usingmapping:] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/objectmapping/rkmapperoperation.m:176 #19 0x0010ddc8 in -[rkmapperoperation maprepresentationorrepresentations:atkeypath:usingmapping:] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/objectmapping/rkmapperoperation.m:314 #20 0x0010e552 in -[rkmapperoperation mapsourcerepresentationwithmappingsdictionary:] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/objectmapping/rkmapperoperation.m:359 #21 0x0010ee72 in -[rkmapperoperation main] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/objectmapping/rkmapperoperation.m:398 #22 0x2f655d0a in -[__nsoperationinternal _start:] () #23 0x00100e70 in -[rkobjectresponsemapperoperation performmappingwithobject:error:] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/network/rkresponsemapperoperation.m:345 #24 0x000ff4e6 in -[rkresponsemapperoperation main] @ /users/earthnail/development/restkitfileupload/pods/restkit/code/network/rkresponsemapperoperation.m:297 #25 0x2f655d0a in -[__nsoperationinternal _start:] ()
the line debugger stops sigabrt
in [rkmappingoperation shouldsetvalue:forkeypath:usingmapping:]
is:
id currentvalue = [self.destinationobject valueforkeypath:keypath];
note when set breakpoint suggested in https://stackoverflow.com/a/13734348/487556, breakpoint never reached. implies there no issues path of response mapping.
i can imagine several things here going wrong:
- restkit assumes there no coredata entity yet , wants create one. lead duplicate entities (which bummer!), still shouldn't crash, since fields of model optional.
- restkit tries find coredata entity based on id (since identifier variable), of course id not set yet.
- something else...
any suggestions?
the problem matching path in response description , request. make work slashes in them should same, either @"/audio.json"
or @"audio.json"
otherwise restkit doesn't see 1 matches other.
Comments
Post a Comment