xcode - saving Json data to coredata IOS and show it in UITableView simultaneously -
i new ios xcode programming.currently working on app uses json data. app reads json data may large in size. need parse data , store core data when app runs next time can read data there saving lots of time. have tried using dispatch_async
ui seems frozen while data being saved leading app crash. used asihttprequest
read , parse json data works fine part have save data core data , load in uitableview
simultaneously proving pain. if can me i'll grateful.
here code
nsstring *connectionstring = [nsstring stringwithformat:@"%@%@?song_id=%@", server_string, url_get_song_list, lassongid]; nslog(@"connection string is:\n%@", connectionstring); nsurl* url = [nsurl urlwithstring:connectionstring]; //the actual request asihttprequest *request = [asihttprequest requestwithurl:url]; // becoming request delegate //to callbacks requestfinished: or requestfailed: [request setdelegate:self]; nslog(@"fetching dataaaaaaaa %@",url); // fire off request [request startasynchronous]; -(void) requestfinished: (asihttprequest *) request { nsstring *thejson = [request responsestring]; nslog(@"dataaaaaaaa,%@",thejson); nsdictionary *responsedictionary = [thejson jsonvalue]; if ([[responsedictionary valueforkey:@"message"] iskindofclass:[nsarray class]]) { [songsarray addobjectsfromarray:[responsedictionary valueforkey:@"message"]]; if (songsarray.count > 0) { dispatch_async (bgqueue, ^(void){ [self savedownloadedsongs]; }); } } }
savedownloadedsongs--> saves json core data after validations
create nsfetchedresultscontroller entity want store
@property (nonatomic) nsfetchedresultscontroller fetchedresultscontroller; //initialize in viewdidload
- your view controller should nsfetchedresultscontrollerdelegate
implement delegate method nsfetchedresultscontroller
- (void)controllerdidchangecontent:(nsfetchedresultscontroller *)controller { [self.tableview reloaddata]; }
implement data source method uitableview
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return self.fetchedresultscontroller.sections.count; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [self.fetchedresultscontroller.sections[0] numberofobjects]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } someobject *object = [self.fetchedresultscontroller objectatindexpath:indexpath]; cell.label.text = object.property return cell; }
everytime persist new object delegate triggered automatically , reloads table, includes new object.
edit:
if want save time, create new master-detail application. in masterviewcontroller you'll finde source code step 1 , smooth animations in step 3.
Comments
Post a Comment