Using sleep and maxConcurrentOperationCount is all you need if you just want to wait for 3 seconds after your operation does something.
If you need something more complex you could use timeIntervalSinceDate in a while loop. This is useful if your operation does some processing that can take an indeterminate time (in my case I need to run a remote create account process) and you want to wait at least, or at most, X seconds before running the next operation in the queue. You can make subsequent NSOperations dependent on completion of a previous operation.
Use addDependency to sequence NSOperation to wait until previous operation completed :
NSOperationQueue *ftfQueue = [NSOperationQueue new];
// Does this user have an account?
// createFTFAccount is subclass of NSOperation.
FTFCreateAccount *createFTFAccount = [[FTFCreateAccount alloc]init];
[createFTFAccount setUserid:@"a-user-id"];
[ftfQueue addOperation:createFTFAccount];
// postFTFRoute subclass NSOperation
FTFPostRoute *postFTFRoute = [[FTFPostRoute alloc]init];
// Add postFTFRoute with a dependency on Account Creation having finished
[postFTFRoute addDependency:createFTFAccount];
[ftfQueue addOperation:postFTFRoute];
In the NSOperation subclass main check if the operation has finished or if its taking too long
#import <Foundation/Foundation.h>
@interface FTFCreateAccount : NSOperation
@property (strong,nonatomic) NSString *userid;
@end
@implementation FTFCreateAccount
{
NSString *_accountCreationStatus;
}
- (void)main {
NSDate *startDate = [[NSDate alloc] init];
float timeElapsed;
.....
.....
.....
// Hold it here until a reply comes back from the account creation process
// Or the process is taking too long
while ((!_accountCreationStatus) && (timeElapsed < 3.0)) {
NSDate *currentDate = [[NSDate alloc] init];
timeElapsed = [currentDate timeIntervalSinceDate:startDate];
}
// Code here to do something dependent on _accountCreationStatus value
}