Skip to content

Commit

Permalink
Merge pull request #405 from YangSen-qn/develop
Browse files Browse the repository at this point in the history
optimize transaction manager
  • Loading branch information
xwen-winnie authored Nov 12, 2021
2 parents 71fa6e1 + ab2d872 commit 84b1f00
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions QiniuSDK/Transaction/QNTransactionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ @interface QNTransaction()
@property(nonatomic, assign)double createTime;
// 执行次数
@property(nonatomic, assign)long executedCount;
// 下次执行时的时间戳
@property(nonatomic, assign)double nextExecutionTime;

// 事务名称
@property(nonatomic, copy)NSString *name;
Expand All @@ -47,6 +49,7 @@ + (instancetype)transaction:(NSString *)name
transaction.action = action;
transaction.executedCount = 0;
transaction.createTime = [[NSDate date] timeIntervalSince1970];
transaction.nextExecutionTime = transaction.createTime + after;
return transaction;
}

Expand All @@ -62,15 +65,16 @@ + (instancetype)timeTransaction:(NSString *)name
transaction.action = action;
transaction.executedCount = 0;
transaction.createTime = [[NSDate date] timeIntervalSince1970];
transaction.nextExecutionTime = transaction.createTime + after;
return transaction;
}

- (BOOL)shouldAction {
double currentTime = [[NSDate date] timeIntervalSince1970];
if (self.type == QNTransactionTypeNormal) {
return self.executedCount < 1 && (currentTime - self.createTime) >= self.after;
return self.executedCount < 1 && currentTime >= self.nextExecutionTime;
} else if (self.type == QNTransactionTypeTime) {
return (currentTime - self.createTime) >= (self.executedCount * self.interval + self.after);
return currentTime >= self.nextExecutionTime;
} else {
return NO;
}
Expand All @@ -91,8 +95,9 @@ - (void)handleAction {
return;
}
if (self.action) {
self.executedCount += 1;
_isExecuting = YES;
self.executedCount += 1;
self.nextExecutionTime = [[NSDate date] timeIntervalSince1970] + self.interval;
self.action();
_isExecuting = NO;
}
Expand Down

0 comments on commit 84b1f00

Please sign in to comment.