Created
June 3, 2016 13:13
-
-
Save kwylez/493be644722da9acca2164bf867458e3 to your computer and use it in GitHub Desktop.
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
private func listenOnChannel(channel: Firebase, handler: RealTimeDataHandler) -> Void { | |
var initialDataLoaded: Bool = false | |
let firebaseHandleRef: FirebaseHandle = channel.observeEventType(.Value, withBlock: {[unowned self] snapshot in | |
Log.print("listening to the channel from manager and got a response \(snapshot.key) -> \(snapshot.value) and [\(channel.description())]") | |
if !initialDataLoaded { return } | |
if snapshot.key == RealTimeMessageType.Marketing.rawValue { | |
/// Not yet implemented | |
} else { | |
guard let iCloudID: String = SOUser.localICloudUserID() where iCloudID == snapshot.key else { return } | |
if let payload: Dictionary<String, AnyObject> = snapshot.value as? [String: AnyObject] { | |
/// Need to convert the Firebase.description value to a | |
/// NSURL so make it easier to remove the host | |
let firebaseURL: NSURL = NSURL(string: channel.description())! | |
guard let channelPath: String = firebaseURL.path else { return } | |
let range = Range<String.Index>(channelPath.startIndex..<channelPath.startIndex.advancedBy(1)) | |
let channel = channelPath.stringByReplacingOccurrencesOfString("/", withString: "", options: .LiteralSearch, range: range) | |
////// | |
guard let payloadSO: String = payload[Constants.AppConstants.SOPayloadKey] as? String, | |
let payloadTS: NSNumber = payload[Constants.AppConstants.lastTimeStampPayloadKey] as? NSNumber, | |
let payloadType: String = payload[Constants.AppConstants.realTimeMessageTypeKey] as? String, | |
let payloadOrigin: String = payload[Constants.AppConstants.realTimeMessageOriginKey] as? String else { return } | |
let payloadDate: NSDate = NSDate(timeIntervalSince1970: payloadTS.doubleValue) | |
let payloadTypeEnum: RealTimeMessageType = RealTimeMessageType(rawValue: payloadType) | |
let realtimeMessage: RealTimeMessage = RealTimeMessage(from: payloadSO, timestamp: payloadDate, channelName: channel, type: payloadTypeEnum, origin: payloadOrigin) | |
if self.handlers.count > 0 { | |
for dataHandler in self.handlers { | |
if let handler: RealTimeDataHandler = dataHandler as? RealTimeDataHandler { | |
if handler.canProcessMessage(self, channel: channel) { | |
handler.didReceiveMessage(self, message: realtimeMessage) | |
} | |
} | |
} | |
} | |
} | |
} | |
}) | |
/// This makes sure that we don't load the entire database at launch | |
channel.observeSingleEventOfType(.Value, withBlock: {snapshot in | |
initialDataLoaded = true | |
}) | |
handler.handlerRef = firebaseHandleRef | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment