0

I need to add upload functionality and all is done except the progress indicator. I am using an NSOperation to initiate the HTTP request, so uploads would happen in background. But by doing so NSURLConnection delegates are not getting invoked.

//.h file
@interface FooNSOperation : NSOperation<NSURLConnectionDataDelegate>
...
@end
// .m file
@implementation FooNSOperation
...
- (void) main {
  // Upload using NSURLRequest, NSURLConnection
  // NSURLConnection sendAsynchronousRequest: queue: completionHandler:
}
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
  // This never gets invoked
}
@end

How can I get upload progress ?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
user88975
  • 1,618
  • 3
  • 19
  • 29
  • Why are you using NSOperation? `sendAsynchronousRequest:` already works in the background – Hani Ibrahim Dec 31 '13 at 16:56
  • In addition if you use `NSURLConnectionsendAsynchronousRequest: queue: completionHandler:` delegate has not been set up and so not called anywhere. – Lorenzo B Dec 31 '13 at 16:57
  • @HaniIbrahim It does not work in the background but in a async fashion. With `NSURLConnectionsendAsynchronousRequest: queue: completionHandler:delegate` you can choose the queue to work with and hence a different queue from the main one. – Lorenzo B Dec 31 '13 at 16:58
  • @HaniIbrahim It can be multiple file uploads, and I need pause and resume functionality so I thought of using NSOperations and queue so that I can suspend and resume. – user88975 Dec 31 '13 at 16:58
  • 1
    So, to not reinvent the wheel just use **AFNetworking**. – Lorenzo B Dec 31 '13 at 16:59
  • @flexaddicted I already came across that answer but that link didnt work for me. If its working for you could you post a gist of that link in the answer itself ? – user88975 Dec 31 '13 at 17:00
  • you can use `NSURLSession` iOS 7 – Hani Ibrahim Dec 31 '13 at 17:03
  • Take a look here http://stackoverflow.com/questions/7491517/objective-c-asynchronously-populate-uitableview-how-to-do-this – Lorenzo B Dec 31 '13 at 17:10
  • @HaniIbrahim There are lots of reasons to use `NSOperation` rendition with `NSURLConnectionDataDelegate` methods. Primarily, you can make make your operations cancelable and handle progress updates (e.g. to update determinate progress indicator view). Furthermore, if you have to do any authentication, redirect handling, streaming, etc., you really need those delegate calls, too. You can also use operation dependencies, too. `NSOperation`-based requests offers tons of advantages. – Rob Dec 31 '13 at 20:08
  • @flexaddicted yes this is what I meant (Async) – Hani Ibrahim Dec 31 '13 at 21:29
  • @Rob Thanks for the info I'll need to try that soon :) – Hani Ibrahim Dec 31 '13 at 21:29

0 Answers0