Home > AI > IOS > Combine >

receive(on:options:)

Example 1: The work of received values need to on main thread or error would occur. By default, the publisher pipeline is in the background.

let cancellable = URLSession.shared.dataTaskPublisher(for: url)
            .map{ UIImage(data: $0.data )}
            .replaceError(with: nil)
            .receive(on: DispatchQueue.main) // or it will have error
            .handleEvents(receiveOutput: {
                self.cache[self.urlString] = $0
            })
            .assign(to: \.image, on: self)

Leave a Reply