ios - POST with RestKit 0.20 - NSManagedObjectContext issue -
i'm trying make post request restkit, when call postobject method of rkobjectmanager following error :
can use -performblockandwait: on nsmanagedobjectcontext created queue.
this error came when method performblockandwait called rkmanagedobjectrequestoperation class.
here code :
rkobjectmapping *responsemapping = [rkobjectmapping requestmapping]; [responsemapping addattributemappingsfromdictionary:@{ // ... }]; nsindexset *statuscodes = rkstatuscodeindexsetforclass(rkstatuscodeclasssuccessful); // in 2xx nsstring *pathpattern = @"mypath"; rkresponsedescriptor *responsedescriptor = [rkresponsedescriptor responsedescriptorwithmapping:responsemapping method:rkrequestmethodpost pathpattern:pathpattern keypath:nil statuscodes:statuscodes]; rkobjectmapping *requestmapping = [rkobjectmapping requestmapping]; [requestmapping addattributemappingsfromdictionary:@{ // ... }]; rkrequestdescriptor *requestdescriptor = [rkrequestdescriptor requestdescriptorwithmapping:requestmapping objectclass:[myclass class] rootkeypath:@"" method:rkrequestmethodpost]; nsurl *baseurl = [nsurl urlwithstring:@"http://192.168.1.1:8080"]; rkobjectmanager *manager = [rkobjectmanager managerwithbaseurl:baseurl]; [manager addrequestdescriptor:requestdescriptor]; [manager addresponsedescriptor:responsedescriptor]; nsmanagedobjectmodel *managedobjectmodel = [nsmanagedobjectmodel mergedmodelfrombundles:nil]; rkmanagedobjectstore *managedobjectstore = [[rkmanagedobjectstore alloc] initwithmanagedobjectmodel:managedobjectmodel]; manager.managedobjectstore = managedobjectstore; [managedobjectstore createpersistentstorecoordinator]; nsstring *storepath = [rkapplicationdatadirectory() stringbyappendingpathcomponent:@"mysqlitedatabasefile"]; nserror *error; nspersistentstore *persistentstore = [managedobjectstore addsqlitepersistentstoreatpath:storepath fromseeddatabaseatpath:nil withconfiguration:nil options:nil error:&error]; nsassert(persistentstore, @"failed add persistent store error: %@", error); [managedobjectstore createmanagedobjectcontexts]; managedobjectstore.managedobjectcache = [[rkinmemorymanagedobjectcache alloc] initwithmanagedobjectcontext:managedobjectstore.persistentstoremanagedobjectcontext]; // post create @try { [manager postobject:visitreport path:@"mypath" parameters:nil success:^( rkobjectrequestoperation *operation , rkmappingresult *mappingresult ) { nslog(@"success"); } failure:^( rkobjectrequestoperation *operation , nserror *error ){ nslog(@"failure - %@",error); }]; } @catch (nsexception *exception) { nslog(@"error - %@", exception); }
i don't understand problem context, welcomed. thank you.
ok find problem :
the object visitreport tried post not nsmanagedobjectcontext expected restkit.
to solve, create new object :
myobject *visitreportcontext = [nsentitydescription insertnewobjectforentityforname:@"tablename" inmanagedobjectcontext:objectstore.mainqueuemanagedobjectcontext]; visitreportcontext.id = visitreport.id visitreportcontext.title = visitreport.title // etc..
then, when post new object, works. it's not beautiful... how can deal old object visitreport without create new 1 in "restkit context" ?
Comments
Post a Comment