@@ -26,6 +26,8 @@ @interface QNTransaction()
2626@property (nonatomic , assign )double createTime;
2727// 执行次数
2828@property (nonatomic , assign )long executedCount;
29+ // 下次执行时的时间戳
30+ @property (nonatomic , assign )double nextExecutionTime;
2931
3032// 事务名称
3133@property (nonatomic , copy )NSString *name;
@@ -47,6 +49,7 @@ + (instancetype)transaction:(NSString *)name
4749 transaction.action = action;
4850 transaction.executedCount = 0 ;
4951 transaction.createTime = [[NSDate date ] timeIntervalSince1970 ];
52+ transaction.nextExecutionTime = transaction.createTime + after;
5053 return transaction;
5154}
5255
@@ -62,15 +65,16 @@ + (instancetype)timeTransaction:(NSString *)name
6265 transaction.action = action;
6366 transaction.executedCount = 0 ;
6467 transaction.createTime = [[NSDate date ] timeIntervalSince1970 ];
68+ transaction.nextExecutionTime = transaction.createTime + after;
6569 return transaction;
6670}
6771
6872- (BOOL )shouldAction {
6973 double currentTime = [[NSDate date ] timeIntervalSince1970 ];
7074 if (self.type == QNTransactionTypeNormal) {
71- return self.executedCount < 1 && ( currentTime - self. createTime ) >= self.after ;
75+ return self.executedCount < 1 && currentTime >= self.nextExecutionTime ;
7276 } else if (self.type == QNTransactionTypeTime) {
73- return ( currentTime - self. createTime ) >= ( self.executedCount * self. interval + self. after ) ;
77+ return currentTime >= self.nextExecutionTime ;
7478 } else {
7579 return NO ;
7680 }
@@ -91,8 +95,9 @@ - (void)handleAction {
9195 return ;
9296 }
9397 if (self.action ) {
94- self.executedCount += 1 ;
9598 _isExecuting = YES ;
99+ self.executedCount += 1 ;
100+ self.nextExecutionTime = [[NSDate date ] timeIntervalSince1970 ] + self.interval ;
96101 self.action ();
97102 _isExecuting = NO ;
98103 }
0 commit comments