debugging 将注解插入Swift合并管道

p8h8hvxi  于 2023-02-13  发布在  Swift
关注(0)|答案(1)|浏览(134)

下面是我的出版商的一个片段:

let cancellable = URLSession.shared.dataTaskPublisher(for: url)
    .map(\.data)
    .decode([Object].self, decoder: JSONDecoder()
    .sink(...)

如果我想知道发生了什么,我可以这样做:

let cancellable = URLSession.shared.dataTaskPublisher(for: url)
    .map(\.data)
    .map { print("Before decoding"); return $0 }
    .decode([Object].self, decoder: JSONDecoder()
    .map { print("After decoding"); return $0 }
    .sink(...)

有没有比这种(ab)使用map或类似方法更好的方法?

sigwle7e

sigwle7e1#

正如在评论中提到的,显而易见的答案是.print()操作符,如果您只想查看特定类型事件的print语句,那么就使用handleEvents

let cancellable = URLSession.shared.dataTaskPublisher(for: url)
    .map(\.data)
    .handleEvents(receiveOutput: { _ in print("before decoding") })
    .decode(type: [Object].self, decoder: JSONDecoder())
    .handleEvents(receiveOutput: { _ in print("after decoding") })
    .sink(receiveCompletion: { _ in }, receiveValue: { _ in })

相关问题