๋ฌธ์ ์ํฉ
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 ์ ๋ํด ๋ ๊ฐ๊น์์ง ๊ธฐ๋ถ์ด ๋ค์๋ค.
์์ ํ ์ดํด์!!! ๐
'ํ๋ก์ ํธ ๐ ๏ธ > ํธ๋ฌ๋ธ ์ํ ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| task(id: ) ์ธ์ ์ฌ์ฉํ๋ฉด ์ข์๊น? (0) | 2023.06.10 |
|---|---|
| onAppear์ onDisappear์ ํธ์ถ ํ์ด๋ฐ (0) | 2023.06.10 |
| Firebase RESTAPI๋ก ๋ฐ์ดํฐ 20๊ฐ ์ด์ ๋ฐ์์ค๊ธฐ (0) | 2023.06.10 |