ํ”„๋กœ์ ํŠธ ๐Ÿ› ๏ธ/ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

PassthroughSubject์™€ onReceive ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‚˜์ด์Šคํ•œ ๋น„๋™๊ธฐ์ฒ˜๋ฆฌํ•˜๊ธฐ

WonderPark 2023. 6. 10. 02:25

๋ฌธ์ œ ์ƒํ™ฉ

Combine ์œผ๋กœ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•จ.

๋น„๋™๊ธฐ ์ฝ”๋“œ๋กœ ๋ทฐ๋ชจ๋ธ์— ์žˆ๋Š” ๋ฐฐ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•œ ํ›„

๊ทธ ๋ฐฐ์—ด์„ ์ถœ๋ ฅ์„ ํ•ด๋ณด๋‹ˆ ๋นˆ๋ฐฐ์—ด์ด ์ถœ๋ ฅ๋˜๋Š” ์ƒํ™ฉ

 

ํ™ˆ๋ทฐ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ž‘์—…์„ ํ•˜๋ฉด์„œ

onAppear ๋‚ด์—์„œ communityVM.fetchCommunity ๋ผ๋Š” ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ฒŒ์‹œ๋ฌผ์„ ๊ฐ€์ ธ์˜ค๋Š” ๋น„๋™๊ธฐ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ 

communityVM.communities (๊ฐ€์ ธ์˜จ ์ปค๋ฎค๋‹ˆํ‹ฐ ๊ฒŒ์‹œ๋ฌผ๋“ค) ๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋‹ˆ

.onAppear {
                print("onAppear ์‹œ์ž‘")
                communityVM.fetchCommunity() 
                print(communityVM.communities) // [] ์ถœ๋ ฅ
            }

 

๊ฒฐ๊ณผ๋Š” ? “ [ ] “ ๋นˆ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ–ˆ๋‹ค. ์ƒ๊ฐํ•ด๋ณด๋ฉด ๋„˜๋‚˜ ๋‹น์—ฐํ•œ ๊ฒฐ๊ณผ์˜€๋‹ค. ^^

 

์›์ธ

func fetchCommunity() {
        print("CommunityViewModel fetchCommunity Start")
        
        CommunityService.getCommunity()
            .receive(on: DispatchQueue.main)
            .sink { (completion: Subscribers.Completion<Error>) in
                print("MagazineViewModel fetchCommunity complete")
            } receiveValue: { (data: CommunityResponse) in
                self.communities = data.community
                self.fetchCommunitySuccess.send()
            }.store(in: &subscription)
    }

 

์™œ๋ƒํ•˜๋ฉด,

fetchCommunity ๋ฉ”์†Œ๋“œ๊ฐ€ ์ผ๋‹จ ์‹คํ–‰๋˜๋ฉด ๋ฐ‘์— ์ฝ”๋“œ์— .receive(on: ) ๊นŒ์ง€ ์‹คํ–‰์ด ๋˜๊ณ ,

.sink ๋กœ ๋„˜์–ด๊ฐ€๊ธฐ์ „์— print๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ ‡๋‹ค.

 

์ฆ‰, ๋น„๋™๊ธฐ์ฝ”๋“œ๊ฐ€ ๋๋‚˜๊ธฐ๋„ ์ „์— print ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ !

 

์•„์ง fetchCommunity ๋ฉ”์†Œ๋“œ์—์„œ .sink ๋ฅผ ํ†ตํ•ด ๋ฐ›์€ ๊ฐ’์„ communities ๋ฐฐ์—ด์— ์ €์žฅํ•˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•˜๋‹ค ๋ณด๋‹ˆ

๋‹น์—ฐํ•˜๊ฒŒ๋„ ๋นˆ๋ฐฐ์—ด์ด ์ถœ๋ ฅ์ด ๋˜์—ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„๋ณธ ๊ฒฐ๊ณผ,

 

 

1. ์ฒซ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€

๋„ˆ๋ฌด๋‚˜ ๋‹น์—ฐํ•˜๊ฒŒ๋„ receiveValue ๋ฅผ ๋ฐ›์•„์„œ ์ €์žฅํ•œ ํ›„์— ๊ทธ ์•ˆ์—์„œ print ํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

func fetchCommunity() {
        print("CommunityViewModel fetchCommunity Start")
        
        CommunityService.getCommunity()
            .receive(on: DispatchQueue.main)
            .sink { (completion: Subscribers.Completion<Error>) in
                print("MagazineViewModel fetchCommunity complete")
            } receiveValue: { (data: CommunityResponse) in
                self.communities = data.community
                self.fetchCommunitySuccess.send()
                print(communityVM.communities) // ์—ฌ๊ธฐ์„œ ์ถœ๋ ฅ 
            }.store(in: &subscription)
    }

 

2. ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์€ 

(์šฐ์„  ํƒˆ๋ฝ์ด์ง€๋งŒ ใ…Žใ…Ž)

 

์ผ๋‹จ …! DispatchGroup ์ด๋ผ๋Š”๊ฑธ ์จ๋ณด๋ ค ํ–ˆ์ง€๋งŒ

๋น„๋™๊ธฐ๋ฅผ ๋๋‚ด๋Š” ๊ณผ์ •์—์„œ group.leave ๊ฐ€ ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ sink ์ฝ”๋“œ ๋‚ด์— ์กด์žฌํ•ด์•ผํ•˜๋ฏ€๋กœ ํƒˆ๋ฝํ–ˆ๋‹ค.

 

์•„๋ž˜ ์ฝ”๋“œ๋Œ€๋กœ ์‹คํ–‰ํ•˜๋ฉด ์ฒ˜์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๋‚ด๊ธฐ ๋•Œ๋ฌธ์— ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

 

.onAppear {
                print("onAppear ์‹œ์ž‘")
                // dispatch Group ํƒˆ๋ฝ
                let group = DispatchGroup()
                group.enter() // ๋น„๋™๊ธฐ ์‹œ์ž‘ ๋“ฑ๋ก
                communityVM.fetchCommunity()
                group.leave() // ๋น„๋™๊ธฐ ๋
                print(communityVM.communities) // [] ์ถœ๋ ฅ
            }

 

 

3. ๋งˆ์ง€๋ง‰ ๋ฐฉ๋ฒ•์€ print๋ฅผ fetchCommunity ์˜ .sink ๋ธ”๋ก ๋‚ด์— ์„ ์–ธํ•˜๋Š” ๊ฒƒ ๋ง๊ณ 

.onAppear ์—์„œ ์ฆ‰ ๋ฐ”๊นฅ์—์„œ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ผ๊นŒ ? ๊ณ ๋ฏผํ•ด๋ณธ ๊ฒฐ๊ณผ์ด๋‹ค.

 

var fetchCommunitySuccess = PassthroughSubject<(), Never>()

 

PassthroughSubject ^^ ์š”๊ฑธ ํ•จ ์‚ฌ์šฉํ•ด๋ณด์Ÿˆ ! 

 

 

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

๋จผ์ €

var fetchCommunitySuccess = PassthroughSubject<[CommunityDTO], Never>()

 

์ด๋Ÿฐ์‹์œผ๋กœ [CommunityDTO] ์š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๊ฒ ๋‹ค. ๋ผ๊ณ  ์„ ์–ธํ•ด์ฃผ๊ณ 

 

func fetchCommunity() {
        print("CommunityViewModel fetchCommunity Start")
        
        CommunityService.getCommunity()
            .receive(on: DispatchQueue.main)
            .sink { (completion: Subscribers.Completion<Error>) in
                print("MagazineViewModel fetchCommunity complete")
            } receiveValue: { (data: CommunityResponse) in
                self.communities = data.community
                // ์—ฌ๊ธฐ์„œ data.community๋ฅผ send์— ์‹ค์–ด๋ณด๋‚ด์คŒ 
                self.fetchCommunitySuccess.send(data.community)
            }.store(in: &subscription)
    }

 

์ €๋Ÿฐ์‹์œผ๋กœ fetchCommunitySuccess ์— data๋ฅผ ์‹ค์–ด๋ณด๋‚ด์ฃผ๊ณ 

 

์ด๊ฑธ ๋ทฐ์—์„œ

 

.onAppear {
                print("onAppear ์‹œ์ž‘")
                communityVM.fetchCommunity()
                print(communityVM.communities) 
            }
            .onReceive(communityVM.fetchCommunitySuccess) { newValue in
                print(newValue)

 

.onReceive ๋ฅผ ํ†ตํ•ด์„œ communityVM.fetchCommunitySuccess์˜ ๊ฐ’์„ ๋ฐ›์•„์™€์„œ ๊ทธ ๋‹ค์Œ print ํ•ด์ฃผ๋Š”๊ฑฐ๋‹ค. !!

 

.onReceive ๊ฐ€ ์–ด๋–ค ๋ฉ”์†Œ๋“œ๋ƒ๋ฉด

 

 

view ์—์„œ ์ง€์ •๋œ publisher ๊ฐ€ ๋ฐฉ์ถœํ•œ ๋ฐ์ดํ„ฐ๋“ค์„ ๊ฐ์ง€ํ•ด์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ทธ๋Ÿฐ ๋ฉ”์†Œ๋“œ๋‹ค.

 

๊ตฌ๋…ํ•  publisher๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„ฃ์–ด์ฃผ๋ฉด, ๊ทธ publisher ๊ฐ€ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ฐฉ์ถœํ•จ์„ ๊ฐ์ง€ํ–ˆ์„๋•Œ action ์—์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ณ ๋Ÿฐ ๋„ˆ๋‚Œ

 

๊ทธ๋ž˜์„œ !

๋ทฐ๋ชจ๋ธ์˜ fetch ๋ฉ”์†Œ๋“œ ๋‚ด์˜ sink ๋ธ”๋ก์—์„œ self.fetchCommunitySuccess.send(data.community) ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐฉ์ถœํ•˜๋ฉด

.onReceive ๋ฉ”์†Œ๋“œ์—์„œ ๊ฐ์ง€ํ•˜๊ณ  print ํ•œ๋‹ค. ์ด ์ˆœ์„œ๋กœ ์ง„ํ–‰๋œ๋‹ค.

 

 

์ด๋ ‡๊ฒŒ ํ•ด์„œ ํ•ด๊ฒฐ์™„๋ฃŒ ใ…Žใ…Ž

 

์Œ ์ด๊ฒŒ ์ด๋ ‡๊ฒŒ๊นŒ์ง€ ํ•˜์ง€ ์•Š์•„๋„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฟŒ๋ ค์ฃผ๋Š”๊ฒƒ์€ ์•„๋ฌด ์ด์ƒ ์—†์—ˆ์ง€๋งŒ

๋น„๋™๊ธฐ์ฒ˜๋ฆฌ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ณผ์ •์—์„œ ๋น„๋™๊ธฐ ์ฝ”๋“œ ๋‹ค์Œ ์ฝ”๋“œ๋Š” ์–ด๋–ป๊ฒŒ ์‹คํ–‰์„ ํ•ด์•ผ๋ ๊นŒ ๋ผ๋Š” ์˜๋ฌธ์  ์ด ์ƒ๊ฒจ์„œ ,,, ใ…Žใ…Ž๐Ÿ˜œ

 

์ด ๋‘˜์˜ ๊ฟ€์กฐํ•ฉ์€ ๋‚˜์ค‘์— ๋‹ค์ค‘ ๋น„๋™๊ธฐ ์ž‘์—…์„ "๋™๊ธฐ์ "์œผ๋กœ ์ฒ˜๋ฆฌํ•  ๋•Œ ์š”๊ธดํ•˜๊ฒŒ ์“ฐ์ผ ์˜ˆ์ •์ด์—ˆ๋‹ค. 

To be continue.. 

 

์ƒˆ๋กœ ์•Œ๊ฒŒ๋œ ๋ฉ”์„œ๋“œ

  • onReceive : Subject Publisher์— ๊ฐ’์ด ๋“ค์–ด์™”์„๋•Œ onRecieve (๋ง๊ทธ๋Œ€๋กœ ๊ฐ’์„ ๋ฐ›์•˜์„๋•Œ) ๋” ๋””ํ…Œ์ผํ•˜๊ฒŒ ์šฐ๋ฆฌ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ๋„คํŠธ์›Œํฌ ์ž‘์—…์ด ์™„๋ฃŒ๋˜๊ณ  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™”์„๋•Œ ์•ก์…˜์„ ์ˆ˜ํ–‰ํ•ด์•ผํ• ๋•Œ ์•„์ฅฌ ์ž˜ ์‚ฌ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค!!

 

๋А๋‚€์ 

Combine์˜ Subject ๋ผ๋Š” ๊ฐœ๋…์„ ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ

์‹ค์ œ ํ”„๋กœ์ ํŠธ์—์„œ ์–ด๋–ค ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉ๋ ๊นŒ๋ผ๋Š” ๊ถ๊ธˆ์ฆ์ด ์žˆ์—ˆ๋‹ค.

์ด๋ฒˆ ์ด์Šˆ๋ฅผ ํ†ตํ•ด PassthroughSubject ๋ผ๋Š” Publisher ์—

.send() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ’์„ ๋ฐ–์—์„œ ์ฃผ์ž…์‹œ์ผฐ์„๋•Œ, ์ฆ‰ ๋„คํŠธ์›Œํ‚น ์ž‘์—…์ด ์™„๋ฃŒ๋˜์—ˆ์„๋•Œ

๋ทฐ์—์„œ onReceive ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฐ’์˜ ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ํ•ด์„œ ์›ํ•˜๋Š” ์•ก์…˜์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ 

PassthroughSubject ์— ๋Œ€ํ•ด ๋” ๊ฐ€๊นŒ์›Œ์ง„ ๊ธฐ๋ถ„์ด ๋“ค์—ˆ๋‹ค.

์™„์ „ํžˆ ์ดํ•ด์™„!!! ๐Ÿ˜†