I have several steps need to be processed synchronously. And the value resulted from the process are consumed by the view. It's working on iOS 14, but it's crashing on iOS 13. I use a Combine to publish an event to update the value stored inside the view model.
This is the PublisherManager:
final class PublisherManager {
static let shared = PublisherManager()
private var cancellable = Set<AnyCancellable>()
func mainPublisher() -> AnyPublisher<MainInput, Never> {
mainSubject
.eraseToAnyPublisher()
}
let mainSubject = PassthroughSubject<MainInput, Never>()
enum MainInput {
case updateValue()
}
}
This is the view model:
final class ViewModel: ObservableObject {
@Published var status: Status = .checking
init() {
setObserver()
start()
}
private func setObserver() {
PublisherManager.shared.mainPublisher()
.receive(on: RunLoop.main)
.sink { [weak self] action in
guard let self = self else { return }
switch action {
case .updateValue:
self.updateValue()
}
}.store(in: &cancellable)
}
func start() {
let dispatchGroup = DispatchGroup()
let dispatchSemaphore = DispatchSemaphore(value: 1)
dispatchGroup.enter()
dispatchQueue.asyncAfter(deadline: DispatchTime.now() + 1) {
dispatchSemaphore.wait()
self.getValues { //--> A process to call API
PublisherManager.shared.pushNotificationTroubleshooterSubject.send(.updateValue())
dispatchSemaphore.signal()
dispatchGroup.leave()
}
}
dispatchGroup.notify(queue: .main) {
// Notify
}
}
private func updateValue() {
status = .active
}
}
When I run it, I got EXC_BAD_ACCESS in the AppDelegate but it doesn't print any error at all on the debugger. If I comment the status = .active
code, it doesn't crash.
What am I doing wrong and how can I solve the problem?