Last active
June 14, 2018 14:39
-
-
Save suatkarakusoglu/e61e1676cb8f00c0330ea92888271baa to your computer and use it in GitHub Desktop.
ZMLocalBroadcastObserver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ZMLocalBroadcastObserver.swift | |
// Zamekan | |
// | |
// Created by Suat Karakusoglu on 11.06.2018. | |
// Copyright © 2018 suat.karakusoglu. All rights reserved. | |
// | |
import Foundation | |
class ZMLocalBroadcastObserver | |
{ | |
var broadcastObserverUID: String = UUID().uuidString | |
var broadcastType: ZMLocalBroadcastType | |
var broadcastReceivedBlock: ZMBroadcastReceivedBlock | |
init(broadcastType: ZMLocalBroadcastType, broadcastReceived: @escaping ZMBroadcastReceivedBlock) | |
{ | |
self.broadcastType = broadcastType | |
self.broadcastReceivedBlock = broadcastReceived | |
} | |
func startObserving() | |
{ | |
NotificationCenter.default.addObserver(forName: self.broadcastType.createNotificationName(), object: nil, queue: nil) { (notification) in | |
debugPrint("Received broadcast data: \(notification.userInfo?.description ?? " none") on observer observer uid: \(self.broadcastObserverUID)") | |
self.broadcastReceivedBlock(notification.userInfo) | |
} | |
} | |
func stopObserving() | |
{ | |
NotificationCenter.default.removeObserver( | |
self, | |
name: self.broadcastType.createNotificationName(), | |
object: nil | |
) | |
debugPrint("Stopped observing via observer uid \(self.broadcastObserverUID)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment