AsyncTimer is a lightweight, easy-to-use library that provides asynchronous timer functionality in your iOS and macOS projects. It allows you to execute code blocks or methods at specific intervals, without blocking the main thread, ensuring smooth performance of your application.
Features
- Asynchronous timer functionality for iOS and macOS
- Execute code blocks or methods at specific intervals
- Does not block the main thread for smooth performance
- Option to repeat the timer or execute it only once
- Flexible scheduling options with customizable time intervals
- Pause, resume, and reset the timer as needed
- Simple and intuitive API for easy integration
- Lightweight library with minimal overhead
- Compatible with both Objective-C and Swift
- Well-documented with comprehensive code examples
Installation
Using CocoaPods
// Add this line to your Podfile
pod 'AsyncTimer'
Manual Installation
- Download the AsyncTimer source files from the official GitHub repository: https://github.com/username/AsyncTimer
- Drag and drop the AsyncTimer folder into your Xcode project
- Make sure to check the “Copy items if needed” option
Usage
Importing AsyncTimer
import AsyncTimer
Creating an AsyncTimer instance
// Swift
let timer = AsyncTimer(interval: 1.0, repeats: true) {
// Code block or method to execute
print("Timer fired!")
}
// Objective-C
AsyncTimer *timer = [[AsyncTimer alloc] initWithInterval:1.0 repeats:YES block:^{
// Code block or method to execute
NSLog(@"Timer fired!");
}];
Starting the timer
timer.start()
Pausing and resuming the timer
// Pausing the timer
timer.pause()
// Resuming the timer
timer.resume()
Resetting the timer
timer.reset()
Stopping the timer
timer.stop()
Handling timer events
To handle timer events, you can either specify a code block directly during the timer creation or assign a method to be called using a selector:
// Swift
let timer = AsyncTimer(interval: 2.5, repeats: true, block: {
// Code block to execute
print("Timer fired!")
})
// Objective-C
AsyncTimer *timer = [[AsyncTimer alloc] initWithInterval:2.5 repeats:YES block:^{
// Code block to execute
NSLog(@"Timer fired!");
}];
// Alternatively, you can use a selector
timer.target = self
timer.selector = #selector(timerFired)
@objc func timerFired() {
// Method to execute
print("Timer fired!")
}
Example
// Swift
import AsyncTimer
func startTimer() {
let timer = AsyncTimer(interval: 3.0, repeats: true) {
// Code block or method to execute
print("Timer fired!")
}
timer.start()
}
AsyncTimer provides a reliable and efficient way to incorporate asynchronous timer functionality in your iOS and macOS projects. Whether you need to execute code blocks at specific intervals or perform recurring tasks, AsyncTimer makes it simple and intuitive to achieve without impacting the performance of your application.