-
Notifications
You must be signed in to change notification settings - Fork 0
/
DistributedNotificationCenter.swift
38 lines (29 loc) · 1.44 KB
/
DistributedNotificationCenter.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
//
// DistributedNotificationsObserver.swift
//
// Created by N.A Shashank on 08/03/18.
//
import UIKit
public final class DistributedNotificationCenter{
public class func registerObservers(arrNotificationName:[String],observer:AnyObject) {
let observer = UnsafeRawPointer(Unmanaged.passUnretained(observer).toOpaque())
arrNotificationName.forEach { (name) in
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer, DistributedNotificationCenter.callBack(),name as CFString,nil, CFNotificationSuspensionBehavior.deliverImmediately)
}
}
private class func callBack() -> CFNotificationCallback {
return { (center,observer,name,object,userInfo) in
guard let unwrappedName = name else{
return
}
NotificationCenter.default.post(name: NSNotification.Name(String(describing: unwrappedName.rawValue)), object: nil)
}
}
class func removeObservers(observer:UIViewController) {
let observer = UnsafeRawPointer(Unmanaged.passUnretained(observer).toOpaque())
CFNotificationCenterRemoveEveryObserver(CFNotificationCenterGetDarwinNotifyCenter(), observer)
}
class func postNotification(name:String) {
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFNotificationName.init(rawValue: name as CFString), nil, nil, true)
}
}