I have the following case, I need to delete a specific observer for the firebase database reference, so I made a code that looks something like it looks to the NotificationCenter. In various managers I used NSMapTable in order to keep the weak references on UIViewControllers and that controller served as the key to the reference handle. In most UIViewControllers I removed observers in viewDidDisappear, but in some I need to delete it in deinit, for this I used runtime and added two arrays (DatabaseQueue's array and DatabaseReferences' array) as checked in the UIViewController extension.Please tell me how can I improve this code? Thanks.private var queuesKey = 0private var referencesKey = 1extension UIViewController {    var firQueues: [DatabaseQuery]? {        get {            return objc_getAssociatedObject(self, &queuesKey) as? [DatabaseQuery]        }        set {            objc_setAssociatedObject(self, &queuesKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)        }    }    var firReferences: [DatabaseReference]? {        get {            return objc_getAssociatedObject(self, &referencesKey) as? [DatabaseReference]        }        set {            objc_setAssociatedObject(self, &referencesKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)        }    }    func removeFIRQueuesReferensesObservers() {        if var _firQueues = firQueues {            for firQueqry in _firQueues {                firQueqry.removeAllObservers()            }            _firQueues.removeAll()            firQueues?.removeAll()            debugPrint(_firReferences, _firQueues.count, firReferences, firQueues?.count ?? nil)        }        if var _firReferences = firReferences {            for firRef in _firReferences {                firRef.removeAllObservers()            }            _firReferences.removeAll()            firReferences?.removeAll()            debugPrint(_firReferences, _firReferences.count, firReferences, firReferences?.count ?? nil)        }    }}snippet code from one of the managersclass FirebaseUsersManager {    private var userDeletingObservers = NSMapTable<AnyObject, AnyObject>(keyOptions: .weakMemory, valueOptions: .strongMemory)    func observeUserDeleting(observer: Any, isObserve: Bool, userID: String, didDeleted: (() -> Void)?, fail: ((_ error: Error) -> Void)?) {        let ref = Database.database().reference().child(MainGateways.users.rawValue).child(userID)        if !isObserve {            guard let handle = userDeletingObservers.object(forKey: observer as AnyObject) as? UInt else { return }            ref.removeObserver(withHandle: handle)            userDeletingObservers.removeObject(forKey: observer as AnyObject)            return        }        if let observableVC = observer as? UIViewController {            if observableVC.firReferences != nil {                observableVC.firReferences?.append(ref)            } else {                observableVC.firReferences = [ref]            }        }        guard userDeletingObservers.object(forKey: observer as AnyObject) as? UInt == nil else { return }        DispatchQueue.global(qos: .background).async {            var handle: UInt = 0            handle = ref.observe(.value, with: { (snap) in                if snap.value is NSNull {                    didDeleted?()                    RealmUsersManager().deleteUser(userID)                    ref.removeObserver(withHandle: handle)                    return                }            }, withCancel: { (error) in                fail?(error)            })            self.userDeletingObservers.setObject(handle as AnyObject, forKey: observer as AnyObject)        }    }}
Runtime function in Swift
swift;objective c runtime