#RunIt
RunIt is a super lightweight yet powerful system components manager. It helps to clean your code from unesessary singletons usage. RunIt has an easy to go interface and provides you an opportunity to extend it for your personal needs.
- Thread-safe access for your components
- Generic getters
- Extensibility
- Remove components only by it type
- NSOperation support (see NSOperation support)
- Remove components by component object itself
An example app is included demonstrating RunIt's functionality.
As example, suppose you have an application that uses user location and handles push notifcation. You know that both components available only if user grant access to it. So, no need to lauch and store your location service in singleton if it unavailable.
// Somewhere in app initialization proccess
if locationIsAvailable {
let locationService = LocationComponent()
RunIt.add(locationService)
}
if APNSIsAvailable {
let APNSService = APNSComponent()
RunIt.add(APNSService)
// or
RunIt.add(component: APNSService, forKey: "APNSService")
}
Later, anywhere in your code you have access to your components with generic getters.
let apnsService: LocationComponent? = RunIt.get()
let apnsService: APNSComponent? = RunIt.get(componentForKey: "APNSService")
No need for component anymore? Easy
RunIt.remove(component: component)
RunIt.remove(componentForKey: "APNSService")
All provided actions on RunIt are thread-safe.
Your component may conforms to protocol Runnable
and RunIt will automaticaly runs and stops your component on add
and remove
operations. Furthermore, run
will be executed on separate queue (background by default). You can control this behavior through flags and by customization of runOperationQueue:
RunIt.runComponentsOnAdd = false
RunIt.stopComponentOnRemove = false
RunIt.syncQueue = dispatch_get_main_queue() //Make your compoents run in main thread
RunIt.runOperationQueue.maxConcurrentOperationCount = 2
It's easy to control component's run queue or it priority, just create a custom queue in your component:
class SomeComponent: Runnable {
private(set) public var isRunning: Bool
public var runQueue: dispatch_queue_t? = dispatch_queue_create("YourQueue", DISPATCH_QUEUE_CONCURRENT)
public var priority: NSOperationQueuePriority = .High
...
}
You are always welcome to add custom NSOperation(s) to RunIt.runOperationQueue, but you have to be aware that these operations will not cannibalize a run progress of your components. So, in case of large amount of NSOperation objects it's wise to add them with lower priority than your components used.
To integrate RunIt into your Xcode project using CocoaPods, specify it in your Podfile
:
pod 'RunIt'
Clone the repo and drag files from Sources
folder into your Xcode project.
- Swift 3.0
- iOS 8.0+
- Xcode 7.0+
RunIt is available under the MIT license. See LICENSE for details.