4

I'm new to objective-c and I'm building an app that requires a background polling to a generic API to refresh some data on my user interface. After several hours looking for an answer/example that fits my problem I came across some solutions like the following:

long polling in objective-C

polling an external server from an app when it is launched

Poll to TCP server every hour ios

http://blog.sortedbits.com/async-downloading-of-data/

but unfortunately none of them covers my scenario which is really basic: I need to start a polling when viewDidLoad, let's say an infinite loop, and on every iteration, let's say every 10 seconds, to call an API and when I didReceiveData I want to log that data into the console with an NSLog, obviously this can't be done on the main thread.

What I really need is a very simple example on how to do that, and with simple I mean:

  • I can't implement long polling/push notifications for many reasons and not looking for a way to do it, lets' just assume I cannot.
  • I don't want to rely on fancy frameworks like LRResty, RESTKit, AFNetworking or anything else since I don't really need them and also I can't believe that there is no SDK bulletin that can cover this basic scenario.
  • I don't need anything else besides what I described, so no authentication, no parameters in my request, no response handling etc. (since I'm new to objective-c and stuff not strictly needed could just be more confusing to me...)

The solution I'm looking for could be something like this (using NSOperationQueue to run my loop into a separate thread):

- (void)viewDidLoad {
    [super viewDidLoad];
    //To run polling on a separate thread
    operationQueue = [NSOperationQueue new];
    NSInvocationOperation *operation=[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doPolling) object:nil];
    [operationQueue addOperation:operation];
}

-(void)doPolling {
    MyDao *myDao = [MyDao new];
    while (true) {
       [myDao callApi];
       [NSThread sleepForTimeInterval:10];
    }
}

// and in MyDao
-(void)callApi {
    NSMutableString *url= [NSMutableString stringWithFormat:@"%@",@"http:www.example.it"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    request.HTTPMethod = @"GET";
    self.conn= [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(self.conn){
        [self.conn start];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"didReceiveData %s","yes");
}

but unfortunately as stated here: Asynchronous NSURLConnection with NSOperation looks like I cannot do that.

Please help, I refuse to believe that there isn't a simple straightforward solution for this basic scenario.

Community
  • 1
  • 1
Manjia
  • 155
  • 1
  • 10
  • 2
    possible duplicate of [What is the best way to poll data periodically from server when app is active in iOS in a separate thread?](http://stackoverflow.com/questions/13140392/what-is-the-best-way-to-poll-data-periodically-from-server-when-app-is-active-in) – Sammio2 Apr 18 '14 at 14:34
  • 3
    You may be better off with an nstimer instead of putting a thread to sleep. The timer would then do what you are doing anyway. First, it fires off the next timer, then it polls the data and upon receiption it logs the result. – Hermann Klecker Apr 18 '14 at 14:34
  • The answer suggested by Sammio2 definitely does the trick! and yes as Hermann said using a timer is a better solution than putting a thread to sleep, thank you both! – Manjia Apr 18 '14 at 15:15

0 Answers0