Intro to iOS Application Architecture
-
Upload
make-school -
Category
Software
-
view
912 -
download
1
Transcript of Intro to iOS Application Architecture
![Page 1: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/1.jpg)
![Page 2: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/2.jpg)
INTRODUCTION TO ARCHITECTING YOUR IOS APP
![Page 3: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/3.jpg)
AGENDA
Goals of software architecture
Design guidelines
Practical tips
![Page 4: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/4.jpg)
GOALS OF SOFTWARE ARCHITECTURE
![Page 5: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/5.jpg)
GOALS OF SOFTWARE ARCHITECTURE
Code is comprehensible for other developers
Code can adopt to changing requirements
Code can be shared within the project and throughout
multiple projects
![Page 6: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/6.jpg)
THINK IN DOMAINS NOT IN OBJECTS
![Page 7: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/7.jpg)
DOMAINS NOT OBJECTS
Business Logic
PersistenceUI/UX Code
NetworkingReusability
User Object
Trip Object
![Page 8: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/8.jpg)
DOMAINS NOT OBJECTS
Business Logic
Persistence
UI/UX Code
Networking
Reusability
User Object
Trip Object
User View
Trip View
![Page 9: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/9.jpg)
SINGLE RESPONSIBILITY PRINCIPLE
![Page 10: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/10.jpg)
SINGLE RESPONSIBILITY PRINCIPLE
Avoid having many responsibilities within a single class
Very important when implementing UIViewController subclasses
Bad View Controller handles: Persistence, Business
Logic, View Logic, etc.
![Page 11: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/11.jpg)
ENCAPSULATE
![Page 12: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/12.jpg)
ENCAPSULATE
Every component should know as little as possible
about its surrounding components → reduces dependencies between components
How can this be accomplished?→ small interfaces for communication
![Page 13: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/13.jpg)
ENCAPSULATE
Add Trip View Controller Core Data Client
startSavingNewTrip(newTrip)
displayError(nameError)
setNameOfTrip(“SF Trip”)
disableSaveButton()
Classes are poorly encapsulated and very dependent on each other - very hard to change code in any of the two classes
![Page 14: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/14.jpg)
ENCAPSULATE
Add Trip View Controller Core Data Client
saveTrip(newTrip)
saveErrorOccurred(error)
Small communication interface reduces dependencies!
![Page 15: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/15.jpg)
PRACTICAL TIPS
![Page 16: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/16.jpg)
#1 AVOID THE MASSIVE VIEW CONTROLLER
![Page 17: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/17.jpg)
AVOID THE MASSIVE VIEW CONTROLLER
Rule of thumb: A View Controller with > 300 lines is
probably doing more than it should do
![Page 18: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/18.jpg)
THINGS A VIEW CONTROLLER SHOULD DO
Listen to callbacks from the View → invoke methods on
the model layer → send responses from model back to
the view
![Page 19: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/19.jpg)
THINGS A VIEW CONTROLLER SHOULD NOT DO
Construct complex network requests
Construct complex database queries
Take care of object serialization / deserialization
![Page 20: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/20.jpg)
#2 DEFINE EXPRESSIVE APIS
![Page 21: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/21.jpg)
DEFINE EXPRESSIVE APIS
E.g. instead of exposing details of the network layer,
provide functions with a simple interface:
func fetchAllUsers(callback: [User] -> ()) fetchAllUsers { users in print(users)}
![Page 22: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/22.jpg)
#3 USE VIEW OBJECTS
![Page 23: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/23.jpg)
USE VIEW OBJECTS
View objects encapsulate multiple properties that are
relevant for a type to be displayed by a custom view
This is preferred over setting individual properties from
outside the view
![Page 24: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/24.jpg)
USE VIEW OBJECTSclass UserView: UIView {
@IBOutlet var nameLabel: UILabel! @IBOutlet var profilePictureImageView: UIImageView! var user: User? { didSet { nameLabel.text = user?.name profilePictureImageView.image = user?.profilePicture } } }
let userView = UserView()let currentUser = User()currentUser.name = "TestUser"
userView.user = currentUser
View encapsulates how an object is displayed!
![Page 25: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/25.jpg)
USE VIEW OBJECTS
class UserView: UIView {
@IBOutlet var nameLabel: UILabel! @IBOutlet var profilePictureImageView: UIImageView! }
let userView = UserView()let currentUser = User()currentUser.name = "TestUser"
userView.nameLabel.text = currentUser.nameuserView.profilePictureImageView.image = currentUser.profilePicture
Code that uses UserView nowdepends on its UI components.Violates encapsulation!
![Page 26: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/26.jpg)
#4 BE WARY OF INHERITANCE
![Page 27: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/27.jpg)
BE WARY OF INHERITANCE
Base View Class
Trip View
You can only inherit one set of functionality
You tend to inherit functionality that you don’t need
More modular alternatives:
Functions that can operate on relevant types (using generics)
Protocols & Protocol Extensions
![Page 28: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/28.jpg)
BE WARY OF INHERITANCE
Base View Class
Trip View
More Specific Trip View
Trip View
ErrorRepresentable
Restorable Presentable
![Page 29: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/29.jpg)
SUMMARY
![Page 30: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/30.jpg)
SUMMARYDivide code into logical domains
Strive for each unit of code having a single responsibility
Consider using protocols instead of inheritance
Define narrow and expressive APIs that hide
implementation details of other units of code and
reduce dependencies between your units
![Page 31: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/31.jpg)
ADDITIONAL RESOURCES
![Page 32: Intro to iOS Application Architecture](https://reader034.fdocuments.in/reader034/viewer/2022052405/58efad651a28abaa7b8b45db/html5/thumbnails/32.jpg)
ADDITIONAL RESOURCES
Talk: Refactor the Mega-Controller
WWDC 2015: Protocol Oriented Programming
WWDC 2014: Advanced Application Architecture
Ray Wenderlich: Protocol Oriented programming