Skip to content

Commit cc52438

Browse files
committed
alternatives to notification registration; more async await updates
1 parent ec511df commit cc52438

File tree

17 files changed

+814
-8
lines changed

17 files changed

+814
-8
lines changed

bk1ch05zp05MakeYourOwnAsyncStream/MakeYourOwnAsyncStream/Base.lproj/Main.storyboard

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@
2222
<fontDescription key="fontDescription" type="system" pointSize="14"/>
2323
<textInputTraits key="textInputTraits"/>
2424
</textField>
25+
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="4rH-mb-Vcs">
26+
<rect key="frame" x="34" y="204" width="153" height="31"/>
27+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
28+
<state key="normal" title="Button"/>
29+
<buttonConfiguration key="configuration" style="plain" title="Cancel"/>
30+
<connections>
31+
<action selector="doCancel:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Rak-05-iRx"/>
32+
</connections>
33+
</button>
2534
</subviews>
2635
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
2736
<color key="backgroundColor" systemColor="systemBackgroundColor"/>

bk1ch05zp05MakeYourOwnAsyncStream/MakeYourOwnAsyncStream/ViewController.swift

+23-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class TextFieldSelectionChangeStreamer: NSObject, UITextFieldDelegate {
99
var myContinuation : AsyncStream<UITextField>.Continuation?
1010
self.values = AsyncStream { continuation in
1111
myContinuation = continuation
12+
// I also tried to set the `onTermination` handler but couldn't; filed a bug
13+
// ooo, Tyler Prevost has a workaround:
14+
continuation.onTermination = { term in
15+
print("terminated!")
16+
} as (@Sendable (AsyncStream<UITextField>.Continuation.Termination) -> Void)
1217
}
1318
super.init()
1419
self.continuation = myContinuation
@@ -22,15 +27,32 @@ class TextFieldSelectionChangeStreamer: NSObject, UITextFieldDelegate {
2227
class ViewController: UIViewController {
2328
let textFieldSelectionChangeStreamer = TextFieldSelectionChangeStreamer()
2429
@IBOutlet var textField : UITextField?
30+
var task : Task<Void, Never>?
2531
override func viewDidLoad() {
2632
super.viewDidLoad()
2733
self.textField?.delegate = self.textFieldSelectionChangeStreamer
28-
Task {
34+
let task = Task {
2935
for await textField in self.textFieldSelectionChangeStreamer.values {
3036
print(textField.text ?? "")
3137
}
38+
// let's unroll that so we can see what's actually happening
39+
// var iter = self.textFieldSelectionChangeStreamer.values.makeAsyncIterator()
40+
// for _ in 1...Int.max {
41+
// if let tf = await iter.next() {
42+
// print(tf.text ?? "")
43+
// } else {
44+
// print("I got nil")
45+
// break
46+
// }
47+
// }
3248
}
49+
self.task = task
3350
}
51+
// prove that an async stream is self-cancelling
52+
@IBAction func doCancel (_ sender:Any) {
53+
self.task?.cancel()
54+
}
55+
3456

3557

3658
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,42 @@
11

22

33
import UIKit
4+
import Combine
45

56
class ViewController2: UIViewController {
67

78
static let notif = Notification.Name("notif")
89

10+
var which = 1
911

12+
// let's also add some alternative ways of getting notifications, namely by subscribing
13+
var storage = Set<AnyCancellable>()
1014
override func viewDidAppear(_ animated: Bool) {
1115
super.viewDidAppear(animated)
12-
print("registering observer")
13-
let _ = NotificationCenter.default.addObserver(forName: ViewController2.notif, object: nil, queue: nil) { _ in
14-
print("notification received")
16+
17+
switch which {
18+
case 0:
19+
print("registering observer")
20+
let _ = NotificationCenter.default.addObserver(forName: ViewController2.notif, object: nil, queue: nil) { _ in
21+
print("notification received")
22+
}
23+
case 1:
24+
print("subscribing with Combine")
25+
NotificationCenter.default.publisher(for: ViewController2.notif)
26+
.sink { _ in print("notification received") }
27+
.store(in: &self.storage)
28+
case 2:
29+
print("subscribing with async stream")
30+
let stream = NotificationCenter.default.notifications(named: ViewController2.notif)
31+
Task {
32+
for await _ in stream {
33+
print("notification received")
34+
}
35+
}
36+
default: break
1537
}
38+
39+
1640
}
1741

1842
}

0 commit comments

Comments
 (0)