diff --git a/stencils/LifecycleViewModel.stencil b/stencils/LifecycleViewModel.stencil index 4b2b4ad..07bdac6 100644 --- a/stencils/LifecycleViewModel.stencil +++ b/stencils/LifecycleViewModel.stencil @@ -7,12 +7,16 @@ class LifecycleViewModel { let containerView: ContainerView? init(_ viewModel: VM, containerView: ContainerView? = nil) { + assert(Thread.isMainThread, "Constructor must be called on the main thread, but called on \(Thread.current)") self.viewModel = viewModel self.containerView = containerView } deinit { - viewModel.clear() + let vm = viewModel + DispatchQueue.main.async { + vm.clear() + } } func lifecycleView(view: (VM) -> V) -> some View { diff --git a/stencils/ListObservable.stencil b/stencils/ListObservable.stencil index 79636dc..137bd8c 100644 --- a/stencils/ListObservable.stencil +++ b/stencils/ListObservable.stencil @@ -17,6 +17,7 @@ class ListObservable: ObservableObject { animated: Bool = false, mapper: @escaping (NSArray) -> [Output] = ListMapper.to ) { + assert(Thread.isMainThread, "Constructor must be called on the main thread, but called on \(Thread.current)") input = observable.currentOrNull if input != nil { value = mapper(input!) @@ -48,7 +49,10 @@ class ListObservable: ObservableObject { } deinit { - disposeBag.dispose() + let bag = disposeBag + DispatchQueue.main.async { + bag.dispose() + } } } diff --git a/stencils/ListSubject.stencil b/stencils/ListSubject.stencil index 0f8cc89..8de4b74 100644 --- a/stencils/ListSubject.stencil +++ b/stencils/ListSubject.stencil @@ -18,6 +18,7 @@ class ListSubject: ObservableObject { animated: Bool = false, mapper: @escaping (NSArray) -> [Output] = ListMapper.to ) { + assert(Thread.isMainThread, "Constructor must be called on the main thread, but called on \(Thread.current)") input = subject.currentOrNull if input != nil { value = mapper(input!) @@ -54,7 +55,10 @@ class ListSubject: ObservableObject { } deinit { - disposeBag.dispose() + let bag = disposeBag + DispatchQueue.main.async { + bag.dispose() + } } } diff --git a/stencils/Observable.stencil b/stencils/Observable.stencil index 39c91a4..29f1200 100644 --- a/stencils/Observable.stencil +++ b/stencils/Observable.stencil @@ -29,6 +29,7 @@ class Observable: ObservableObject { animated: Bool = false, mapper: @escaping (Input) -> Output ) { + assert(Thread.isMainThread, "Constructor must be called on the main thread, but called on \(Thread.current)") input = observable.currentOrNull if input != nil { value = mapper(input!) @@ -59,7 +60,10 @@ class Observable: ObservableObject { } deinit { - disposeBag.dispose() + let bag = disposeBag + DispatchQueue.main.async { + bag.dispose() + } } } diff --git a/stencils/Subject.stencil b/stencils/Subject.stencil index b089f94..609cabd 100644 --- a/stencils/Subject.stencil +++ b/stencils/Subject.stencil @@ -25,6 +25,7 @@ class Subject: ObservableObject { toMapper: @escaping (Input) -> Output, fromMapper: @escaping (Output) -> Input ) { + assert(Thread.isMainThread, "Constructor must be called on the main thread, but called on \(Thread.current)") input = subject.currentOrNull if input != nil { value = toMapper(input!) @@ -61,7 +62,10 @@ class Subject: ObservableObject { } deinit { - disposeBag.dispose() + let bag = disposeBag + DispatchQueue.main.async { + bag.dispose() + } } } diff --git a/stencils/UninitializedObservable.stencil b/stencils/UninitializedObservable.stencil index b7a087b..9c16852 100644 --- a/stencils/UninitializedObservable.stencil +++ b/stencils/UninitializedObservable.stencil @@ -25,6 +25,7 @@ class UninitializedObservable: Observabl animated: Bool = false, mapper: @escaping (Input?) -> Output? ) { + assert(Thread.isMainThread, "Constructor must be called on the main thread, but called on \(Thread.current)") input = observable.currentOrNull if input != nil { value = mapper(input!) @@ -53,7 +54,10 @@ class UninitializedObservable: Observabl } deinit { - disposeBag.dispose() + let bag = disposeBag + DispatchQueue.main.async { + bag.dispose() + } } } diff --git a/stencils/UninitializedSubject.stencil b/stencils/UninitializedSubject.stencil index d420558..82613d0 100644 --- a/stencils/UninitializedSubject.stencil +++ b/stencils/UninitializedSubject.stencil @@ -22,6 +22,7 @@ class UninitializedSubject: ObservableOb toMapper: @escaping (Input?) -> Output?, fromMapper: @escaping (Output?) -> Input? ) { + assert(Thread.isMainThread, "Constructor must be called on the main thread, but called on \(Thread.current)") input = subject.currentOrNull if input != nil { value = toMapper(input!) @@ -65,7 +66,10 @@ class UninitializedSubject: ObservableOb } deinit { - disposeBag.dispose() + let bag = disposeBag + DispatchQueue.main.async { + bag.dispose() + } } }