[英]NSOperationQueue with a timer
I would like to use an NSOperationQueue
which has got a timer inside it. 我想使用一个NSOperationQueue
,里面有一个计时器。 For example - I download 1 element (complete the 1st NSOperation
) then I want to wait for 3 seconds before the compiler goes on to the next operation. 例如 - 我下载1个元素(完成第1个NSOperation
)然后我想等待3秒钟, 然后编译器继续下一个操作。 After the 2nd NSOperation
has been completed I want it to wait for the same 3 seconds and then start the operation number 3. 在完成第二次NSOperation
之后,我希望它等待相同的3秒,然后开始操作编号3。
How can this behaviour be implemented? 如何实现这种行为? I have no prior experience of using NSTimer
or NSRunLoop
and I'm unsure whether I should use them at all. 我以前没有使用NSTimer
或NSRunLoop
经验,我不确定是否应该使用它们。
Thanks in advance. 提前致谢。
As long as the operations are executed in background thread; 只要操作在后台线程中执行;
You can set the maxConcurrentOperationCount to 1 and use the sleep(3) for 3 seconds in your operation block. 您可以将maxConcurrentOperationCount设置为1,并在操作块中使用sleep(3)3秒。
使用sleep(int secondsToSleep);
Using sleep and maxConcurrentOperationCount is all you need if you just want to wait for 3 seconds after your operation does something. 如果您只是想在操作执行某些操作后等待3秒钟,则可以使用sleep和maxConcurrentOperationCount。
If you need something more complex you could use timeIntervalSinceDate in a while loop. 如果您需要更复杂的东西,可以在while循环中使用timeIntervalSinceDate。 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. 如果您的操作执行某些可能需要不确定时间的处理(在我的情况下我需要运行远程创建帐户进程)并且您希望至少等待,或者最多等待X秒,然后再运行下一个操作,这将非常有用。队列。 You can make subsequent NSOperations dependent on completion of a previous operation. 您可以使后续NSOperations依赖于先前操作的完成。
Use addDependency to sequence NSOperation to wait until previous operation completed : 使用addDependency对NSOperation进行排序,以等待上一个操作完成:
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 在NSOperation子类中,主要检查操作是否已完成或是否花费太长时间
#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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.