ios - Parsing JSON and save locally -
i'm working on ios application written in objective-c , i'm done, except 1 thing:
i'm parsing json tableview, , want store data locally in app. don't know best way of doing this. each row has image, title, description, , link. have different tabs different json.
(i don't want parse json every time app opens up, want check if new has been added json , parse again. i'm happy if can manage save data json locally)
i've been struggling while , need help. last thing need fix before app ready app store.
don't know if need see code, post anyway might better understanding of i'm doing.
code parsing json:
jsondata_ = [[nsmutabledata alloc] init]; nsstring *urlstring = [nsstring stringwithformat:@"http://www.myurl.se/json/%@.json", _whatstate]; nsurl *url = [nsurl urlwithstring: urlstring]; nsurlrequest *urlreq = [nsurlrequest requestwithurl: url]; nsurlconnection *connection = [nsurlconnection connectionwithrequest: urlreq delegate:self]; -(void)connectiondidfinishloading:(nsurlconnection*) connection{ //nslog(@"succeeded! recieved %d bytes of data", [jsondata_ length]); nserror *error; nsarray *items = [nsjsonserialization jsonobjectwithdata: jsondata_ options: kniloptions error: &error]; nssortdescriptor *sortdesc = [[[nssortdescriptor alloc] initwithkey:@"title" ascending:yes]copy]; nsmutablearray *sorteditems = [nsmutablearray arraywithobject: sortdesc]; sorteditems = [[items sortedarrayusingdescriptors: sorteditems] copy]; if(!error) { tabledata_ = sorteditems; tableimgs_ = [[nsmutablearray alloc] initwitharray: tabledata_ copyitems: yes]; [self.tableview reloaddata]; jsonisfinishedloading = true; } else { nslog(@"error"); } }
code tableview:
-(uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"listitem"; tscustomcell *cell = (tscustomcell*) [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath]; [cell setselectedbackgroundview:selectedcellbgcolorview]; if(jsonisfinishedloading) { nsdictionary *row = [tabledata_ objectatindex: indexpath.row]; nsstring *title = [row objectforkey:@"title"]; cell.url = [row objectforkey:@"url"]; uifont *defaultfont = [uifont fontwithname:@"edmondsans-bold" size:12.0]; [cell.listitemlbl settext: title]; [cell.listitemlbl setfont:defaultfont]; if([[tableimgs_ objectatindex:indexpath.row] ismemberofclass: [uiimage class]]) { [cell.listitemimage setimage: [tableimgs_ objectatindex: indexpath.row]]; } else { uiactivityindicatorview *loadingsymbol = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle: uiactivityindicatorviewstylegray]; loadingsymbol.frame = cell.listitemimage.frame; [cell addsubview: loadingsymbol]; [loadingsymbol startanimating]; dispatch_queue_t queue = dispatch_get_global_queue(dispatch_queue_priority_default, 0ul); dispatch_async(queue, ^{ nsstring *url = [row objectforkey: @"image"]; if([url isequaltostring: @""]) { url = @"http://www.myurl.se/images/standard.png"; } else { url = [row objectforkey: @"image"]; } nsurl *imgurl = [nsurl urlwithstring: url]; nsdata *imagedata = [nsdata datawithcontentsofurl: imgurl]; uiimage *image = [uiimage imagewithdata: imagedata]; if(image != nil) { [tableimgs_ replaceobjectatindex:indexpath.row withobject:image]; nslog(@"tableimgs != nil"); } else { nslog(@"tableimgs = nil"); [cell.listitemimage setimage: [uiimage imagenamed:@"standard_transp.png"]]; } dispatch_sync(dispatch_get_main_queue(), ^{ [loadingsymbol removefromsuperview]; [tableview reloadrowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation: uitableviewrowanimationnone]; }); }); //end dispatch_async } } } return cell; }
thanks!!
if understood correct - can save data json user defaults code
[[nsuserdefaults standarduserdefaults] setobject:jsondata_ forkey:@"mydata"];
if parsing json - have data it
when want
jsondata_ = [[nsuserdefaults standarduserdefaults] valueforkey: @"mydata"];
Comments
Post a Comment