QNTransactionManager.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // QNTransactionManager.m
  3. // QiniuSDK
  4. //
  5. // Created by yangsen on 2020/4/1.
  6. // Copyright © 2020 Qiniu. All rights reserved.
  7. //
  8. #import "QNTransactionManager.h"
  9. //MARK: -- 事务对象
  10. typedef NS_ENUM(NSInteger, QNTransactionType){
  11. QNTransactionTypeNormal, // 普通类型事务,事务体仅会执行一次
  12. QNTransactionTypeTime, // 定时事务,事务体会定时执行
  13. };
  14. @interface QNTransaction()
  15. // 事务类型
  16. @property(nonatomic, assign)QNTransactionType type;
  17. // 定时任务执行时间间隔
  18. @property(nonatomic, assign)NSInteger interval;
  19. // 事务延后时间 单位:秒
  20. @property(nonatomic, assign)NSInteger after;
  21. // 事务执行时间 与事务管理者定时器时间相关联
  22. @property(nonatomic, assign)long long actionTime;
  23. // 事务名称
  24. @property(nonatomic, copy)NSString *name;
  25. // 事务执行体
  26. @property(nonatomic, copy)void(^action)(void);
  27. // 下一个需要处理的事务
  28. @property(nonatomic, strong, nullable)QNTransaction *nextTransaction;
  29. @end
  30. @implementation QNTransaction
  31. + (instancetype)transaction:(NSString *)name
  32. after:(NSInteger)after
  33. action:(void (^)(void))action{
  34. QNTransaction *transaction = [[QNTransaction alloc] init];
  35. transaction.type = QNTransactionTypeNormal;
  36. transaction.after = after;
  37. transaction.name = name;
  38. transaction.action = action;
  39. return transaction;
  40. }
  41. + (instancetype)timeTransaction:(NSString *)name
  42. after:(NSInteger)after
  43. interval:(NSInteger)interval
  44. action:(void (^)(void))action{
  45. QNTransaction *transaction = [[QNTransaction alloc] init];
  46. transaction.type = QNTransactionTypeTime;
  47. transaction.after = after;
  48. transaction.name = name;
  49. transaction.interval = interval;
  50. transaction.action = action;
  51. return transaction;
  52. }
  53. - (BOOL)shouldAction:(long long)time{
  54. if (time < self.actionTime) {
  55. return NO;
  56. } else {
  57. return YES;
  58. }
  59. }
  60. - (BOOL)maybeCompleted:(long long)time{
  61. if (time >= self.actionTime && self.type == QNTransactionTypeNormal) {
  62. return YES;
  63. } else {
  64. return NO;
  65. }
  66. }
  67. - (void)handleAction:(long long)time{
  68. if (![self shouldAction:time]) {
  69. return;
  70. }
  71. if (self.action) {
  72. self.action();
  73. }
  74. if (self.type == QNTransactionTypeNormal) {
  75. self.actionTime = 0;
  76. } else if(self.type == QNTransactionTypeTime) {
  77. self.actionTime = time + self.interval;
  78. }
  79. }
  80. @end
  81. //MARK: -- 事务链表
  82. @interface QNTransactionList : NSObject
  83. @property(nonatomic, strong)QNTransaction *header;
  84. @end
  85. @implementation QNTransactionList
  86. - (BOOL)isEmpty{
  87. if (self.header == nil) {
  88. return YES;
  89. } else {
  90. return NO;
  91. }
  92. }
  93. - (NSArray <QNTransaction *> *)transactionsForName:(NSString *)name{
  94. NSMutableArray *transactions = [NSMutableArray array];
  95. [self enumerate:^(QNTransaction *transaction, BOOL * _Nonnull stop) {
  96. if ([transaction.name isEqualToString:name]) {
  97. [transactions addObject:transaction];
  98. }
  99. }];
  100. return [transactions copy];
  101. }
  102. - (void)enumerate:(void(^)(QNTransaction *transaction, BOOL * _Nonnull stop))handler {
  103. if (!handler) {
  104. return;
  105. }
  106. BOOL isStop = NO;
  107. QNTransaction *transaction = self.header;
  108. while (transaction && !isStop) {
  109. handler(transaction, &isStop);
  110. transaction = transaction.nextTransaction;
  111. }
  112. }
  113. - (void)add:(QNTransaction *)transaction{
  114. @synchronized (self) {
  115. QNTransaction *transactionP = self.header;
  116. while (transactionP.nextTransaction) {
  117. transactionP = transactionP.nextTransaction;
  118. }
  119. if (transactionP) {
  120. transactionP.nextTransaction = transaction;
  121. } else {
  122. self.header = transaction;
  123. }
  124. }
  125. }
  126. - (void)remove:(QNTransaction *)transaction{
  127. @synchronized (self) {
  128. QNTransaction *transactionP = self.header;
  129. QNTransaction *transactionLast = nil;
  130. while (transactionP) {
  131. if (transactionP == transaction) {
  132. if (transactionLast) {
  133. transactionLast.nextTransaction = transactionP.nextTransaction;
  134. } else {
  135. self.header = transactionP.nextTransaction;
  136. }
  137. break;
  138. }
  139. transactionLast = transactionP;
  140. transactionP = transactionP.nextTransaction;
  141. }
  142. }
  143. }
  144. - (BOOL)has:(QNTransaction *)transaction{
  145. @synchronized (self) {
  146. __block BOOL has = NO;
  147. [self enumerate:^(QNTransaction *transactionP, BOOL * _Nonnull stop) {
  148. if (transaction == transactionP) {
  149. has = YES;
  150. *stop = YES;
  151. }
  152. }];
  153. return has;
  154. }
  155. }
  156. - (void)removeAll{
  157. @synchronized (self) {
  158. self.header = nil;
  159. }
  160. }
  161. @end
  162. //MARK: -- 事务管理者
  163. @interface QNTransactionManager()
  164. // 事务处理线程
  165. @property(nonatomic, strong)NSThread *thread;
  166. // 事务链表
  167. @property(nonatomic, strong)QNTransactionList *transactionList;
  168. // 定时器执行次数
  169. @property(nonatomic, assign)long long time;
  170. // 事务定时器
  171. @property(nonatomic, strong)NSTimer *timer;
  172. @end
  173. @implementation QNTransactionManager
  174. + (instancetype)shared{
  175. static QNTransactionManager *manager = nil;
  176. static dispatch_once_t onceToken;
  177. dispatch_once(&onceToken, ^{
  178. manager = [[QNTransactionManager alloc] init];
  179. });
  180. return manager;
  181. }
  182. - (instancetype)init{
  183. if (self = [super init]) {
  184. _time = 0;
  185. _transactionList = [[QNTransactionList alloc] init];
  186. }
  187. return self;
  188. }
  189. - (NSArray <QNTransaction *> *)transactionsForName:(NSString *)name{
  190. return [self.transactionList transactionsForName:name];
  191. }
  192. - (BOOL)existtransactionsForName:(NSString *)name{
  193. NSArray *transactionList = [self transactionsForName:name];
  194. if (transactionList && transactionList.count > 0) {
  195. return YES;
  196. } else {
  197. return NO;
  198. }
  199. }
  200. - (void)addTransaction:(QNTransaction *)transaction{
  201. transaction.actionTime = self.time + transaction.after;
  202. [self.transactionList add:transaction];
  203. [self createThread];
  204. }
  205. - (void)removeTransaction:(QNTransaction *)transaction{
  206. BOOL canDestroyResource = NO;
  207. [self.transactionList remove:transaction];
  208. if ([self.transactionList isEmpty]) {
  209. canDestroyResource = YES;
  210. }
  211. //
  212. // if (canDestroyResource) {
  213. // [self destroyResource];
  214. // }
  215. }
  216. - (void)performTransaction:(QNTransaction *)transaction{
  217. if (!transaction) {
  218. return;
  219. }
  220. @synchronized (self) {
  221. if (![self.transactionList has:transaction]) {
  222. [self.transactionList add:transaction];
  223. }
  224. transaction.actionTime = self.time;
  225. }
  226. }
  227. /// 销毁资源
  228. - (void)destroyResource{
  229. @synchronized (self) {
  230. [self invalidateTimer];
  231. [self.thread cancel];
  232. self.thread = nil;
  233. [self.transactionList removeAll];
  234. }
  235. }
  236. //MARK: -- handle transaction action
  237. - (void)handleAllTransaction{
  238. [self.transactionList enumerate:^(QNTransaction *transaction, BOOL * _Nonnull stop) {
  239. [self handleTransaction:transaction];
  240. if ([transaction maybeCompleted:self.time]) {
  241. [self removeTransaction:transaction];
  242. }
  243. }];
  244. }
  245. - (void)handleTransaction:(QNTransaction *)transaction{
  246. [transaction handleAction:self.time];
  247. }
  248. //MARK: -- thread
  249. - (void)createThread{
  250. @synchronized (self) {
  251. if (self.thread == nil) {
  252. __weak typeof(self) weakSelf = self;
  253. self.thread = [[NSThread alloc] initWithTarget:weakSelf
  254. selector:@selector(threadHandler)
  255. object:nil];
  256. self.thread.name = @"com.qiniu.transaction";
  257. [self.thread start];
  258. }
  259. }
  260. }
  261. - (void)threadHandler{
  262. @autoreleasepool {
  263. if (self.timer == nil) {
  264. [self createTimer];
  265. }
  266. NSThread *thread = [NSThread currentThread];
  267. while (thread && !thread.isCancelled) {
  268. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
  269. beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
  270. }
  271. }
  272. }
  273. //MARK: -- timer
  274. - (void)createTimer{
  275. __weak typeof(self) weakSelf = self;
  276. NSTimer *timer = [NSTimer timerWithTimeInterval:1
  277. target:weakSelf
  278. selector:@selector(timerAction)
  279. userInfo:nil
  280. repeats:YES];
  281. [[NSRunLoop currentRunLoop] addTimer:timer
  282. forMode:NSDefaultRunLoopMode];
  283. [self timerAction];
  284. _timer = timer;
  285. }
  286. - (void)invalidateTimer{
  287. [self.timer invalidate];
  288. self.timer = nil;
  289. }
  290. - (void)timerAction{
  291. self.time += 1;
  292. [self handleAllTransaction];
  293. }
  294. @end