Post

Replies

Boosts

Views

Activity

Reply to How to solve "Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates." error?
This can help let url = ..... // your url var warehouseOrders = .... // your array private var cancelable: AnyCancellable? private static let sessionProcessingQueue = DispatchQueue(label: "SessionProcessingQueue") private func loadWarehouseOrders() {     cancelable = URLSession.shared.dataTaskPublisher(for: url)       .subscribe(on: Self.sessionProcessingQueue)       .map({         print("\n $0.data: \($0.data)")         return $0.data         })       .decode(type: WarehouseOrder.self, decoder: JSONDecoder())&#9;&#9;// if you have a WarehouseOrder model       .receive(on: DispatchQueue.main) // <-&#9; here is the problem, you are missing this       .sink(receiveCompletion: { (suscriberCompletion) in         switch suscriberCompletion {         case .finished: &#9;&#9; // do something that you want to do when finished           break         case .failure(let error):           print(error.localizedDescription)         }       }, receiveValue: { [weak self] (warehouseOrder) in         self?.warehouseOrders.append(warehouseOrder)       })   } deinit {     self.cancel() } func cancel(){     cancelable.cancel() }
Aug ’20