Publishers.Encode

์ œ๋„ค๋ฆญ ๊ตฌ์กฐ์ฒด

์ด๋‹ˆ์…œ๋ผ์ด์ €๋Š” ๋‘ ๊ฐœ์˜ ์ธ์ž๋ฅผ ๋ฐ›๋Š”๋‹ค.

  • upstream: ์ƒ์œ„์— ํ๋ฅด๋Š” Publisher

  • encoder : ์‚ฌ์šฉํ•  ์ธ์ฝ”๋”

encoder๋Š” Combine์˜ TopLevelEncoder ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•ด์•ผ ํ•˜๋Š”๋ฐ, ํ˜„์žฌ JSONEncoder์™€ PropertyListEncoder๊ฐ€ ์ด ํ”„๋กœํ† ์ฝœ์„ ์ฑ„ํƒํ•˜๊ณ  ์žˆ๋‹ค.

์ƒ์œ„ ์ŠคํŠธ๋ฆผ์˜ ์š”์†Œ๋ฅผ ์ธ์ฝ”๋”ฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

encode ์˜คํผ๋ ˆ์ดํ„ฐ์™€ ๊ด€๋ จ์ด ์žˆ๋‹ค.

struct SimpleJSON: Encodable {
  let key: String
}
let simpleJSON = SimpleJSON(key: "value")

// Publishers.Encode Publisher
Publishers.Encode(upstream: Just(simpleJSON), encoder: JSONEncoder())
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Encode Error")
    case .finished:
      print("Combine Encode Finish")
    }
  }, receiveValue: { value in
    print("Combine Encode : \(value)")
  })
  .store(in: &cancellables)

// encode Operator
Just(simpleJSON)
  .encode(encoder: JSONEncoder())
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine Encode Error")
    case .finished:
      print("Combine Encode Finish")
    }
  }, receiveValue: { value in
    print("Combine Encode : \(value)")
  })
  .store(in: &cancellables)

// Combine Encode : 15 bytes
// Combine Encode Finish

์ƒ์œ„ Publisher๋Š” Encodable ํ”„๋กœํ† ์ฝœ์„ ๋”ฐ๋ฅด๋Š” ๊ตฌ์กฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐœํ–‰ํ•˜๋ฉฐ, JSONEncoder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฝ”๋”ฉํ•œ๋‹ค.

RxSwift

ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

ReactiveSwift

ํ•ด๋‹น ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

Last updated

Was this helpful?