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:

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.