我很难弄清楚如何创建一个包含 的 Swift 组合管道,其中.flatMap引用了self. 为了防止保留周期,这应该是一个[weak self]引用,但这不适用于.flatMap.
这是一个显示我的问题的简化示例:
import Foundation
import Combine
class SomeService {
func someOperation(label: String) -> Future<String, Never> {
Future { promise in
print("Starting work for", label)
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
print("Finished work for", label)
promise(.success(label))
}
}
}
}
class SomeDataSource {
let someService = SomeService()
var cancellables = Set<AnyCancellable>()
deinit {
print("Deinit SomeDataSource")
}
func complexOperation() {
// First part 'defined' is inside the complexOperation method:
someService.someOperation(label: …Run Code Online (Sandbox Code Playgroud)