iphone - Restkit 0.20 objects are not mapped after getting json response -
i'm using restkit 0.20. i'm trying download json response local rails application , save core-data. tried follow https://github.com/restkit/rkgist/blob/master/tutorial.md , worked fine.
wanted use many times made base class(listcontroller) , subclassed sessionlistcontroller , eventlistcontroller. mapping provided in appdelegate.m.
the app has first viewcontroller root controller maps responses , works fine. change viewcontroller response , operation stops. does't if mapping has started. i'm not sure have missed.
here i'm initialising restkit
appdelegate.m
#import "appdelegate.h" #import <uikit/uikit.h> @implementation appdelegate rkentitymapping *evententitymapping,*sessionentitiymapping; - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { nserror *error = nil; nsurl *modelurl = [nsurl fileurlwithpath:[[nsbundle mainbundle] pathforresource:@"event" oftype:@"momd"]]; nsmanagedobjectmodel *managedobjectmodel = [[[nsmanagedobjectmodel alloc] initwithcontentsofurl:modelurl] mutablecopy]; [self setmanagedobjectstore: [[rkmanagedobjectstore alloc] initwithmanagedobjectmodel:managedobjectmodel]]; // initialize core data stack [self.managedobjectstore createpersistentstorecoordinator]; nspersistentstore __unused *persistentstore = [self.managedobjectstore addinmemorypersistentstore:&error]; nsassert(persistentstore, @"failed add persistent store: %@", error); [self.managedobjectstore createmanagedobjectcontexts]; // set default store shared instance [rkmanagedobjectstore setdefaultstore:self.managedobjectstore]; nsstring *url=@"http://192.168.11.11:3000"; rkobjectmanager *objectmanager = [rkobjectmanager managerwithbaseurl:[nsurl urlwithstring:url]]; objectmanager.managedobjectstore = self.managedobjectstore; [rkobjectmanager setsharedmanager:objectmanager]; [self mapentities]; rkresponsedescriptor *responsedescriptor = [rkresponsedescriptor responsedescriptorwithmapping:evententitymapping pathpattern:@"/api/v1/events" keypath:nil statuscodes:rkstatuscodeindexsetforclass(rkstatuscodeclasssuccessful)]; [objectmanager addresponsedescriptor:responsedescriptor]; rkresponsedescriptor *responsedescriptor2 = [rkresponsedescriptor responsedescriptorwithmapping:sessionentitiymapping pathpattern:@"/api/v1/sessions" keypath:nil statuscodes:rkstatuscodeindexsetforclass(rkstatuscodeclasssuccessful)]; [objectmanager addresponsedescriptor:responsedescriptor2]; // override point customization after application launch. uistoryboard *storyboard = [uistoryboard storyboardwithname:@"mainstoryboard_iphone" bundle:nil]; uinavigationcontroller *navigationcontroller = [storyboard instantiateviewcontrollerwithidentifier:@"maincentercontroller"]; iiviewdeckcontroller* seconddeckcontroller = [[iiviewdeckcontroller alloc] initwithcenterviewcontroller:navigationcontroller leftviewcontroller:[storyboard instantiateviewcontrollerwithidentifier:@"sidebarcontroller"]]; seconddeckcontroller.centerhiddeninteractivity=iiviewdeckcenterhiddennotuserinteractivewithtaptoclose; self.window.rootviewcontroller = seconddeckcontroller; return yes; } -(void)mapentities{ //event evententitymapping = [rkentitymapping mappingforentityforname:@"event" inmanagedobjectstore:self.managedobjectstore]; [evententitymapping addattributemappingsfromdictionary:@{ @"id": @"event_id", @"name": @"name", @"description": @"desc", @"no_of_days": @"no_of_days", @"start_date": @"start_date", }]; evententitymapping.identificationattributes = @[ @"event_id" ]; sessionentitiymapping = [rkentitymapping mappingforentityforname:@"session" inmanagedobjectstore:self.managedobjectstore]; [sessionentitiymapping addattributemappingsfromdictionary:@{ @"name": @"name", @"description": @"desc", @"start_time": @"start_time", @"duration": @"duration", @"location_id": @"location_id", @"event_id": @"event_id", @"id": @"session_id", }]; sessionentitiymapping.identificationattributes = @[ @"session_id" ]; }
base class i'm calling getobjectsatpath rkmanager depending on base class.
listcontroller.m
@implementation listcontroller - (void)viewdidload { [super viewdidload]; appdelegate *appdelegate = (appdelegate *)[[uiapplication sharedapplication] delegate]; self.managedobjectcontext = appdelegate.managedobjectstore.mainqueuemanagedobjectcontext; [self loadlocs]; [self.refreshcontrol beginrefreshing]; } -(void)setmodel:(models)model{ if(self.model!=model){ _model=model; self.title=[model displayfor:self.model]; _fetchedresultscontroller = nil; [self.tableview reloaddata]; [self loadlocs]; [self.refreshcontrol beginrefreshing]; } } - (void)loadlocs { [[rkobjectmanager sharedmanager] getobjectsatpath:[model listapifor:self.model] parameters:nil success:^(rkobjectrequestoperation *operation, rkmappingresult *mappingresult) { [self.refreshcontrol endrefreshing]; } failure:^(rkobjectrequestoperation *operation, nserror *error) { [self.refreshcontrol endrefreshing]; uialertview *alertview = [[uialertview alloc] initwithtitle:@"an error has occurred" message:[error localizeddescription] delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil]; nslog(@"error: %@",error); [alertview show]; }]; } #pragma mark - table view - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [[[self fetchedrcfortableview:tableview] sections] count]; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ id <nsfetchedresultssectioninfo> sectioninfo = [self.fetchedresultscontroller sections][section]; return [sectioninfo numberofobjects]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [self.tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; [self configurecell:cell atindexpath:indexpath fortableview:tableview]; return cell; } #pragma mark - fetched results controller - (nsfetchedresultscontroller *)fetchedresultscontroller { if (_fetchedresultscontroller != nil) { return _fetchedresultscontroller; } nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; // edit entity name appropriate. nsentitydescription *entity = [nsentitydescription entityforname:[model entityfor:self.model] inmanagedobjectcontext:self.managedobjectcontext]; [fetchrequest setentity:entity]; // set batch size suitable number. [fetchrequest setfetchbatchsize:20]; // edit sort key appropriate. nssortdescriptor *sortdescriptor = [[nssortdescriptor alloc] initwithkey:[model sortdescriptorfor:self.model] ascending:yes]; nsarray *sortdescriptors = @[sortdescriptor]; [fetchrequest setsortdescriptors:sortdescriptors]; nsfetchedresultscontroller *afetchedresultscontroller = [[nsfetchedresultscontroller alloc] initwithfetchrequest:fetchrequest managedobjectcontext:self.managedobjectcontext sectionnamekeypath:nil cachename:[model displayfor:self.model]]; afetchedresultscontroller.delegate = self; self.fetchedresultscontroller = afetchedresultscontroller; nserror *error = nil; if (![self.fetchedresultscontroller performfetch:&error]) { nslog(@"unresolved error %@, %@", error, [error userinfo]); abort(); } nslog(@"_fetchedresultscontroller count %d",_fetchedresultscontroller.fetchedobjects.count); return _fetchedresultscontroller; } @end
this 1 of base classes set model name
eventviewcontroller.m
- (void)viewdidload { [super viewdidload]; self.model=eventm; // additional setup after loading view. }
sessionviewcontroller.m
- (void)viewdidload { [super viewdidload]; self.model=sessionm; // additional setup after loading view. }
full code @ https://github.com/leohemanth/event-app
after using
rklogconfigurebyname("restkit", rkloglevelwarning); rklogconfigurebyname("restkit/objectmapping", rklogleveltrace); rklogconfigurebyname("restkit/network", rklogleveltrace);
i can see that, i'm getting proper http response not being mapped. i'm not sure why?
i've updated restkit package 0.20.2. i'm not facing problem now. should fixed in update.
Comments
Post a Comment