0

I have set up my application so that every the application enters the foreground, it does a web call which calls information from a web server, where it receives and parses JSON, then stores it offline.

My application is a tabbed application, and each screen the information is set up from the JSON, and the images are also pulled from a server. So I want the information on each page to update after this is done, but its not updating.

I am running my application from the simulator for the time being incase that makes any difference. At present only way I can get the information to update is either use that viewDidAppear or stop and start the application again from xCode.

I tried calling the classes viewDidLoad from the AppDelegates

 - (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */

   WebCalls *wc = [[WebCalls alloc] init];

    DashboardVC *db = [[DashboardVC alloc] init];

    [wc setWebCallDidFinish:^(NSString * json, NSString *ID) {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [paths objectAtIndex: 0];
        NSString *docFile = [docDir stringByAppendingPathComponent: @"json.txt"];
        [json writeToFile:docFile atomically:NO encoding:NSUTF8StringEncoding error:Nil];

        NSString *docDir2 = [paths objectAtIndex: 0];
        NSString *docFile2 = [docDir2 stringByAppendingPathComponent: @"theID.txt"];
        [ID writeToFile:docFile2 atomically:NO encoding:NSUTF8StringEncoding error:Nil];

        [db testme];
    }];
    [wc getData];


}

This calls the viewDidLoad again, but the information doesn't update. Any ideas why?

Edit: In that class, it reads the JSON like this

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [paths objectAtIndex: 0];
        NSString *docFile = [docDir stringByAppendingPathComponent: @"json.txt"];
        [json writeToFile:docFile atomically:NO encoding:NSUTF8StringEncoding error:Nil];

I can print the JSON out, and the JSON is definitely updating, but the information on the screen is not.

Edit: Updated with web call

AdamM
  • 4,400
  • 5
  • 49
  • 95
  • Why did you leave out the web call code? It seems like that might be important. Is it asynchronous? – Brian May 10 '12 at 14:18
  • Yeah the web call is asynchronous as you can. wc is just an object of my WebCall class. But the code to refresh is inside the method which is only called after the web call is finished – AdamM May 10 '12 at 14:24
  • Updated to show web call code – AdamM May 10 '12 at 14:29

2 Answers2

0

you can perhaps try using a delegate method to update the fetched data into the UI. Call the delegate method after the JSON parsing is over. I am doing the same thing with xml parsing and it's working.

  • (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.delegate optimizeAndDrawShortestPath:coordinatesArray];

    [routeArray release]; routeArray = nil; }

Check the above example. This is the method where my parsing of the xml document gets over. Then as you can see in the first line, I call a delegate method to update UI and draw the line on the map. The delegate method is implemented in the class that calls the object of my parsing class.

I assume, you are also doing something similar with JSON as I am doing with XML. To know about protocols and delegate methods check this SO answer.

I am pretty sure, this is what you are looking for.

Community
  • 1
  • 1
zahreelay
  • 1,742
  • 12
  • 18
0

Fixed this issue. Set up periodic web call in application and when web call is made, calls refresh method in the class.

AdamM
  • 4,400
  • 5
  • 49
  • 95