iPhone - a delay between operations in the queue

后端 未结 4 1851
挽巷
挽巷 2021-02-03 15:20

I am adding operations to the queue using something like this

NSInvocationOperation *operation0 = [[NSInvocationOperation alloc] 
initWithTarget:self
selector:@s         


        
4条回答
  •  囚心锁ツ
    2021-02-03 15:25

    Without extra code, you can't really guarantee that your NSOperations will run in serial - if Apple release a multiprocessor iOS device, it will run multiple operations in parallel. So firstly, you should set up the dependency chain between your operations. I would suggest that you achieve this using NSOperation's - (void)addDependency:(NSOperation *)operation. You could also roll your own fancy locking code...

    You can then add a sleep(5) to the end of each of your doStuff methods. If you need the sleep outside the doStuff methods, create a sleep NSOperation:

    - (void)sleepOperationBody; {
    sleep(0.5f);
    }
    
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] 
    initWithTarget:self
    selector:@selector(sleepOperationBody) 
    object:nil];
    

    That said, without knowing in more detail what you need to happen, I think you perhaps just need one NSOperation that combines all three tasks and puts a sleep between them. That would certainly be simpler code.

提交回复
热议问题