3

Both of the following questions are being asked in context to maintain NSOperationQueue and NSInvocationOperation.

As I have used this concept to download multiple videos, how do I remove/release added NSInvocationOperation from NSOperationQueue after completion of downloading a video?

And also, what should I do if in case I want to stop to download specific video while the process of downloading in progress?

alloc_iNit
  • 5,173
  • 2
  • 26
  • 54

2 Answers2

5

how do I remove/release added NSInvocationOperation from NSOperationQueue after completion of downloading a video?

The operation is automatically removed from the queue when it has finished i.e. when -isFinished returns true.

And also, what should I do if in case I want to stop to download specific video while the process of downloading in progress?

When you want to stop an operation part way through, you must send it the -cancel message. However this will not magically stop the operation from running. Your task needs to periodically check if it is cancelled and finish itself if it turns out that it has been. So you need to download your video in chunks and check the operation's cancelled status after each chunk. The following pseudocode might help:

 while (![myOperation isCancelled] && thereIsMoreData)
 {
     download a chunk of data and save it
 }

This means, for instance, you can't use NSURLConnection's -sendSynchronousRequest:returningResponse:error: to get the data because it won't be able to check the cancelled status of the operation until after all the data is already downloaded.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • I have implemented the same logic but I was lacking little bit due to some confusion of NSOperationQueue. Thanks JeremyP for your descriptive reply. It's working perfectly as I wanted to. – alloc_iNit Jul 26 '11 at 12:58
  • Can I pause the asynchronous downloading process of video for a while? – alloc_iNit Jul 27 '11 at 11:06
  • I presume you actually want to drop the connection for a while. If that's the case, it probably depends on the server. For instance, HTTP/1.1 has a `range` header that allows the client to request partial downloads, but the server doesn't have to implement the logic to do this. – JeremyP Jul 27 '11 at 12:16
  • How do I make my server able to support partial downloading? Or What are the modifications should be required for the same in server? – alloc_iNit Jul 29 '11 at 08:00
  • @iApple: it depends on the server. You might find that your server already supports the range header. Try it and see. – JeremyP Aug 01 '11 at 10:07
  • I'm puzzled by myOperation reference in this code. If I use NSInvocationOperation, the running code exists inside some other method, and it has no reference to the operation that called it. Is there a way for me to get this reference somehow? Update: found answer to this question here http://stackoverflow.com/a/1262900/312725 – Max Yankov Jun 12 '12 at 14:19
1

After an NSOperation performed that task to completion, it will be automatically removed from the queue. Ref link.

Make sub-class of NSOperation. Keep an identifier for each operation. Store all the operations in array. When u dont want an operation to continue, just give cancel message to the operation , ie., [_operation cancel];

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
  • Thanks Gomathi for your quick and helpful reply...:) – alloc_iNit Jul 26 '11 at 12:55
  • but how do i get the NSInvocationOperation object to cancel early? I understand i can call the -cancel method on the operation object, but how do i get a reference to the operation object, so that the i can have `if (myOperation.cancelled == true) then exit early` ? – Just a coder Oct 03 '15 at 08:22