Initialization of the MasterController (iOS/Swift)
Always begin by calling the StartEventEx function from the MasterController SDK.
Develop an Adapter class to handle communication between your application and the MasterController SDK.
MasterControllerHandler
class MasterControllerAdapter: KSAsyncEventReceiver{
static let sharedInstance: MasterControllerAdapter = {
var sharedInstance = MasterControllerAdapter()
return sharedInstance
}()
private var eventListeners = [KSAsyncEventReceiver]()
private var latestEvents = [KsEvent]()
var masterController: (KSAsyncEventReceiver & KSEcoModulInterface & KSEventSource)?
// Initialize Master controller
func initializeMasterController() -> (KSAsyncEventReceiver &
KSEcoModulInterface &
KSEventSource) {
if masterController == nil {
let environment = ProcessInfo.processInfo.environment
if environment["ISTEST"] == nil {
masterController = KSMasterControllerFactory.getMasterController(
withParmeter: "",
andConsumer: self)
}
}
return masterController!
}
// Register your class so that you will get a notification when new events is received from master controller
func register(forEvents: KSAsyncEventReceiver) {
let serialQueue = DispatchQueue(label: "com.kobil.register")
serialQueue.sync {
if eventListeners.count > 0{
for event in eventListeners {
if (event as AnyObject === forEvents as AnyObject){
return
}
}
}
eventListeners.append(forEvents)
}
}
// Once your task is done make sure to deregister your class so that it will not get any notification.
func deregister(forEvents: KSAsyncEventReceiver?) {
let serialQueue = DispatchQueue(label: "com.kobil.deregister")
serialQueue.sync {
if forEvents == nil {
return
}
eventListeners.removeAll { $0 as AnyObject === forEvents as AnyObject }
}
}
// Received events from master controller
func receive(_ event: KsEvent, withCompletionHandler completionBlock: ((KsEvent?) -> Void)? = nil) {
let macroEvent = event as? KSMEvent
print("Tag: received event \(type(of: macroEvent))")
let serialQueue = DispatchQueue(label: "com.kobil.distributeEvents")
serialQueue.sync {
for entry in eventListeners {
entry.receive(event, withCompletionHandler: { _ in })
}
}
}
// Send events to master controller
func sendEvent2MasterController(_ event: KsEvent,
withCompletionHandler completionBlock:
((KsEvent?) -> Void)? = nil){
self.masterController?.receive(event, withCompletionHandler:
{ (event) in
completionBlock?(event)
})
}
}