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

4

2 に答える 2

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.

于 2012-05-10T13:58:49.147 に答える
0

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

于 2012-05-23T09:18:24.867 に答える