Issue #70

  • Today I was browsing through Suas-iOS and the subscription links to life cycle of another object
subscription.linkLifeCycleTo(object: self)

It observes the deinit of another job, interesting approach ๐Ÿ‘ , take a look in https://github.com/zendesk/Suas-iOS/blob/master/Sources/StoreDeinitCallback.swift

var deinitCallbackKey = "DEINITCALLBACK_SUAS"

// MARK: Registartion
extension Suas {

  static func onObjectDeinit(forObject object: NSObject,
                             callbackId: String,
                             callback: @escaping () -> ()) {
    let rem = deinitCallback(forObject: object)
    rem.callbacks.append(callback)
  }

  static fileprivate func deinitCallback(forObject object: NSObject) -> DeinitCallback {
    if let deinitCallback = objc_getAssociatedObject(object, &deinitCallbackKey) as? DeinitCallback {
      return deinitCallback
    } else {
      let rem = DeinitCallback()
      objc_setAssociatedObject(object, &deinitCallbackKey, rem, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
      return rem
    }
  }
}

@objc fileprivate class DeinitCallback: NSObject {
  var callbacks: [() -> ()] = []

  deinit {
    callbacks.forEach({ $0() })
  }
}