The correct way to poll a web service in an iPhone app

I am trying to determine the best web service polling strategy once per minute, parse the returned xml and then update the object stored in the shared instance. This process should be launched in a separate thread and will continue until the application is launched.

It seems that I could translate all the code into a webservice call and parse the xml in NSOperation and add NSOperation to the NSOperationQueue stored in the application delegate as soon as the application starts.

Is it correct to use NSTimer inside the main NSOperation method so that the operation is performed once a minute, indefinitely? In this case, NSOperation will never return - it seems that I want, but I'm not sure if this is the right way to think about it.

The problem I'm trying to solve is, of course, extremely widespread, so I'm trying to figure out the correct way to implement it. Any advice really appreciated.

+5
source share
4 answers

I would not use NSTimer for this problem / design. I have to create NSThread from AppDelegate when the application starts. I would lower the priority of this topic. The main method of NSThread is basically a loop.

-(void)main {

    while(true) {
        // get raw data from url
        // hash the result
        // compare the hash to the last time
        if (currentHash != lastHash) {
          // post a notification to default center with the new data
          lastHash = currentHash;
        }
        // sleep the thread sleepForTimeInterval
    }
}

Your Model object will subscribe to the notification from the stream and analyze the new data and update ivars. The View object will listen to the model using KVO and display any updates / changes.

-1
source

- push notifications. - , , .

, NSTimer , NSOperation. , performSelectorInBackground:withObject: . NSThread sleepForTimeInterval: .

0

ASIHTTPRequest. .

Edit:

, .

0

: , . , /.

///, ( ).

0

All Articles