alstjr7437 / TIL

Today I Learned!!
1 stars 0 forks source link

[iOS]Combine ๊ฐœ๋… ๊ณต๋ถ€ #4

Closed alstjr7437 closed 7 months ago

alstjr7437 commented 8 months ago

๐Ÿ“œ ๊ณต๋ถ€ ๋‚ด์šฉ Combine ๊ฐœ๋… ์ดํ•ดํ•˜๊ธฐ

โœ… ํ•ด์•ผํ•  ์ผ

alstjr7437 commented 7 months ago

Combine ์ •๋ฆฌ ๊ธ€

alstjr7437 commented 7 months ago

Combine์„ ์ •๋ฆฌํ•˜๋ฉด์„œ ํ”„๋กœ์ ํŠธ์—์„œ ์ง„ํ–‰ํ•œ ๋น„๋™๊ธฐ ๋™๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ–ˆ์œผ๋ฉด ๋”์šฑ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌ๊ฐ€ ๋์„์ง€ ์ƒ๊ฐ์ด ๋์Œ.

๋”ฐ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ๋ณด๋ฉด

 SharedData.shared.$userInfo
            .sink { [weak self] userInfo in
                guard let self = self, !userInfo.isEmpty else { return }

                // Handle updated userInfo here
                print("main updated userInfo: \(userInfo)")

                // Assuming userInfo is an array of strings
                self.CautionSource += userInfo[0]
                print(CautionSource)

                // Reload or update your collection views here
                self.cautionView.reloadData()
            }
            .store(in: &cancellables)

๋ผ๋Š” ์ฝ”๋“œ๊ฐ€ ์žˆ์—ˆ๋Š”๋ฐ sink๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  receive(on:)์„ ์‚ฌ์šฉํ–ˆ์œผ๋ฉด ๋”์šฑ ๊น”๋”ํ•˜๊ณ  ์ข‹๊ฒŒ ๋์„ ๊ฒƒ ๊ฐ™์Œ.