vassilag
vassilag

Reputation: 470

Effective automatic refresh of UITableview without user interaction (IOS 5)

I have never coded an App with Push Notifications and I have a very general question. I would like to refresh a UITableview's data (coming from a webservice) automaticaly without "pull to refresh" or any other user's interaction. Is it possible to use Push notifications to inform my app for a data change, in order to avoid polling every xx seconds wasting bandwidth and battery?

In other words, while my app runs in the foreground (maybe) I can handle the message and the user wont receive a popup but what happens otherwise?

Is there any other way to achieve the same result?

I am going to use IOS 5.0+

Thanks in Advance

Upvotes: 2

Views: 3371

Answers (2)

Stephen Darlington
Stephen Darlington

Reputation: 52565

When your app is running, you can register to receive push notifications. Then it's simply a matter of implementing the method, making it update your tableview.

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {

It's pretty well documented by Apple.

Upvotes: 1

Dion
Dion

Reputation: 3345

You could make a server HTTP-Request which only returns something if there was a change. Then in case this response is not empty you can reload the data. The function you can call by a NSTimer.

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(get) userInfo:nil repeats:YES];

and

-(void)get {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com/app.php"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    if(![responseString isEqualToString:@""]) {
    // reload
    }
}

Upvotes: 4

Related Questions