From 98218264a35cdd53be9d37b566b7b86da6f67a92 Mon Sep 17 00:00:00 2001 From: Srdan Rasic Date: Wed, 13 Oct 2021 00:10:16 +0200 Subject: [PATCH] Fix an issue where binding closure could be executed after disposing --- Sources/Bindable.swift | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/Sources/Bindable.swift b/Sources/Bindable.swift index 96ddd91..507c876 100644 --- a/Sources/Bindable.swift +++ b/Sources/Bindable.swift @@ -104,11 +104,9 @@ extension SignalProtocol where Error == Never { /// - Returns: A disposable that can cancel the binding. @discardableResult public func bind(to target: Target, context: ExecutionContext, setter: @escaping (Target, Element) -> Void) -> Disposable { - return prefix(untilOutputFrom: target.deallocated).observeNext { [weak target] element in - context.execute { - if let target = target { - setter(target, element) - } + return prefix(untilOutputFrom: target.deallocated).receive(on: context).observeNext { [weak target] element in + if let target = target { + setter(target, element) } } } @@ -188,11 +186,9 @@ extension SignalProtocol where Error == Never, Element == Void { /// - Returns: A disposable that can cancel the binding. @discardableResult public func bind(to target: Target, context: ExecutionContext, setter: @escaping (Target) -> Void) -> Disposable { - return prefix(untilOutputFrom: target.deallocated).observeNext { [weak target] _ in - context.execute { - if let target = target { - setter(target) - } + return prefix(untilOutputFrom: target.deallocated).receive(on: context).observeNext { [weak target] _ in + if let target = target { + setter(target) } } }