cozzin
코찐 기술 블로그
cozzin
전체 방문자
오늘
어제
  • 분류 전체보기
    • Kotlin
    • 백엔드
    • iOS
      • Swift
      • SwiftUI
      • Combine
      • Architecture
    • 개발환경
    • 세미나
    • 생각정리
    • 스터디
    • CS
      • Refactoring
      • OS

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • LinkedIn
  • 강의

공지사항

인기 글

태그

  • darkmode
  • Combine
  • Swift
  • 테스트
  • 리팩토링
  • 컴퓨터공학
  • SwiftUI
  • ios
  • WWDC
  • 워닝제거
  • WWDC21
  • 디자인패턴
  • Warning
  • slide-over
  • XCode
  • CS
  • 운영체제
  • multicast
  • Ribs
  • os

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
cozzin

코찐 기술 블로그

[Combine 책 정리] Chapter 10: Debugging
iOS/Combine

[Combine 책 정리] Chapter 10: Debugging

2021. 1. 27. 14:50
반응형

안녕하세요 코찐입니다.

아래의 자료를 따라서 공부하고 있습니다.

https://www.raywenderlich.com/books/combine-asynchronous-programming-with-swift/v2.0

let subscription = (1...3).publisher
  .print("publisher")
  .sink { _ in }


// 다음에서 발췌: By Marin Todorov. ‘Combine: Asynchronous Programming with Swift.’ Apple Books. 
publisher: receive subscription: (1...3)
publisher: request unlimited
publisher: receive value: (1)
publisher: receive value: (2)
publisher: receive value: (3)
publisher: receive finished

print("이름") 으로 publisher에 붙여주면 간단하게 이벤트들을 출력해줍니다.

간단해서 많이 쓸 것 같습니다.

 

그리고 print 하는걸 커스터마이징 할 수 있는데, TextOutputStream을 사용해서 만들 수 있습니다.

developer.apple.com/documentation/swift/textoutputstream

 

Apple Developer Documentation

 

developer.apple.com

zeddios.tistory.com/1010

 

Swift ) TextOutputStream

안녕하세요 :) Zedd입니다. 방금 TextOutputStream이란걸 알아서 ㅇ0ㅇ 공부해보려고 합니다아 TextOutputStream은 프로토콜이에요! Swift Standard Library안에 있는 친구입니다. 뭔가 Text / Output / Stream...

zeddios.tistory.com

class TimeLogger: TextOutputStream {
  private var previous = Date()
  private let formatter = NumberFormatter()

  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 }

 

아직까진 어디에 쓸지 모르곘지만, 반복적으로 print하고 싶은 내용이 있으면 프로젝트에서 만들어서 쓸 수 있을 것 같아요.

 

그리고 네트워크 진행 상황을 출력해보고 싶다면 handleEvents 를 사용할 수 있습니다.

let request = URLSession.shared
    .dataTaskPublisher(for: URL(string: "https://www.raywenderlich.com/")!)

request
    .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 data received
Sink received data: 153253 bytes
Sink received completion: finished

 

그리고 breakpoint 함수도 있는데 딱히 쓸일은 없을 듯 합니다.

 

developer.apple.com/documentation/combine/publisher/breakpoint(receivesubscription:receiveoutput:receivecompletion:)

 

Apple Developer Documentation

 

developer.apple.com

간단하게만 요약했는데요...ㅎ

아래 글에서 좀 더 책 내용을 자세하게 다루고 있습니다 :)

sujinnaljin.medium.com/combine-debugging-2c045cdd2ce3

 

[Combine] Debugging

뚝딱따리뚝딱딱 디버깅을 해보자 🛠

sujinnaljin.medium.com

 

반응형
저작자표시 (새창열림)

'iOS > Combine' 카테고리의 다른 글

[Combine 책 정리] Chapter 12: Key-Value Observing  (0) 2021.01.27
[Combine 책 정리] Chapter 11: Timers  (0) 2021.01.27
[Combine 책 정리] Chapter 9: Networking  (0) 2021.01.27
[Combine 책 정리] Chapter 7: Sequence Operators  (0) 2021.01.22
[Combine 책 정리] Chapter 6: Time Manipulation Operators  (0) 2021.01.19
    'iOS/Combine' 카테고리의 다른 글
    • [Combine 책 정리] Chapter 12: Key-Value Observing
    • [Combine 책 정리] Chapter 11: Timers
    • [Combine 책 정리] Chapter 9: Networking
    • [Combine 책 정리] Chapter 7: Sequence Operators
    cozzin
    cozzin
    Software Engineer

    티스토리툴바