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?
Asked
Active
Viewed 1,792 times
2

Diptesh
- 383
- 1
- 5
- 14
-
Why do you want that? You're blocking the main thread for that 1 second? – Wain Mar 31 '14 at 14:07
-
This call happens on the background thread in my application. – Diptesh Mar 31 '14 at 14:08
-
This is the best logic http://stackoverflow.com/questions/2129794/how-to-log-a-methods-execution-time-exactly-in-milliseconds – iPatel Mar 31 '14 at 14:15
-
if `createDummyData` has iteration nature just check time each iteration – sage444 Mar 31 '14 at 14:19
1 Answers
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