Search code examples
ioswaitnsoperationnsoperationqueueafhttprequestoperation

AFHttpRequestOperation inside NSOperation


Been doing iOS for a while, but now I'm working in a complex client-server app that requires me to:

  • Send a request to the server (using AFHttpRequestOperation) The response is either an interval or a Dictionary

  • If I get an interval, I need to wait (interval)-seconds and call again the server again (wait / sleep?)

  • this continues until I receive an expired message, the user cancels or a Dictionary (as mentioned above) is received.

  • IF a dictionary is received, I have to pause the calling to the server until the user takes an action on the information I display to him.

  • Based on user decision, I need to resume and keep calling, wait, calling, and so on until i do receive the dictionary or it expires (if the user rejected the information) OR if the user accepts, stop asking and further process the information (but I wont keep calling the serve)

Being new to threading in iOS, what's the best approach:? I have been working with dispatch_asynch + performSelector:onThread:withObject but it's giving me all sort of problems!

Question is: What's the best approach / approach to achieve pause, cancel, wait / sleep in an NSOpertation subclass that internally uses an AFHttpRequestOperation?

I'm a rookie at threading, specially under iOS, so your help is greatly appreciated!!!!


Solution

  •     I would suggest not to use threading with networking . While it doesn’t seem clear enough but implementing a asynchronous network requests take very less CPU time.
      Many try to do this and end up with delegates methods of an asynchronous network calls not being called. If network calls are performed on a background thread, the thread might probably exit before the delegates are finished calling.
      The asynchronous calls can be cancelled just by calling the cancel/pause method.
      If you feel that main thread is very slow, make sure you avoid heavy operation on it, like parsing, calling CameraView.
      I used NSrunLoop for just testing how to do this, without much advantages
     

     -(void)start
    {
      [self willChangeValueForKey:@"isExecuting"];
      _isExecuting = YES;
      [self didChangeValueForKey:@"isExecuting"];
      NSURL* url = [[NSURL alloc] initWithString:@"http:your url"];
      NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
      connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; // ivar
      [request release];
      [url release];
    
     // Via the run loop 
      NSPort* port = [NSPort port];
      NSRunLoop* runLoop = [NSRunLoop currentRunLoop]; 
      [runLoop addPort:port forMode:NSDefaultRunLoopMode];
      [connection scheduleInRunLoop: runLoop forMode:NSDefaultRunLoopMode];
      [connection start];
      [runLoop run];
    }
    

    You can replace the native api's with your library ones. Hope this helps in some way.