Lagacy๋ฅผ Combine ์ ํํ๋ฉด์ ๋ฐฐ์ ๋ Tip๋ค์ ์ ์ด๋ณธ๋ค. Notification Cetner์ ๊ฑธ์๋ Observer๋ฅผ Publisher๋ก ์ ํํ๋ ๊ฒ์ ๋ํ ๊ธ์ด๋ค.
publisher(for:object:)
iOS 13๋ถํฐ Notification Center์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ค. addObserver ํ๋ ๊ฒ์ ์ด property๋ฅผ ์ฌ์ฉํ์ฌ ๋ณ๊ฒฝํ ์ ์๋ค. ๊ธฐ์กด์๋ VC๋ฅผ ๋์ด๊ฐ๋ ๊ฒฝ์ฐ์ removeObserver๋ฅผ ํญ์ํด์คฌ์ด์ผ ํ๋๋ฐ, ์ด๋ ๊ฒ ํ ๊ฒฝ์ฐ ์ฝ๋๊ฐ ๋ณด๋ค ๊ฐ๊ฒฐํด์ง๊ณ ๋ก์ง์ ์ฝ๊ธฐ ์ฌ์์ง๋ค๋ ์ฅ์ ์ด ์๋ค.
Publisher ์ฌ์ฉํ๊ธฐ
override func viewWillAppear() {
super.viewWillAppear()
let notification = Notification.Name("MyNotification")
self.observer = NotificationCenter.default.addObserver(forName: notification, object: nil, queue: nil) { notification in
// Some Action
}
// ๋๋
self.observer = NotificationCenter.default.addObserver(self, selecter: #selector(self.reloadData(), name: notification, object: nil))
}
override func viewWillDisappear() {
super.viewWillDisappear()
NotificationCenter.default.removeObserver(self.observer)
}๊ธฐ์กด์ ๊ฒฝ์ฐ ์์ ๊ฐ์ด ์ฌ์ฉํ์ ๊ฒ์ด๋ค. ์ฆ, ์ด๋๊ฐ์์ addObserver๋ฅผ ํด์ฃผ๊ณ , removeObserver๋ฅผ ํด์ฃผ์์ด์ผ ํ๋ค.
func addSubscribers() {
let notificationName = Notification.Name("MyNotification")
NotificationCenter.default.publisher(for: notificationName, object: nil).publisher
.receive(on: DispatchQueue.main)
.sink { [weak self] notification in
self.reloadData(notification)
}
.store(in: &self.cancellables)
}์ด๋ ๊ฒ ์ฌ์ฉํ๋ฉด, ํด๋น VC๊ฐ ํ ๋นํด์ ๋๋ ์๊ฐ self.cancellables๊ฐ ๋ฉ๋ชจ๋ฆฌ์์ ํด์ ๋๋ฉด์ subscriber๋ค์ด ๋ชจ๋ ์ ๊ฑฐ๋๋ค. ๊ทธ๋์ ์ถ๊ฐ์ ์ผ๋ก removeObserver๋ฅผ ํด์ค ํ์๊ฐ ์๋ค.
Notification Combine ์ ํํ๊ธฐ
ํน์ ํ๋์ ๊ฐ์ฒด์์ ๋ค์ํ publisher๋ฅผ ๋ณด๋ด์ฃผ์ด์ผ ํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ๋ค์ํ ๊ณณ์์ ์์ฒญ์ด ๋ค์ด์ค๊ธฐ ๋๋ฌธ์ singleton์ผ๋ก ์ ์ํ๋ ํ๋จ์ด ์ณ์์ ๊ฒฝ์ฐ์ด๋ค. ์ด๋ฐ ๊ฒฝ์ฐ, ๊ฐ๊ฐ์ ํ๋ฉด์์ ์์ ๊ฐ์ด NotificationCenter.default.publisher์ ๊ฐ์ด ๋ฑ๋กํด์ ์ฌ์ฉํ๋ค๋ฉด ์ค๋ณต๋ ์ฝ๋๋ ๋ง๊ณ ๊ฐ๋
์ฑ์ด ๋จ์ด์ง ๊ฒ์ด๋ค.
์ด๋ฐ ๊ฒฝ์ฐ, singleton ๊ฐ์ฒด์ publisher๋ฅผ ์์ ๋ฌ์๋ฒ๋ ค์, ๋ค์ํ ํ๋ฉด์์ ์ง์ ์ ๊ทผํ์ฌ ์ฌ์ฉํ๋๋ก ํ๋ ๊ฒ์ด ์ข๊ฒ ๋ค. ํน์ ๋ฐ์ดํฐ๊ฐ ์ ๋ฐ์ดํธ ๋ ์๊ธฐ์, ๋ณ๊ฒฝ๋ ๊ฐ์ ์ ๊ณตํ๋ publisher๋ฅผ ๋ง๋ ๋ค๊ณ ์๊ฐํด๋ณด์.
final class DataManager {
static let shared = DataManager()
static let notificationName = Notification.Name(rawValue: "DataChange")
private func someUpdateFunction() {
// Do some jobs
NotificationCenter.default.post(name: Self.notificationName, object: self, userInfo: ["update": update])
}
}
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.observer = NotificationCenter.default.addObserver(forName: DataManager.shared.notifiactionName)
}
override func viewDidDisappear() {
super.viewDidDisappear()
NotificationCenter.default.removeObserver(self.observer)
}
}๊ธฐ์กด ๊ฐ์ ๊ฒฝ์ฐ์๋, ์ด๋ฐ์์ผ๋ก ํน์ ๊ฐ์ฒด์์ ๋ณด๋ธ ๋ฉ์์ง๋ฅผ Observer๋ฅผ ๋ฑ๋กํ์ฌ ๋ฐ์ ๋ค ์ฒ๋ฆฌํ๋ค๊ณ ์๊ฐํด๋ณด์. ์ฝ๋๋ ๋๊ฐ ์ง ๊ฒ์ด๋ค.
internal protocol DataManageable: AnyObject {
var updatedPublisher: AnyPublisher<Update, Never> { get }
}
final class DataManager: DataManageable {
static let shared = DataManager()
private updatedSubject = PassthroughSubject<Update, Never>()
internal updatedPublisher: AnyPublisher<Update, Never> {
self.updatedSubect.eraseToAnyPublisher()
}
private func someUpdateFunction() {
// Do some jobs
NotificationCenter.default.post(name: Self.notificationName, object: self, userInfo: ["update": update])
self.updateSubject.send(update)
}
}
final class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
DataManager.shared.updatedPublisher
.receive(on: DispatchQueue.main)
.sink { [weak self] updatedInfo in
// Do Some Logic..
}
.store(in: &self.cancellables)
}
}
์ฌ๊ธฐ์ ๋ฐฐ์ธ๋งํ ์ ์ ๋๊ฐ์ด๋ค.
- Protocol์ ์ฌ์ฉํด์ interface๋ฅผ ๋ง๋ ๋ค์ ์ฒ๋ฆฌํ ์ ์๋ค.
eraseToAnyPublisher๋ฅผ ํตํด์ ์ฌ์ฉํ๋ ์ชฝ์์๋ ์ด๋ค Publisher ํ์ ์ธ์ง ์์ง ๋ชปํ๊ฒ ํ ์ ์๋ค. ์ฆ, ์๋์ ์ผ๋ก ๋ง๋ค์ด๋ฒ๋ฆด ์ ์๋ค.
์ฝ๋๋ ๋ณ๊ฒ ์์ง๋ง, ๋ฐฐ์ธ์ ์ด ์๋ ์ฝ๋์ด๋ค.
์ค์ ์ฌ์ฉ์
NotificationCenter.default
.publisher(for: UIDevice.orientationDidChangeNotification)
.filter { _ in UIDevice.current.orientation == .portrait }
.sink { _ in print("Orientation changed to portrait!") }๋ง๋ฌด๋ฆฌ
์์ง์ Combine์ ๋ํด์ ์ ๋๋ก ๊ณต๋ถํ์ง ๋ชปํด์ ์ ์์ง ๋ชปํ์ง๋ง, ๋น๋ถ๊ฐ ๋ฐ๋ก ์ฌ์ฉํ ์ ์๋ ๊ฒ์ ๊ธฐ์ค์ผ๋ก ์ ๋ฆฌํ๊ณ ์๋ค. ์ถํ Network, Error Handling, Operator๋ฑ์ ๋ํด ๊ณต๋ถํด์ ๊ธ์ ์์ฑํด์ผ ํ ๋ฏํ๋ค. ๋!