Skip to content

Instantly share code, notes, and snippets.

@loganwright
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save loganwright/e85deeda7ed6ba5a20ab to your computer and use it in GitHub Desktop.

Select an option

Save loganwright/e85deeda7ed6ba5a20ab to your computer and use it in GitHub Desktop.
"Real Time" Data - WatchKit

#Add To Project

Make sure to set the target to your app and to the WatchKit Extension

#Setup -- APP

In applicationDidFinishLaunching

Swift:

LGWatchKitListener.setGroupContainerIdentifier("group.<#YourGroupIdentifier#>")

ObjC:

[LGWatchKitListener setGroupContainerIdentifier:@"group.<#YourGroupIdentifier#>"];

#Setup -- WatchKit Extension

In initWithContext

Swift:

LGWatchKitListener.setGroupContainerIdentifier("group.<#YourGroupIdentifier#>")

ObjC:

[LGWatchKitListener setGroupContainerIdentifier:@"group.<#YourGroupIdentifier#>"];

#Listen

Swift:

LGWatchKitListener.startListeningForKey("test", withCallback: { (value, key) -> Void in
    println("Value: \(value) updatedForKey: \(key)")
})

ObjC:

[LGWatchKitListener startListeningForKey:@"test" withCallback: (id value, NSString *key)^{
    NSLog(@"Value: %@ updatedForKey: %@", value, key);
}];
//
// LGWatchKitListener.swift
// RemoteCamera
//
// Created by Logan Wright on 11/19/14.
// Copyright (c) 2014 lowriDevs. All rights reserved.
//
import UIKit
// MARK: Listener Ref
class LGListenerRef : NSObject {
var lastValue: NSObject?
let callback: (value: NSObject?, key: String) -> Void
init(lastValue: NSObject?, callback: (value: NSObject?, key: String) -> Void) {
self.lastValue = lastValue
self.callback = callback
}
}
// MARK: Listener
class LGWatchKitListener: NSObject {
// MARK: Identifier
class func setGroupContainerIdentifier(groupContainerIdentifier: String) {
Constants.GroupContainerIdentifier = groupContainerIdentifier
}
private struct Constants {
static var GroupContainerIdentifier: String = ""
}
// MARK: Defaults Loader
private lazy var defaults: NSUserDefaults = {
if countElements(Constants.GroupContainerIdentifier) == 0 {
fatalError("LGWatchKitListener: GroupContainerIdentifier Not Set Before Initializing Listener! You should call LGWatchKitListener.setGroupContainerIdentifier in your AppDelegate in appDidLoad")
}
return NSUserDefaults(suiteName: Constants.GroupContainerIdentifier)!
}()
// MARK: State Management
private enum State {
case Listening, Pending
}
// MARK: Private Properties
private var state: State = .Pending
private var listenerRefDictionary: [String : LGListenerRef] = [:]
private var timer: NSTimer?
// MARK: Singleton
private class func sharedListener() -> LGWatchKitListener {
struct Singleton {
static let instance = LGWatchKitListener()
}
return Singleton.instance
}
// MARK: Listening Interaction
class func startListeningForKey(key: String, withCallback callback: (value: NSObject?, key: String) -> Void) {
self.sharedListener().listenerRefDictionary[key] = LGListenerRef(lastValue: nil, callback: callback)
self.startListening()
}
class func stopListeningForKey(key: String) {
self.sharedListener().listenerRefDictionary.removeValueForKey(key)
}
class func startListening() {
if self.sharedListener().state == .Pending {
self.sharedListener().state = .Listening
self.sharedListener().timer?.invalidate()
self.sharedListener().timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self.sharedListener(), selector: "timerFired:", userInfo: nil, repeats: true)
}
}
class func stopListening() {
self.sharedListener().timer?.invalidate()
self.sharedListener().state = .Pending
}
class func removeAllListeningKeys() {
self.stopListening()
self.sharedListener().listenerRefDictionary = [:]
}
// MARK: Timer Handling
func timerFired(timer: NSTimer) {
for (key, listenerRef) in self.listenerRefDictionary {
let currentValue: NSObject? = self.defaults.objectForKey(key) as? NSObject
if currentValue != listenerRef.lastValue {
self.listenerRefDictionary[key]?.lastValue = currentValue
listenerRef.callback(value: currentValue, key: key)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment