Hi,
I know there is the receiveOn call for defining the scheduler for all downstream publishers, but how can I tell combine on wich scheduler to execute the first publisher? Example:
Say I want to read a file from a background thread. Since there is no special publisher for that, I use a Future:
extension Data {
static func read(fileAt url: URL) -> Future<Data, Error> {
return Future { promise in
do {
let data = try Data(contentsOf: url)
promise(Result<Data, Error>.success(data))
} catch (let error) {
promise(Result<Data, Error>.failure(error))
}
}
}
}
Now I can use this as a publisher of the data at that file url, and its easy to execute the downstream on another thread:
let disposal = Data.read(fileAt: myFileUrl)
.receive(on: myBackgroundthread)
.decode(type: MyType.self, decoder: JSONDecoder())
.sink(...)
But how can I tell combine on wich scheduler to perform the first publisher? I know I could run the promise in the future on another queue, but I wonder if there is a way doing it with the combine api.
Greetings,
Johannes