Working With External Data
π¬ Part1 μμλ ...
- μ μ μ SwiftUI λ·° κ° λ°μ΄ν° νλ¦μ λν΄ μκΈ°ν΄λ΄€μλ€.
- @State νλ‘νΌν° λνΌλ₯Ό ν΅ν΄ κ°μ΄ λ³κ²½λ¨μ νΈλνΉνλ λ³μλ₯Ό λ μ μκ³ ,
- @Binding μ ν΅ν΄ μμλ·°μμλ κ°μ λ°μ΄ν°λ₯Ό λ°λΌλ³΄λλ‘ ν μ μμλ€.
πβοΈ Part2 μμλ?
- λ·° λ΄λΆμμμ λμμ κ΄λ¦¬νλ @State λ³μλ‘λ λΆκ°λ₯ν μΈλΆμμ λ°μνλ μ΄λ²€νΈ, μΈλΆμμ κ΄λ¦¬λλ λ°μ΄ν°λ μ΄λ»κ² μ²λ¦¬ν΄μΌν μ§ λ€λ€λ³Έλ€.
- κ²°λ‘ μ μΌλ‘ μ μ μΈν°λ μ
μ΄ μλ timer, notification μ²λΌ μΈλΆμμ λ°μνλ μ΄λ²€νΈλ λμΌν νλ¦μΌλ‘ λμνλ€.
- action μμ± -> State κ° λ³κ²½ -> λ·°μ λν μλ‘μ΄ μΉ΄νΌ -> μ μ μκ² μλ‘κ² λ³΄μ¬μ€
- SwiftUI μμλ μλ¬ν μ§μμ μΌλ‘ κ°μ΄ λ³κ²½λλ μΈλΆ μ΄λ²€νΈλ₯Ό λνλ΄κΈ° μν μΆμν νμ μΈ Publisher κ° μλ€.
Combine Publisher
Combine μ λΉλκΈ° μ΄λ²€νΈλ₯Ό νΈλ€λ§νκΈ°μν νλ μμν¬μ΄κ³ , Publisher λ Combine νλ μμν¬μμ μκ°λ νμ μ΄λ€.
- Publisher λ μκ°μ΄ μ§λ¨μ λ°λΌ κ°μ λ³΄λΌ μ μλ νμ μΌλ‘, RxSwift μ Observable λ λΉμ·ν κ°λ μ΄λ€.
- SwiftUI λ·°μμ Publisher μ κ°μ ꡬλ ν΄μ μ¬μ©νλ €λ©΄ main thread μμ κ°μ λ°©μΆν΄μΌνκ³ , μ€μΌμ₯΄λ¬ μ§μ μ μν .receive(on:) λ©μλλ₯Ό μ 곡νλ€.
π‘ Publisher (Protocol)
Declares that a type can transmit a sequence of values over time.
π‘ Combine
Customize handling of asynchronous events by combining event-processing operators.
func receive<S>(
on scheduler: S,
options: S.SchedulerOptions? = nil
) -> Publishers.ReceiveOn<Self, S> where S : Scheduler
βοΈπ₯ example
podcast player μ μ¬μμκ°μ νμνλ λ·°λ₯Ό μΆκ°ν΄λ³΄μ.
- @State λ³μμΈ currentTime μ μΆκ°
- currentTime μ νμν΄μ€ Text λ·°λ₯Ό μΆκ°
- λ·°μ λν΄μ onReceive λ©μλλ₯Ό μΆκ°
- λ©μλμ μΈμλ‘ μ§μ ν publisher κ° κ°μ λ°©μΆν λ λ§λ€ closure κ° μνλλ€.
- μ΄μ SwiftUI μ Publisher κ° λνλμκ° μκΈ°κ² λμλ€!
- currentTimePublisher κ° λ³κ²½ > state λ³μ κ° λ³κ²½ > view μλ‘ κ·Έλ €μ§
π‘onReceive(_:perfome:)
Adds an action to perform when this view detects data emitted by the given publisher.
func onReceive<P>(
_ publisher: P,
perform action: @escaping (P.Output) -> Void
) -> some View where P : Publisher, P.Failure == Never
BindableObject ObservableObject Protocol
β οΈ BindableObject is replaced by the ObservableObject protocol from the Combine framework. (50800624)
2019 WWDC μμ μ λ³κ²½μ¬νμ΄ μ’ μλ€μ π₯Ί
- BindableObject -> ObservableObject λ‘ λ³κ²½λ¨
- didChange λ₯Ό νμλ‘ κ΅¬νν΄μΌν¨ -> willChange λ₯Ό νμλ‘ κ΅¬νν΄μΌν¨
- μ§μ ꡬννμ§ μμλ @Published λ₯Ό λΆμΈ νλ‘νΌν°λ κ°μ΄ λ³κ²½λκΈ°μ μ μλμΌλ‘ κ°μ λ°©μΆν¨
- λ·°μμ ν΄λΉ λͺ¨λΈμμ μ¬μ©ν νλ‘νΌν°μ λν΄μλ§ @Published λ₯Ό λΆμ¬μ£Όλ©΄ λ¨.
class Contact: ObservableObject {
@Published var name: String
@Published var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
func haveBirthday() -> Int {
age += 1
return age
}
}
let john = Contact(name: "John Appleseed", age: 24)
cancellable = john.objectWillChange
.sink { _ in
print("\(john.age) will change")
}
print(john.haveBirthday())
// Prints "24 will change"
// Prints "25"
Creating Dependencies on ObservableObject
SwiftUI λ·°μμ ObservableObject λ‘ μ€μ λ λͺ¨λΈμ λν λνλμλ₯Ό μ°κ²°νλ λ°©λ²
- λ·°μμ @ObservedObject νλ‘νΌν° λνΌλ₯Ό ν΅ν΄ ν΄λΉ λͺ¨λΈ νμ μ λ³μλ₯Ό μ μΈνλ€.
- λ·°λ₯Ό μ΄κΈ°νν λ model instance λ₯Ό λ겨μ€λ€.
- ObservableObject λͺ¨λΈμ λν subscribe λ₯Ό μλμΌλ‘ νλκ²!
- μλ μμ μμ λ³κ²½λ μ
- BindableObject → ObservableObject
- @ObjectBinding → @ObservedObject
Creating Dependencies Indirectly
λ·°μ μμλ·°μμλ§ νμλ‘νλ λ°μ΄ν°μ λνλμλ₯Ό μ°κ²°νλ λ°©λ²
- @EnvironmentObject νλ‘νΌν° λνΌλ₯Ό ν΅ν΄ κ³μΈ΅μ μ λΆ κ±°μΉμ§ μκ³ , νμ λ·°μμ λͺ¨λΈμ λν μμ‘΄μ±μ κ°κ²ν μ μλ€.
- μ¬λ¬κ°μ λ·°μμ κ°μ λͺ¨λΈμ μ¬μ©ν μλ μλ€. λ°μ΄ν°κ° λ³κ²½λ λ μ¬λ¬κ°μ λ·°μμ μλμΌλ‘ μ
λ°μ΄νΈκ° λλ€.
- μ§λλ²μ μΈκΈλ source of truth κ° μκ°λλ κ² π
π‘EnvironmentObject
A property wrapper type for an observable object supplied by a parent or ancestor view.
π‘environmentObject(_:)
Supplies an ObservableObject to a view subhierarchy.
PlayerView()
.environmentObject(player)
ObservedObject vs EnvironmentObject
μ΄λ‘μ λ·°μμλ§ Model μ μ¬μ©ν΄μΌνλ κ²½μ°
- @ObservedObject λ₯Ό μ΄μ©νλ©΄ λ·°κ³μΈ΅μμ μμλ·°λ‘ λ΄λ €κ°λ©΄μ λͺ¨λΈμ κ³μ μ λ¬ν΄μΌνλ€.
- @ObjectBinding → @ObservedObject
- @EnvironmentObject λ₯Ό μ¬μ©νκ²λλ©΄ νμν λ·°μλ§ λͺ¨λΈμ μ λ¬νλ©΄ λλ€.
Sources of Truth
SwiftUI λ λκ°μ§ λ°©λ²μΌλ‘ sources of truth λ₯Ό μ 곡νλ€.
- state : view μμλ§ μ¬μ©νλ value νμ , λ©λͺ¨λ¦¬μ ν λΉλκ³ μμ±λλκ±Έ νλ μμν¬μμ κ΄λ¦¬νλ€.
- ObservedObject : view μΈλΆμ λ°μ΄ν°, DB λͺ¨λΈ λ±,, κ°λ°μμ μν΄ κ΄λ¦¬λλ€.
sources of truth κ° λ³΄μ₯λκ³ λλ©΄, reusable component μ μ§μ€ν μ μκ² λλ€.
- λ·° λ΄μμ μ§μ λ°μ΄ν°λ₯Ό λ³κ²½νλ μΌμ΄ μ μ΄μ§κ³ , read-only λ°μ΄ν°λ₯Ό μ νΈνκ² λ κ²μ΄λ€.
- SwiftUI λ΄μμ μΌλ°μ μΈ λ°μ΄ν°μ Environment λ read-only
λ§μ½ κ° λ³κ²½μ΄ νμν κ²½μ°μλ Binding μ μ¬μ©νλ©΄ λλ€.
- State λΏ μλλΌ, ObservedObject, Binding λ³μμ λν΄μλ λ°μΈλ©μ΄ κ°λ₯νλ€.
π State vs ObservedObject
- State property λ λ·° λ΄μμλ§ μ¬μ©λλκ±°λΌ, νλ‘ν νμ΄νμ μ ν©νλ€.
- μΌλ°μ μΌλ‘ λ°μ΄ν°λ DB λ± SwiftUI λ·° μΈλΆμμ κ΄λ¦¬λκΈ° λλ¬Έμ ObservableObject νλ‘ν μ½μ μ±ννλ κ°μ²΄λ‘ κ΄λ¦¬λλ κ²μ΄ μ’λ€.
State μ°κΈ°μ μ μκ°ν΄λ³΄μ π€ μ λ§ μ§μ§ 리μΌλ‘ λ·°μμλ§ κ΄λ¦¬λ λ°μ΄ν°μΈκ°?
- Button μ²λΌ μ¬μ©μκ° λλ μλ νμ΄λΌμ΄νΈ 컬λ¬λ₯Ό μ€μ νλ λ± μ λ§ λ²νΌμ΄λΌλ λ·°μμ κ΄λ¦¬λμ΄μΌνλ λ°μ΄ν°λ State κ° μ μ νλ€.
- νμ§λ§ κ·Έκ² μλλΌλ©΄, ObservableObject μ²λΌ SwiftUI μμ μ 곡λλ λ€λ₯Έ λ°©λ²μ ννμ.
μκ²μ΄ SwiftUI λ‘ μ¬μ¬μ©κ°λ₯ν λ·°λ₯Ό λ§λλ λ°©λ²μ΄κ³ , λͺ¨λ μννΈμ¨μ΄μμ μΌλ°μ μΌλ‘ μ μ©λλ λ°©λ²μ΄λ€.
- λͺ¨λ μννΈμ¨μ΄μλ λ°μ΄ν°κ° μκ³ , λ°μ΄ν°μ μ‘μΈμ€ ν μ μλ€.
- λ°μ΄ν°μ μμ²μ μ΅μννκ³ , μ¬μ¬μ©κ°λ₯ν μ»΄ν¬λνΈλ₯Ό λ§λ¬μΌλ‘μ¨ μ±μ μ 체μ μΈ λ²κ·Έλ₯Ό μ κ±°ν μ μλ€.
- SwiftUI λ₯Ό μ¬μ©νλ€λ©΄ μ΄λ° κ°λ μ μ μ©νλκ² λ§€μ° μ¬μμ§λ€. π
'π iOS > WWDC' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[WWDC18] iOS Memory Deep Dive (2) | 2022.11.27 |
---|---|
[WWDC19] Advances in Collection View Layout - Part 1 (0) | 2022.10.17 |
[WWDC19] Data Flow Through SwiftUI - Part 1 (0) | 2022.08.30 |
[WWDC19] LLDB: Beyond "po" (0) | 2022.07.04 |
[iOS] SnapKit / Then λΌμ΄λΈλ¬λ¦¬λ₯Ό μ΄μ©ν΄ μ½λλ‘ UI ꡬννκΈ° (0) | 2021.11.11 |