Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Known issue with CBCentralManager initialization #610

Closed
pilmen opened this issue Mar 26, 2019 · 5 comments
Closed

Known issue with CBCentralManager initialization #610

pilmen opened this issue Mar 26, 2019 · 5 comments

Comments

@pilmen
Copy link

pilmen commented Mar 26, 2019

@ #602 When try to init CBCentralManager with option CBCentralManagerOptionRestoreIdentifierKey (CBCentralManager(delegate: <CBCentralManagerDelegate?>, queue: <DispatchQueue?>, options: <[String : Any]?>)) you getting crash:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '<CBCentralManager: 0x600002a73c40> has provided a restore identifier but the delegate doesn't implement the centralManager:willRestoreState: method'
I believe it happens because at the moment of initialization CBCentralManager calling delegate method centralManager:willRestoreState: if this key present. But we don't actually have delegate passed as a parameter at time of initialization. @tonyarnold @srdanrasic Do you have any suggestions how can I fix it? Thanks in advance.

@pilmen pilmen changed the title Known issue Known issue with CBCentralManager initialization Mar 26, 2019
@srdanrasic
Copy link
Contributor

srdanrasic commented Mar 26, 2019

Hey @pilmen,

First of all, thank you for that PR. The reason I haven't merged it yet is because it would introduce dependency on CoreBluetooth which most Bond users don't use. I'm still looking into ways to resolve that.

To answer this question: Could you have a dummy delegate during the initialization that implements that method?

@srdanrasic
Copy link
Contributor

srdanrasic commented Mar 26, 2019

You probably want something like this:

extension ReactiveExtensions where Base: NSObject {

    public static func makeWithProtocolProxy<P>(for `protocol`: Protocol, keyPath: ReferenceWritableKeyPath<Base, P?>, make: @escaping (P) -> Base) -> Base {
        let controller = OptionalKeyPathProtocolProxyPropertyController(object: nil, keyPath: keyPath)
        let protocolProxy = ProtocolProxy(protocol: `protocol`, propertyController: controller)
        let object = make(protocolProxy as! P)
        controller._object = object
        object.protocolProxies[controller] = protocolProxy
        return object
    }
}

so that you can do

let manager = CBCentralManager.reactive.makeWithProtocolProxy(for: CBCentralManagerDelegate.self, keyPath: \.delegate) { (delegate) -> CBCentralManager in
        return CBCentralManager(delegate: delegate, queue: nil, options: [:])
    }

Note that this will not work out of the box because KeyPathProtocolProxyPropertyController can't be initialized with nil object at the moment so you'll have to change that. If you want, please experiment, and if you get it working with a clean solution, do make a PR.

@pilmen
Copy link
Author

pilmen commented Mar 27, 2019

Hey @pilmen,

First of all, thank you for that PR. The reason I haven't merged it yet is because it would introduce dependency on CoreBluetooth which most Bond users don't use. I'm still looking into ways to resolve that.

To answer this question: Could you have a dummy delegate during the initialization that implements that method?

Hey @srdanrasic , yes dummy delegate works in this case. I'll try to work with the approach from your last message. I'm not sure either if this extension should to be a part of main Bond Framework. Thanks for your help and great job around this framework.

@srdanrasic
Copy link
Contributor

I will close this for now. Feel free to reopen if there will be more stuff on this.

@pilmen
Copy link
Author

pilmen commented Apr 5, 2019

Sure, I didn't have much time to look into this. If I'll be have solution for this I let you know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants