ios - Plist file not creating from webservice in one button clickbut loading on second click -


i'm having viewcontroller has show segue next view controller

but each button connected show segue having webservice call , response webservice storing in plist file , accessing in next view

my issue:when click on button directly going nextview without loading plist contents first time when i'm go , click on button showing plist contents

any appreciated

here plist creation code creating after button click

if(connection == conn1) {           nserror *e = nil;      classresponse = [nsjsonserialization jsonobjectwithdata:classdata options:nsjsonreadingmutableleaves error:&e];     nserror *error;      nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);      nsstring *documentsdirectory = [paths objectatindex:0];      nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"classesarray.plist"];      nslog(@"file path %@ ",path);      nsfilemanager *filemanager=[nsfilemanager defaultmanager];      if(![filemanager fileexistsatpath:path])     {          nsstring *bundle = [[[nsbundle mainbundle]resourcepath]stringbyappendingstring:@"classesarray.plist"];          //nsstring *bundle = [[nsbundle mainbundle]pathforresource:@"create" oftype:@"plist"];          [filemanager copyitematpath:bundle topath:path error:&error];      }     [classresponse writetofile:path atomically:yes];   } 

i'm using tableview custom cell in next view load plist here code

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section  {     return [classname count];  }   - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath    *)indexpath   {     static nsstring *cellidentifier = @"cellid";     classesteacher *cell = [tableview                             dequeuereusablecellwithidentifier:cellidentifier                             forindexpath:indexpath];       if (cell == nil) {          cell = [[classesteacher alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];         [cell setselectionstyle:uitableviewcellselectionstylenone];     }      long row = [indexpath row];      cell.classname.text = classname[row];     cell.classdescription.text = classdescription[row];     cell.classcode.text = classref[row];     cell.studentcount.text=classids[row];      return cell;   }  - (void)viewdidload {     [super viewdidload];       nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);      nsstring *documentsdirectory = [paths objectatindex:0];     nsstring *path = [documentsdirectory stringbyappendingpathcomponent:@"classesarray.plist"];       nsdictionary *studentdict=[[nsdictionary alloc]initwithcontentsoffile:path];     // nslog(@" quizdict has %@ ",studentdict);      //get values key in array      nsarray *see = studentdict[@"alldata"];      classname= [see valueforkey:@"class_name"];     classdescription=[see valueforkey:@"class_description"];     classref=[see valueforkey:@"class_ref"];     classids=[see valueforkey:@"number_of_students"];     //classid = [see objectatindex:@"classid"];     nslog(@"class id %@",classid);      nslog(@"class id %@",classids);      uiimage *backimage = [uiimage imagenamed:@"s-left-arrow.png"];     uibutton *backbutton = [uibutton buttonwithtype:uibuttontypecustom];     backbutton.frame = cgrectmake(0, 0, backimage.size.width, backimage.size.height);      [backbutton setimage:backimage forstate:uicontrolstatenormal];     [backbutton addtarget:self action:@selector(pushbackbutton:)    forcontrolevents:uicontroleventtouchupinside];      uibarbuttonitem *backbarbuttonitem = [[uibarbuttonitem alloc] initwithcustomview:backbutton] ;      self.navigationitem.hidesbackbutton = yes;     self.navigationitem.leftbarbuttonitem = backbarbuttonitem;     [self.table reloaddata];         // additional setup after loading view. } 

this sends red flag me, don't understand why storing data in plist , accessing later unless using way pass data between 2 views.

regardless - if data not available when need @ transition time have few choices: 1 - data before transition new view. require hold transition until data returned; 2 - data after transition new view. require pass enough data new view can make request.

need careful option 1. if start request , transition new view view controller first view go out of scope leaving return data no go , possibly calling method no longer exists (app crash).

here article (one of many) on callbacks , delegates. delegate function vs callback function

this can go in either first or second view controller depending on when decide data.

if put in first view controller want change segue , have trigger manually rather in storyboard on button. still create segue in storyboard not assigned button. assign generically view , sure give storyboard name can referenced later. re assign button action trigger download. in completion block callback (or in delegate method) call segue manually once data has returned.

[self performseguewithidentifier: @"yourseguename" sender: self]; 

if decide request data in second view controller can use prepare segue method pass enough data method complete data request

- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender {     if([segue.identifier isequaltostring:@"yourseguename"]) {         yoursecondvc *secondvc = (yoursecondvc *)segue.destinationviewcontroller;         secondvc.dataforrequest= @"request data;     } 

this assumes second view controller declares nsstring variable named "dataforrequest" because second view controller has been created system @ point can use assign data it's member objects if complex data type.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -