print(_:to:)
operator
- publisher λ₯Ό ν΅ν΄ μ²λ¦¬ λκ³ μλμ§ νμ€νμ§ μμ λ κ°μ₯ λ¨Όμ μ¬μ©
- μ΄λ€ μΌμ΄ μΌμ΄λκ³ μλμ§μ λν λ§μ μ 보λ₯Ό μΆλ ₯νλ passthrough publisher
let subscription = (1...3).publisher
.print("publisher")
.sink { _ in }
publisher: receive subscription: (1...3)
publisher: request unlimited
publisher: receive value: (1)
publisher: receive value: (2)
publisher: receive value: (3)
publisher: receive finished
- Subscription μ μ λ¬ λ°μμ λ μΆλ ₯νκ³ upstream publisher μ λν μ€λͺ μ 보μ¬μ€λ€.
- Subscriberμ μꡬ μμ²μ μΆλ ₯ν΄μ λͺκ°μ μμ΄ν μ΄ μμ² λμλμ§ λ³΄μ¬μ€λ€.
- Upstream publisher κ° emit ν λͺ¨λ κ°μ μΆλ ₯νλ€
- λ§μ§λ§μΌλ‘, completion μ΄λ²€νΈλ₯Ό μΆλ ₯νλ€
μΆκ°μ μΌλ‘ TextOutputSream
κ°μ²΄λ₯Ό κ°λ νλΌλ―Έν°κ° μλ€.
- μ§μ string μ logger μ μΆλ ₯ νκ² ν μ μλ€.
- νμ¬ λ μ§λ μκ° κ°μ μ 보λ₯Ό λ‘κ·Έμ μΆκ° ν μ μλ€.
class TimeLogger: TextOutputSream {
private var previous = Date()
private let formatter = NSNumberFormatter()
init() {
formatter.maximumFractionDigits = 5
formatter.minimumFractionDigits = 5
func write(_ string: String) {
let trimmed = string.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return }
let now = Date()
print("+\(formatter.string(for: now.timeIntervalSince(previous))!)s: \(string)")
previous = now
}
}
let subscription = (1...3).publisher
.print("publisher", to: TimeLogger())
.sink { _ in }
+0.00111s: publisher: receive subscription: (1...3)
+0.03485s: publisher: request unlimited
+0.00035s: publisher: receive value: (1)
+0.00025s: publisher: receive value: (2)
+0.00027s: publisher: receive value: (3)
+0.00024s: publisher: receive finished
handleEvents(receiveSubscription:receiveOutput:receiveCompletion:receiveCancel:r eceiveRequest:)
publisher μ μλͺ μ£ΌκΈ° λ΄μμ μ΄λ²€νΈλ₯Ό intercept νκ³ κ°κ°μ λ¨κ³μ μ‘μ μ μ·¨ν μ μλ€.
let request = URLSession.shared
.dataTaskPublisher(for: URL(string: "https://www.raywenderlich.com/")!)
let subscription = requset
.handleEvents(receiveSubscription: { _ in
print("Network request will start")
}, receiveOutput: { _ in
print("Network request data received")
}, receiveCancel: {
print("Network request cancelled")
})
.sink(receiveCompletion: { completion in
print("Sink received completion: \(completion)")
}) { data, _ in
print("Sink received data: \(data)")
}
Network request will start
Network request cancelled
Sink received data: 153253 bytes
Sink received completion: finished
breakpointOnError()
- Upstream publisher μμ μλ¬λ₯Ό emit νμλ, Xcode λ λλ²κ±°μ break νκ³ μ€νμ λ³Ό μ μλλ‘ ν΄μ£Όκ³ μ΄λμ μ μλ¬κ° λμ¨κ±΄μ§ μ°Ύκ² ν΄μ€λ€.
breakpoint(receiveSubscription:receiveOutput:receiveCompletion:)
- λ€μν μ΄λ²€νΈμ intercept νκ³ μΌμ΄μ€ λ§λ€ λλ²κ±°λ₯Ό λ©μΆκ³ μΆμμ§ κ²°μ νλ€.
.breakpoint(receiveOutput: { value in
return value > 10 && value < 15
}
쑰건μ μΌλ‘ ꡬλ
κ³Ό completion time μ break ν μ μμ§λ§, handleEvents
operator κ°μ μ·¨μλ intercept ν μ μλ€.
Note : Playground μμλ breakpoint publisher κ° λμνμ§ μλλ€. μ€νμ΄ interrupt λμλ€λ μλ¬λ λ³Ό μ μμ§λ§, λλ²κ±°μ λ€μ΄κ°μ§ μλλ€.
print
operator λ‘ publisher μ μλͺ μ£ΌκΈ°λ₯Ό μΆμ ν μ μλ€.TextOutputStream
μ μμ±ν΄μ κ²°κ³Ό string μ 컀μ€ν ν μ μλ€.handleEvents
operator λ μλͺ μ£ΌκΈ° μ΄λ²€νΈλ₯Ό intercept νκ³ μ‘μ μ μννλ€.breakpointOnError
μbreakpoint
operator λ νΉμ μ΄λ²€νΈλ₯Ό break νλ€.