Skip to content

Commit 3266532

Browse files
committed
Chapter 12, alternatives to notification registration
1 parent cc52438 commit 3266532

File tree

11 files changed

+52
-3
lines changed

11 files changed

+52
-3
lines changed

bk1ch12p490autoreleasepool/bk1ch12p490autoreleasepool.xcodeproj/project.pbxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@
277277
buildSettings = {
278278
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
279279
INFOPLIST_FILE = bk1ch12p490autoreleasepool/Info.plist;
280-
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
280+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
281281
LD_RUNPATH_SEARCH_PATHS = (
282282
"$(inherited)",
283283
"@executable_path/Frameworks",
@@ -294,7 +294,7 @@
294294
buildSettings = {
295295
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
296296
INFOPLIST_FILE = bk1ch12p490autoreleasepool/Info.plist;
297-
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
297+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
298298
LD_RUNPATH_SEARCH_PATHS = (
299299
"$(inherited)",
300300
"@executable_path/Frameworks",

bk1ch12p495danglingPointer/bk1ch12p495danglingPointer.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
274274
DEVELOPMENT_TEAM = W3LHX5RGV2;
275275
INFOPLIST_FILE = bk1ch12p495danglingPointer/Info.plist;
276+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
276277
LD_RUNPATH_SEARCH_PATHS = (
277278
"$(inherited)",
278279
"@executable_path/Frameworks",
@@ -289,6 +290,7 @@
289290
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
290291
DEVELOPMENT_TEAM = W3LHX5RGV2;
291292
INFOPLIST_FILE = bk1ch12p495danglingPointer/Info.plist;
293+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
292294
LD_RUNPATH_SEARCH_PATHS = (
293295
"$(inherited)",
294296
"@executable_path/Frameworks",

bk1ch12p497notificationCrasherNot/ch12p325NotificationCrasherNot.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
GCC_PRECOMPILE_PREFIX_HEADER = YES;
268268
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
269269
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
270+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
270271
LD_RUNPATH_SEARCH_PATHS = (
271272
"$(inherited)",
272273
"@executable_path/Frameworks",
@@ -289,6 +290,7 @@
289290
GCC_PRECOMPILE_PREFIX_HEADER = YES;
290291
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
291292
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
293+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
292294
LD_RUNPATH_SEARCH_PATHS = (
293295
"$(inherited)",
294296
"@executable_path/Frameworks",

bk1ch12p497notificationLeaker/ch12p325NotificationLeaker.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
GCC_PRECOMPILE_PREFIX_HEADER = YES;
268268
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
269269
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
270+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
270271
LD_RUNPATH_SEARCH_PATHS = (
271272
"$(inherited)",
272273
"@executable_path/Frameworks",
@@ -289,6 +290,7 @@
289290
GCC_PRECOMPILE_PREFIX_HEADER = YES;
290291
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
291292
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
293+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
292294
LD_RUNPATH_SEARCH_PATHS = (
293295
"$(inherited)",
294296
"@executable_path/Frameworks",

bk1ch12p497notificationLeakerSolution/ch12p325NotificationLeaker.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
GCC_PRECOMPILE_PREFIX_HEADER = YES;
268268
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
269269
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
270+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
270271
LD_RUNPATH_SEARCH_PATHS = (
271272
"$(inherited)",
272273
"@executable_path/Frameworks",
@@ -289,6 +290,7 @@
289290
GCC_PRECOMPILE_PREFIX_HEADER = YES;
290291
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
291292
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
293+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
292294
LD_RUNPATH_SEARCH_PATHS = (
293295
"$(inherited)",
294296
"@executable_path/Frameworks",

bk1ch12p497notificationLeakerSolution/ch12p325NotificationLeaker/FlipsideViewController.swift

+20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22

33
import UIKit
4+
import Combine
45

56
protocol FlipsideViewControllerDelegate : AnyObject {
67
func flipsideViewControllerDidFinish(_ controller:FlipsideViewController)
@@ -17,9 +18,19 @@ class FlipsideViewController: UIViewController {
1718
weak var delegate : FlipsideViewControllerDelegate!
1819

1920
var observers = Set<NSObject>()
21+
var storage = Set<AnyCancellable>()
22+
// async task
23+
var task = Task {
24+
let stream = NotificationCenter.default.notifications(named: .woohoo)
25+
for await _ in stream {
26+
print("the observer still exists!", self)
27+
}
28+
}
2029

2130
override func viewWillAppear(_ animated: Bool) {
2231
super.viewWillAppear(animated)
32+
33+
// register observer
2334
let ob = NotificationCenter.default.addObserver(
2435
forName: .woohoo, object:nil, queue:nil) {
2536
[unowned self] // *
@@ -28,6 +39,14 @@ class FlipsideViewController: UIViewController {
2839
print(self.description) // leak me, leak me
2940
}
3041
self.observers.insert(ob as! NSObject)
42+
43+
// combine pipeline
44+
NotificationCenter.default.publisher(for: .woohoo)
45+
.sink {
46+
[unowned self] // *
47+
_ in
48+
print("The observer still exists!", self) }
49+
.store(in: &self.storage)
3150
}
3251

3352
override func viewDidDisappear(_ animated: Bool) {
@@ -45,6 +64,7 @@ class FlipsideViewController: UIViewController {
4564
for ob in self.observers {
4665
NotificationCenter.default.removeObserver(ob) // *
4766
}
67+
self.task.cancel() // *
4868
}
4969

5070
}

bk1ch12p497notificationLeakerSolution/ch12p325NotificationLeaker/MainViewController.swift

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import UIKit
22

3+
extension Task where Success == Never, Failure == Never {
4+
static func sleep(_ seconds:Double) async {
5+
await self.sleep(UInt64(seconds * 1_000_000_000))
6+
}
7+
static func sleepThrowing(_ seconds:Double) async throws {
8+
try await self.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))
9+
}
10+
}
11+
12+
313
class MainViewController: UIViewController, FlipsideViewControllerDelegate {
414

515
func flipsideViewControllerDidFinish(_ controller:FlipsideViewController) {
@@ -15,7 +25,10 @@ class MainViewController: UIViewController, FlipsideViewControllerDelegate {
1525
}
1626

1727
@IBAction func woohoo(_ sender : Any) {
18-
NotificationCenter.default.post(name: .woohoo, object: nil)
28+
Task {
29+
await Task.sleep(2.0)
30+
NotificationCenter.default.post(name: .woohoo, object: nil)
31+
}
1932
}
2033

2134
}

bk1ch12p498timerLeaker/ch12p325NotificationLeaker.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
GCC_PRECOMPILE_PREFIX_HEADER = YES;
268268
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
269269
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
270+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
270271
LD_RUNPATH_SEARCH_PATHS = (
271272
"$(inherited)",
272273
"@executable_path/Frameworks",
@@ -289,6 +290,7 @@
289290
GCC_PRECOMPILE_PREFIX_HEADER = YES;
290291
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
291292
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
293+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
292294
LD_RUNPATH_SEARCH_PATHS = (
293295
"$(inherited)",
294296
"@executable_path/Frameworks",

bk1ch12p498timerLeaker2/ch12p325NotificationLeaker.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@
267267
GCC_PRECOMPILE_PREFIX_HEADER = YES;
268268
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
269269
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
270+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
270271
LD_RUNPATH_SEARCH_PATHS = (
271272
"$(inherited)",
272273
"@executable_path/Frameworks",
@@ -289,6 +290,7 @@
289290
GCC_PRECOMPILE_PREFIX_HEADER = YES;
290291
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
291292
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
293+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
292294
LD_RUNPATH_SEARCH_PATHS = (
293295
"$(inherited)",
294296
"@executable_path/Frameworks",

bk1ch12p499cancelableTimer/ch12p325NotificationLeaker.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@
275275
GCC_PRECOMPILE_PREFIX_HEADER = YES;
276276
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
277277
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
278+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
278279
LD_RUNPATH_SEARCH_PATHS = (
279280
"$(inherited)",
280281
"@executable_path/Frameworks",
@@ -297,6 +298,7 @@
297298
GCC_PRECOMPILE_PREFIX_HEADER = YES;
298299
GCC_PREFIX_HEADER = "ch12p325NotificationLeaker/ch12p325NotificationLeaker-Prefix.pch";
299300
INFOPLIST_FILE = ch12p325NotificationLeaker/Info.plist;
301+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
300302
LD_RUNPATH_SEARCH_PATHS = (
301303
"$(inherited)",
302304
"@executable_path/Frameworks",

bk1ch12p500MapTableExample/MapTableExample.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@
279279
buildSettings = {
280280
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
281281
INFOPLIST_FILE = MapTableExample/Info.plist;
282+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
282283
LD_RUNPATH_SEARCH_PATHS = (
283284
"$(inherited)",
284285
"@executable_path/Frameworks",
@@ -294,6 +295,7 @@
294295
buildSettings = {
295296
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
296297
INFOPLIST_FILE = MapTableExample/Info.plist;
298+
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
297299
LD_RUNPATH_SEARCH_PATHS = (
298300
"$(inherited)",
299301
"@executable_path/Frameworks",

0 commit comments

Comments
 (0)