PassthroughSubject + flatMap 不能调用多次?
PassthroughSubject + flatMap cannot be called more than once?
如果我发送到PassthroughSubject<Void, Error>
,这个进程会运行一次,但是如果我发送不止一次,flatMap中的进程就不会运行。这是为什么?
var testTappedSubject = PassthroughSubject<Void, Error>()
testTappedSubject
.print("testTappedSubject")
.flatMap({ () -> AnyPublisher<Int, Error> in
print("called test")
return Fail(error: LoginError.someError(error: .unknown))
.eraseToAnyPublisher()
})
.sink { error in
print("error", error)
} receiveValue: { value in
print("pressed", value)
}
.store(in: &cancellables)
这就是 Combine(和其他 Reactive 框架)的工作方式。
您正在设置对发布者的订阅,订阅所做的唯一事情就是发出错误:
return Fail(error: LoginError.someError(error: .unknown))
.eraseToAnyPublisher()
当发布者发出错误时。订阅完成,不再接收任何事件。
我将你的代码版本放在了 playground 中:
import Combine
var cancellables = Set<AnyCancellable>()
var testTappedSubject = PassthroughSubject<Void, Error>()
enum LoginError: Error {
case unknown
}
testTappedSubject
.print("testTappedSubject")
.flatMap({ () -> AnyPublisher<Int, Error> in
print("called test")
return Fail(error: LoginError.unknown)
.eraseToAnyPublisher()
})
.sink { error in
print("error", error)
} receiveValue: { value in
print("pressed", value)
}
.store(in: &cancellables)
testTappedSubject.send()
testTappedSubject.send()
testTappedSubject.send()
控制台中的结果是:
testTappedSubject: receive subscription: (PassthroughSubject)
testTappedSubject: request unlimited
testTappedSubject: receive value: (())
called test
error failure(__lldb_expr_72.LoginError.unknown)
testTappedSubject: receive value: (())
testTappedSubject: receive value: (())
这表明收到了无限订阅,然后,在发送值后,调用了您的 print("called test")
,然后收到错误。
订阅现已完成。
发送更多值仅表示已收到一个值,但未向订阅者发送任何内容。
如果我发送到PassthroughSubject<Void, Error>
,这个进程会运行一次,但是如果我发送不止一次,flatMap中的进程就不会运行。这是为什么?
var testTappedSubject = PassthroughSubject<Void, Error>()
testTappedSubject
.print("testTappedSubject")
.flatMap({ () -> AnyPublisher<Int, Error> in
print("called test")
return Fail(error: LoginError.someError(error: .unknown))
.eraseToAnyPublisher()
})
.sink { error in
print("error", error)
} receiveValue: { value in
print("pressed", value)
}
.store(in: &cancellables)
这就是 Combine(和其他 Reactive 框架)的工作方式。
您正在设置对发布者的订阅,订阅所做的唯一事情就是发出错误:
return Fail(error: LoginError.someError(error: .unknown))
.eraseToAnyPublisher()
当发布者发出错误时。订阅完成,不再接收任何事件。
我将你的代码版本放在了 playground 中:
import Combine
var cancellables = Set<AnyCancellable>()
var testTappedSubject = PassthroughSubject<Void, Error>()
enum LoginError: Error {
case unknown
}
testTappedSubject
.print("testTappedSubject")
.flatMap({ () -> AnyPublisher<Int, Error> in
print("called test")
return Fail(error: LoginError.unknown)
.eraseToAnyPublisher()
})
.sink { error in
print("error", error)
} receiveValue: { value in
print("pressed", value)
}
.store(in: &cancellables)
testTappedSubject.send()
testTappedSubject.send()
testTappedSubject.send()
控制台中的结果是:
testTappedSubject: receive subscription: (PassthroughSubject)
testTappedSubject: request unlimited
testTappedSubject: receive value: (())
called test
error failure(__lldb_expr_72.LoginError.unknown)
testTappedSubject: receive value: (())
testTappedSubject: receive value: (())
这表明收到了无限订阅,然后,在发送值后,调用了您的 print("called test")
,然后收到错误。
订阅现已完成。
发送更多值仅表示已收到一个值,但未向订阅者发送任何内容。