2

I have a function call as below

NSData *data = [self createDummyData];

I want function the createDummyData to break and return nil if it takes execution time of more than 1 second. How can i achieve it?

Diptesh
  • 383
  • 1
  • 5
  • 14

1 Answers1

3
  • Create an NSOperationQueue.
  • Create a Timer.
  • Add an operation to the queue that does what you want it to do.
  • Start the timer with a fire date of 1 second in the future
  • When the timer fires, cancel the operations in the queue.

If the operation has finished, cancelling will have no effect. If it hasn't finished, and you've correctly configured the operation, then the operation will stop.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • If the operation is currently executing the operation is not cancelled. How do we cancel it ? – Diptesh Apr 01 '14 at 12:50
  • You need to put hooks into your NSOperation subclass to check for the `isCancelled` event and deal with it that way. – Abizern Apr 01 '14 at 13:00