STWeakTimer is an alternative to NSTimer, it does not cause the retain cycle, it is thread-safe.
- iOS 8.0 or later (For iOS 8.0 before, maybe it can work, but I have not tested.)
- ARC
STWeakTimer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'STWeakTimer'
In the source files where you need to use the library, import the header file:
#import <STWeakTimer/STWeakTimer.h>
Initialize a STWeakTimer, you can use a selector or block as the callback. You can also set the thread to execute the callback:
STWeakTimer *timer = [STWeakTimer scheduledTimerWithTimeInterval:1 userInfo:nil repeats:YES dispatchQueue:dispatch_get_main_queue() handler:^(STWeakTimer * _Nullable timer) {
NSLog(@"Timer triggered");
}];
STWeakTimer supports pause and resume:
[timer pause];
[timer resume];
Suta, shien7654321@163.com