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
Copy file name to clipboardExpand all lines: Lessons/08-Coordinators/README.md
+72-80Lines changed: 72 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,31 +16,31 @@ By the end of this lesson, you should be able to...
16
16
17
17
1. Describe:
18
18
- the **Coordinator** design pattern
19
-
- the software construction problem(s) it is intended to solve
20
-
- potential use cases for it (when to use it; when not to use it)
21
-
- how to implement Coordinator in iOS
19
+
- the problem it is intended to solve
22
20
3. Assess:
23
-
-the suitability of a given design pattern to solve a given problem
21
+
-when to use it
24
22
- the trade offs (pros/cons)
25
-
4.Design and implement basic examples of **Coordinator** explored in this class
23
+
4.Implement basic examples of **Coordinator** explored in this class
26
24
27
25
<!-- > -->
28
26
29
-
## Initial Exercise
27
+
## Initial Exercise (5 min)
30
28
31
-
### As A Class
29
+
As a class:
32
30
33
31
1. Review assignment from last class
34
-
- students demo their solutions
32
+
- students demo their solutions in pairs
35
33
36
34
<!-- > -->
37
35
38
36
### The Coordinator Pattern
39
37
40
-
The idea behind the Coordinator pattern is the creation of a separate entity —__*a Coordinator object*__— which is responsible for the application’s *flow.*
38
+
The idea behind the Coordinator pattern is the creation of a separate entity —__*a Coordinator object*__— which is responsible for the application’s *navigation.*
41
39
42
40
*So what is a coordinator?*
43
41
42
+
<!-- v -->
43
+
44
44
>
45
45
> A **coordinator** is an object that bosses one or more view controllers around.
46
46
>
@@ -51,10 +51,11 @@ The idea behind the Coordinator pattern is the creation of a separate entity &md
Similar to how UIViewControllers manage UIViews, Coordinators can manage UIViewControllers taking all of the driving logic (navigation) out of view controllers and moving it up one level — to a __*Coordinator layer.*__
One of the standard ways to perform navigation on iOS is to use a `UINavigationController` onto which each view controller can either pop or push other view controllers.
When the view controllers themselves must decide the next view controller to push onto `self.navigationController`, the View controllers are too tightly coupled.
79
+
When the view controllers themselves must decide the next view controller to push onto `self.navigationController`, the View controllers are **too tightly coupled**.
81
80
82
-
As an app grows, this approach becomes difficult to deal with: The codebase will be hard to change and maintain, and view controllers are almost impossible to reuse.
81
+
<!-- v -->
83
82
84
-
<!-- > -->
83
+
As an app grows, this approach becomes difficult to deal with:
85
84
86
-
What if...
87
-
-you need to be able to navigate to the same view controller from multiple places?
88
-
-you want to implement something like deep linking from outside your app?
85
+
86
+
-codebase is hard to change and maintain
87
+
-view controllers are hard to reuse
89
88
90
89
<!--
91
90
```Swift
@@ -111,17 +110,17 @@ class MyViewController : UIViewController {
111
110
}
112
111
``` -->
113
112
114
-
<!-->-->
113
+
<!--v-->
115
114
116
-
2.**Massive View Controller Problem**— (see previous lesson for description and example)
115
+
2.**Massive View Controller Problem**
117
116
118
117
3.**Reusability**— with standard MVC, View Controllers are nearly impossible to reuse, especially if overloaded with non-VC tasks.
119
118
120
119
4.**Tight Coupling**— an inflexible design in which every controller needs to know details about other controllers inhibits an app's growth.
121
120
122
-
<!-->-->
121
+
<!--v-->
123
122
124
-
5.**Deep Linking Issues**—*(see Coordinators and Deep Linking section)*
123
+
5.**Deep Linking Issues**
125
124
126
125
6.**Testing**— if you want to generate different views or a different flow in response to user choices (A/B testing) or to handle layout variations, standard MVC implementations of VCs make it extremely difficult to test those scenarios.
127
126
@@ -130,9 +129,7 @@ class MyViewController : UIViewController {
130
129
### Implementation Notes
131
130
132
131
A solid, basic implementation of coordinators includes 3 main steps:
133
-
1. Design two protocols:
134
-
-__*Coordinator Protocol*__ - To be used by all our coordinators.
135
-
-__*View Controller Creation Protocol*__ - To facilitate view controller creation.
132
+
1. Design a **Coordinator protocol**
136
133
2. Create a __*main coordinator*__ that will control app flow. Start it when your app launches.
137
134
3. Create/present view controllers.
138
135
@@ -141,13 +138,12 @@ A solid, basic implementation of coordinators includes 3 main steps:
141
138
**About the Coordinator Protocol**
142
139
143
140
All coordinators will conform to this protocol. At bare minimum, it should include:
144
-
- A property to store any **child coordinators**. Coordinator responsibility is to **handle navigation flow**: the same way that UINavigationController keeps reference of its stack, Coordinators do the same with their children. (optional)
145
-
- A property to store the **navigation controller** being used to present view controllers. (Even if you don’t show the navigation bar at the top, using a navigation controller is the easiest way to present view controllers.)
141
+
- A property to store the **navigation controller** being used to present view controllers.
146
142
- A `start()` function to make the coordinator take control. This allows us to create a coordinator fully and activate it only when we’re ready.
143
+
- A property to store any **child coordinators**. Coordinator responsibility is to **handle navigation flow**: the same way that UINavigationController keeps reference of its stack, Coordinators do the same with their children. (optional)
147
144
148
145
<!-- > -->
149
146
150
-
151
147
#### Example:
152
148
153
149
1. Coordinator protocol created
@@ -156,7 +152,7 @@ All coordinators will conform to this protocol. At bare minimum, it should inclu
156
152
protocolCoordinator {
157
153
funcstart()
158
154
var navigationController: UINavigationController { getset }
159
-
var childCoordinators : [Coordinator] { getset }//not always
155
+
//var childCoordinators : [Coordinator] { get set }
160
156
}
161
157
```
162
158
@@ -165,7 +161,9 @@ protocol Coordinator {
165
161
2. For building the user flow, a concrete implementation of the protocol is created:
166
162
167
163
```Swift
168
-
classBaseCoordinator: Coordinator {
164
+
classMainCoordinator: Coordinator {
165
+
166
+
//var childCoordinators: [Coordinator] = []
169
167
170
168
var navigationController: UINavigationController
171
169
privatevar someViewController: UIViewController?
@@ -185,60 +183,58 @@ class BaseCoordinator: Coordinator {
185
183
<!-- > -->
186
184
187
185
3. All view controllers targeted for participation in the coordinated navigation approach must:
188
-
- Have a `BaseCoordinator` property for access to its properties and functions
186
+
- Have a `MainCoordinator` property for access to its properties and functions
Follow [these instructions]() to create a flow using coordinators.
218
+
Code Demo
217
219
218
220
<!-- > -->
219
221
220
222
## Benefits
221
223
222
-
They can have huge impact on cleaning the code base and on making view controllers more loosely coupled from each other.
223
-
224
-
The pattern can be adopted for only part of an app or used across the entire application.
225
-
226
-
<!-- > -->
227
-
228
224
Coordinators create a well defined way to deal with navigation in which view controllers are:
229
225
1. Isolated from each other (do not need to know about each other)
230
-
2. Reusable in different contexts
231
-
3. Lightweight, less "massive," and focused on their key responsibilities
226
+
2. Reusable
227
+
3. Lightweight and focused on their key responsibilities
232
228
233
229
<!-- > -->
234
230
235
231
## Pitfalls
236
232
237
233
The downside of the Coordinator pattern:
238
234
239
-
1.**Back Button Issue**— When the user navigates back, developers must ensure that the right coordinator is released. Solutions for this might come with potential loss of built-in framework features.
235
+
1.More code 😅
240
236
241
-
2.**Overkill**— The pattern could well take too much work for very simple apps. Many extra classes will need to be created upfront.
237
+
2.Can get complex as the app goes to deeper levels in the stack
242
238
243
239
<!-- > -->
244
240
@@ -259,14 +255,14 @@ This requires one high-level coordinator instantiated in the `AppDelegate` that
259
255
260
256
The `AppCoordinator` holds an array of child coordinators, who in turn might hold an array of their own child coordinators.
261
257
262
-
This is especially useful in a TabBar app: each TabBar scene's navigation controller could get its own coordinator for directing its own behavior and flow. And each coordinator can be spawned by its parent coordinator.
258
+
This is especially useful in a **TabBar** app: each TabBar scene's navigation controller could get its own coordinator for directing its own behavior and flow. And each coordinator can be spawned by its parent coordinator.
@@ -284,26 +280,17 @@ A common way to do this on iOS is to define a URL scheme that other apps can the
284
280
285
281
<!-- > -->
286
282
287
-
__*The Obstacle*__
288
-
289
-
To support deep linking in an iOS app, you might need to open specific views in the app, regardless of the navigation history or navigation design.
290
-
291
-
With standard MVC and its potential to tightly-couple View Controllers, it is very difficult to implement deep linking.
292
-
293
-
<!-- > -->
294
-
295
283
__*The Solution*__
296
284
297
285
Using Coordinators and navigator objects, implementing URL and deep linking support becomes a lot easier because it allows for dedicated navigation points in which URL handling logic can be injected.
2.[The Coordinator - an aricle](http://khanlou.com/2015/01/the-coordinator/), by Soroush Khanlou, <sup>1</sup> who popularized the pattern for iOS development.
366
+
2.[The Coordinator - an article](http://khanlou.com/2015/01/the-coordinator/), by Soroush Khanlou, <sup>1</sup> who popularized the pattern for iOS development.
375
367
3.[Coordinators - video presentation](https://vimeo.com/144116310)
376
368
4.[Coordinators - a slideshare from LinkedIn](https://www.slideshare.net/secret/3jJlEE1weo0RRl)
377
369
5.[8 Patterns to Help You Destroy Massive View Controller - an article](http://khanlou.com/2014/09/8-patterns-to-help-you-destroy-massive-view-controller/)
@@ -386,7 +378,7 @@ This exercise follows on today's Activity 1...
0 commit comments