0

Im fetching data from database through a php-API with this code:

- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self loadDataWithSpinner];
[self reloadAllData];

}

- (void) loadDataWithSpinner {
if (!self.spinner) [self setupSpinnerView];
self.sessions = nil;

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
    self.userId = [[NSUserDefaults standardUserDefaults] integerForKey:@"CurrentUserId"] ;
    self.sessions = [self getAllSessionsForUserId:self.userId];
    dispatch_async(dispatch_get_main_queue(), ^(void){
        if (self.sessions) {
            self.spinnerBgView.hidden = YES;
            [self setupChartsAndCountingLabelsWithData];
        }
    });
});

}

- (NSArray *) getAllSessionsForUserId: (int) userId {
NSData *dataURL = nil;
NSString *strURL = [NSString stringWithFormat:@"http://webapiurl.com/be.php?queryType=fetchAllBasicSessions&user_id=%i", userId];
dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSError *error = nil;

if (dataURL) {
    NSArray *sessions = [NSJSONSerialization JSONObjectWithData:dataURL options:kNilOptions error:&error];
    return sessions;
} else {
    return Nil;
}

}

Is there something wrong with the code? I'm getting the correct data when testing in a web-browser with the same database call. But in the app it sometimes updates straight away, as it should, and sometimes it takes up to five minutes for the data to update. Even though i remove the app from the phone/simulator, the data sometime hasn't been update when opening the app again.

I would really appreciate some help.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
sold
  • 314
  • 1
  • 3
  • 11

1 Answers1

0

I finally found the answer. Its nothing wrong with my code, as I thought. The problem lies on the server side. And, as they say in this thread:

NSURLConnection is returning old data

It seems to be badly configured server-side or proxy. Which makes the cache act all wierd. I tried another server and everything worked just fine!

Hope this helps someone with the same issue, cause it sure wasted me ALOT of time.

Community
  • 1
  • 1
sold
  • 314
  • 1
  • 3
  • 11