Reputation: 77
One feature of my application is to retrieve live data (JSON object) every 2 sec and display it (only while app is in foreground). I can't use push notification service because of server limitations. Could you please tell me effective way for polling in IOS?
Upvotes: 1
Views: 2560
Reputation: 7845
Polling the network wastes battery, but one option may be a recurrent timer:
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self
selector:@selector(checkServer:) userInfo:nil repeats:YES];
Where you hit the network in your checkServer:
function.
Upvotes: 6
Reputation: 1477
You can use a timer.
poolingTimer = [NSTimer timerWithTimeInterval:pollingPeriod target:self selector:@selector(timerRanOut:) userInfo:nil repeats:YES];
And then in your timerRanOut method you can ask for the new json, parse it, and reload the views.
Upvotes: 1