Skip to content

Instantly share code, notes, and snippets.

@s-aska
Last active December 5, 2015 18:27
Show Gist options
  • Save s-aska/817579a7cde6a6d835d0 to your computer and use it in GitHub Desktop.
Save s-aska/817579a7cde6a6d835d0 to your computer and use it in GitHub Desktop.
NSNotificationCenterのラッパー
import Foundation
class Notification {
// MARK: - Singleton
struct Static {
static let instance = Notification()
static let queue = dispatch_queue_create("Notification.Static.instance.cache", DISPATCH_QUEUE_SERIAL)
}
var cache = [UInt:[NSObjectProtocol]]()
// MARK: - addObserverForName
class func on(target: AnyObject, name: String, queue: NSOperationQueue?, handler: ((NSNotification!) -> Void)) {
let id = ObjectIdentifier(target).uintValue()
dispatch_sync(Static.queue) {
let observer = NSNotificationCenter.defaultCenter().addObserverForName(name, object: nil, queue: queue, usingBlock: handler)
if let observers = Static.instance.cache[id] {
Static.instance.cache[id] = observers + [observer]
} else {
Static.instance.cache[id] = [observer]
}
}
}
class func onMainThread(target: AnyObject, name: String, handler: ((NSNotification!) -> Void)) {
Notification.on(target, name: name, queue: NSOperationQueue.mainQueue(), handler: handler)
}
class func onBackgroundThread(target: AnyObject, name: String, handler: ((NSNotification!) -> Void)) {
Notification.on(target, name: name, queue: NSOperationQueue(), handler: handler)
}
// MARK: - removeObserver
class func off(target: AnyObject) {
let id = ObjectIdentifier(target).uintValue()
dispatch_sync(Static.queue) {
if let observers = Static.instance.cache[id] {
for observer in observers {
NSNotificationCenter.defaultCenter().removeObserver(observer)
}
Static.instance.cache.removeValueForKey(id)
}
}
}
// MARK: - postNotificationName
class func post(name: String) {
NSNotificationCenter.defaultCenter().postNotificationName(name, object: nil)
}
class func post(name: String, userInfo: [NSObject : AnyObject]?) {
NSNotificationCenter.defaultCenter().postNotificationName(name, object: nil, userInfo: userInfo)
}
}
import Foundation
import XCTest
class NotificationTests: XCTestCase {
override func setUp() {
super.setUp()
Notification.off(self)
}
override func tearDown() {
Notification.off(self)
super.tearDown()
}
func testOnMainThread() {
Notification.onMainThread(self, name: "testOnMainThread", handler: { _ in
XCTAssertTrue(NSThread.isMainThread())
})
Notification.post("testOnMainThread")
}
func testOnBackgroundThread() {
Notification.onBackgroundThread(self, name: "testOnBackgroundThread", handler: { _ in
XCTAssertTrue(NSThread.isMainThread() == false)
})
Notification.post("testOnBackgroundThread")
}
func testOff() {
var counter = 0
let handler = { (n: NSNotification!) -> Void in
counter++
return
}
Notification.onMainThread(self, name: "testOffCounter", handler: handler)
Notification.post("testOffCounter")
Notification.post("testOffCounter")
Notification.off(self)
Notification.post("testOffCounter")
Notification.post("testOffCounter")
Notification.onMainThread(self, name: "testOffCounter", handler: handler)
Notification.post("testOffCounter")
Notification.post("testOffCounter")
Notification.onMainThread(self, name: "testOffVerify", handler: { _ in
XCTAssertEqual(counter, 4)
})
Notification.post("testOffVerify")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment