0

I am trying to parse JSON with iOS5's NSJSONSerialization. This code CORRECTLY parse the data I wanted for the first time, BUT THEN THE DATA JUST STAYED THE SAME. The JSON on the URL already changed, but the code kept giving me the first data it parsed, which is now incorrect. No matter how many times I "build and run", it keeps giving me the same thing.

When I copied the code to the new project, it works, again, for the first time, and then does the same thing.

I have no idea where the problem is, maybe cache?

Thanks for you help!!!

- (void)fetchedData:(NSData *)responseData {
    NSError* error;
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
                                                     options:kNilOptions 
                                                       error:&error];
    NSArray* allDepartures = [json objectForKey:@"departures"];

    NSLog(@"departures: %@", allDepartures);
    NSDictionary* stops = [allDepartures objectAtIndex:0];
    NSNumber* time = [stops objectForKey:@"expected_mins"];
    NSString* name = [stops objectForKey:@"headsign"];

    nameLabel.text = [NSString stringWithFormat:@"%@",name];
    timeLabel.text = [NSString stringWithFormat:@"%i",[time intValue]];
}

- (IBAction)getInfo:(id)sender {
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL: myURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) 
        withObject:data waitUntilDone:YES];
});
Jake1164
  • 12,291
  • 6
  • 47
  • 64
  • after a while (like one hour), it loads like when the app is first installed (the data is correct), then it does the same thing, not updating – Nop Shusang Jun 13 '12 at 16:47

1 Answers1

0

How do you set "myURL", only in viewDidLoad? After a while if the OS calls viewDidUnload and you run the app again, viewDidLoad is called and myURL is set again. If thats the case you should set myURL every time you call getInfo.

user1447414
  • 1,306
  • 2
  • 12
  • 25
  • Thanks for your response. I tried it, but still didn't work. I think it has something to do with cache. The first time I ran the app, it took 2-3 seconds to load, but after that it just shows up without loading. – Nop Shusang Jun 14 '12 at 13:48
  • set a break point at the NSData*data line and double check myURL is ok – user1447414 Jun 14 '12 at 16:00