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

BluetoothManager deinit #156

Closed
taraskontsur opened this issue Oct 12, 2017 · 3 comments
Closed

BluetoothManager deinit #156

taraskontsur opened this issue Oct 12, 2017 · 3 comments

Comments

@taraskontsur
Copy link

Hi, guys. There are retain cycle issues in BluetoothManager. For example:

    let disposeBag = DisposeBag()
    var manager: BluetoothManager?
    
    @IBAction func action(_ sender: UIButton) {
        if manager == nil {
            manager = BluetoothManager()
            manager?.rx_state.filter { $0 == .poweredOn }.debug().subscribe().disposed(by: disposeBag)
        } else {
            manager = nil
        }
    }

Steps:

  • tap to the button and turn bluetooth on
2017-10-12 16:13:50.482: TestViewController.swift:21 (action) -> subscribed
2017-10-12 16:13:50.482: TestViewController.swift:21 (action) -> Event next(poweredOn)
  • tap again, now manager = nil but deinit never called
  • tap again, create new manager instance
2017-10-12 16:19:11.940: TestViewController.swift:21 (action) -> subscribed
2017-10-12 16:19:11.956: TestViewController.swift:21 (action) -> Event next(poweredOn)
  • turn bluetooth off and on, (action) -> Event next(poweredOn) print twice which is not ok
2017-10-12 16:21:16.438: TestViewController.swift:21 (action) -> Event next(poweredOn)
2017-10-12 16:21:16.438: TestViewController.swift:21 (action) -> Event next(poweredOn)

Proposed solution
https://github.com/Polidea/RxBluetoothKit/blob/master/Source/BluetoothManager.swift#L211

    public var rx_state: Observable<BluetoothState> {
        return .deferred { [unowned self] in
            return self.centralManager.rx_didUpdateState.startWith(self.centralManager.state)
        }
    }

or

    public var rx_state: Observable<BluetoothState> {
        return .deferred { [central = self.centralManager] in
            return central.rx_didUpdateState.startWith(central.state)
        }
    }
@Kacper20
Copy link
Contributor

You're right. I will release a fix on the weekend/beginning of next week.
Thanks for the feedback!

@taraskontsur
Copy link
Author

@Kacper20 thanks, don't forget to fix other operators which keeps strong references)

@Kacper20
Copy link
Contributor

Fixed by #158, will be released in 4.0 version

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