You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `Store` class provides UIKit-friendly APIs for subscribing to state changes.
256
+
257
+
### Closure-based subscription
258
+
259
+
```swift
260
+
importUIKit
261
+
importBrownie
262
+
263
+
classMyViewController: UIViewController {
264
+
privatevar store: Store<MyStoreState>?
265
+
privatevar cancelSubscription: (() ->Void)?
266
+
267
+
overridefuncviewDidLoad() {
268
+
super.viewDidLoad()
269
+
270
+
store = StoreManager.get(key: MyStoreState.storeName, as: MyStoreState.self)
271
+
272
+
// Subscribe to all state changes
273
+
cancelSubscription = store?.subscribe { [weakself] state in
274
+
self?.updateUI(with: state)
275
+
}
276
+
}
277
+
278
+
deinit {
279
+
cancelSubscription?()
280
+
}
281
+
}
282
+
```
283
+
284
+
### Subscribe to specific property
285
+
286
+
```swift
287
+
// Only fires when `counter` changes
288
+
cancelSubscription = store?.subscribe(\.counter) { [weakself] counter in
289
+
self?.counterLabel.text="Count: \(counter)"
290
+
}
291
+
```
292
+
293
+
### Combine-based subscription
294
+
295
+
```swift
296
+
importCombine
297
+
298
+
classMyViewController: UIViewController {
299
+
privatevar store: Store<MyStoreState>?
300
+
privatevar cancellables =Set<AnyCancellable>()
301
+
302
+
overridefuncviewDidLoad() {
303
+
super.viewDidLoad()
304
+
305
+
store = StoreManager.get(key: MyStoreState.storeName, as: MyStoreState.self)
306
+
307
+
store?.$state
308
+
.sink { [weakself] state in
309
+
self?.updateUI(with: state)
310
+
}
311
+
.store(in: &cancellables)
312
+
}
313
+
}
314
+
```
315
+
316
+
### Updating state
317
+
318
+
```swift
319
+
// Update with closure
320
+
store?.set { $0.counter+=1 }
321
+
322
+
// Update specific property
323
+
store?.set(\.user.name, to: "New Name")
324
+
```
325
+
326
+
---
327
+
253
328
## Example
254
329
255
330
You can find an example consumer Apple platform app [here](https://github.com/callstack/react-native-brownfield/tree/main/apps/AppleApp) and the consumed library module [here](https://github.com/callstack/react-native-brownfield/tree/main/apps/RNApp/ios).
0 commit comments