SlideShare a Scribd company logo
Andrei Solovev, iOS Tech Lead at Seven Peaks Software
SwiftUI
Performance and Memory Management
SwiftUI
SwiftUI automatically updates the a
ff
ected parts of the interface. As a result, the
framework automatically performs most of the work traditionally done by view
controllers.
Topic
• ObjectWillChange and @Publishe
d

• NavigationLink performanc
e

• Creating @ObservedObject with parameters
ObjectWillChange
• ObservableObjec
t

• @StateObject and objectWillChang
e

• @StateObject and @Publishe
d

• @ObservedObject and @Publishe
d

• Unused @ObservedObject
ObservableObject
A type of object with a publisher that emits before the object has changed.
 

By default an ObservableObject synthesizes an objectWillChange publisher that emits the changed value
before any of its @Published properties changes
.

class Contact: ObservableObject {

@Published var name: String

}

https://developer.apple.com/documentation/Combine/ObservableObject
@StateObject and objectWillChange
objectWillChange

A publisher that emits before the object has changed.

var objectWillChange: Self.ObjectWillChangePublisher { get }

https://developer.apple.com/documentation/combine/observableobject/
objectwillchange-2oa5v
@StateObject and @Published
Avoid the overhead of a published property when you don’t need it. Only publish
properties that both can change and that matter to the user interface.
@ObservedObject and @Published
SwiftUI might create or recreate a view at any time, so it’s important that initializing a
view with a given set of inputs always results in the same view. As a result, it’s
unsafe to create an observed object inside a view.
Unused @ObservedObject
SwiftUI monitors observable object changes, not a particular property and View will be
updated even if you don’t use the property that changed
NavigationLink
A view that controls a navigation presentation. 

A NavigationLink de
fi
nes a destination for a navigation-based interface and allows the user to
perform that navigation. Optionally, you can use the NavigationLink to perform a navigation
programmatically

• LazyVie
w

• Deep Navigatio
n
LazyView
To solve the problem when a parent View creates all children Views that could be used
for navigation we can introduce LazyView, that would take factory closure that creates a
new View and execute it on rendering, means only when this view is going to be
presented on the screen.
Deep Navigation
Sometimes there are multi-step flow when you have to present multiple Views with next
button and then do some action on the last screen, most common case it’s a Sign Up
where used could enter Email on first screen, Password on seconds, then OTP and
Success in the end.
Creating @ObservedObject with parameters
• @StateObjec
t

• @ObservedObjec
t

• Caching @ObservedObjec
t
@StateObject
A property wrapper type that instantiates an observable object.
 

Create a state object in a View, App, or Scene by applying the @StateObject attribute to
a property declaration and providing an initial value that conforms to the
ObservableObject protocol
:

https://developer.apple.com/documentation/swiftui/stateobject
@ObservedObject
A property wrapper type that subscribes to an @observable object and invalidates a
view whenever the observable object changes
.

https://developer.apple.com/documentation/swiftui/observedobject
Caching @ObservedObject
Everytime @ObservableObject emits changes every View that owns this object as
@EnvironmentObject, @StateObject or @ObservedObject will be recreated and the
body will be executed automatically by SwiftUI, even if this view doesn’t use the property
that was changed. It could be done multiple times per user action or some event
triggered.
That means we have to keep View’s init() and body as simple as possible and keep free from any kind of calculations and object creations.

More Related Content

PDF
MVVM with SwiftUI and Combine
PPTX
Flutter
PPTX
Flutter workshop
PPTX
core java
PPTX
Asynchronous programming in C#
PPTX
Android jetpack compose | Declarative UI
PPTX
React state
PDF
Jetpack Compose beginner.pdf
MVVM with SwiftUI and Combine
Flutter
Flutter workshop
core java
Asynchronous programming in C#
Android jetpack compose | Declarative UI
React state
Jetpack Compose beginner.pdf

What's hot (20)

PPT
Spring ppt
PPTX
Deep dive into swift UI
PDF
Introduction to flutter
PPTX
PPTX
Jetpack Compose.pptx
PPTX
Introduction to Spring Framework
PDF
WEB DEVELOPMENT USING REACT JS
PDF
API Integration For Building Software Applications Powerpoint Presentation Sl...
PDF
Jetpack Compose a new way to implement UI on Android
PPTX
Types of Drivers in JDBC
PDF
Android Jetpack Compose - Turkey 2021
PPTX
Reactjs
PPTX
Servlets
PPTX
React JS - A quick introduction tutorial
PPTX
Dart and Flutter Basics.pptx
PPTX
Spring Framework
PPTX
Presentation on Core java
PDF
Pune Flutter Presents - Flutter 101
PPTX
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Spring ppt
Deep dive into swift UI
Introduction to flutter
Jetpack Compose.pptx
Introduction to Spring Framework
WEB DEVELOPMENT USING REACT JS
API Integration For Building Software Applications Powerpoint Presentation Sl...
Jetpack Compose a new way to implement UI on Android
Types of Drivers in JDBC
Android Jetpack Compose - Turkey 2021
Reactjs
Servlets
React JS - A quick introduction tutorial
Dart and Flutter Basics.pptx
Spring Framework
Presentation on Core java
Pune Flutter Presents - Flutter 101
Unit 1 - TypeScript & Introduction to Angular CLI.pptx
Ad

Similar to SwiftUI - Performance and Memory Management (20)

PDF
Сергій Міськів, «SwiftUI: Animations»
PPTX
Lessons-Learned-SwiftUI.pptx
PDF
Property wrapper and how to use them with mvvm in swift ui i copy
PDF
Prescribing RX Responsibly
PDF
What 100M downloads taught us about iOS architectures
PDF
Some Stuff I was thinking about state machines and types
PDF
Distributing information on iOS
PDF
Swift
PDF
Swift ui userinput
PDF
Do iOS Presentation - Mobile app architectures
PDF
Using a model view-view model architecture for iOS apps
PDF
Unidirectional Data Flow Architecture (Redux) in Swift
PDF
Introduction of Xcode
PDF
RxSwift
PPTX
iOS Architectures
PPTX
iOS Beginners Lesson 2
PDF
Firebase for Apple Developers - SwiftHeroes
PDF
SwiftUI and Combine All the Things
PDF
ReRxSwift
PDF
Intro to UIKit • Made by Many
Сергій Міськів, «SwiftUI: Animations»
Lessons-Learned-SwiftUI.pptx
Property wrapper and how to use them with mvvm in swift ui i copy
Prescribing RX Responsibly
What 100M downloads taught us about iOS architectures
Some Stuff I was thinking about state machines and types
Distributing information on iOS
Swift
Swift ui userinput
Do iOS Presentation - Mobile app architectures
Using a model view-view model architecture for iOS apps
Unidirectional Data Flow Architecture (Redux) in Swift
Introduction of Xcode
RxSwift
iOS Architectures
iOS Beginners Lesson 2
Firebase for Apple Developers - SwiftHeroes
SwiftUI and Combine All the Things
ReRxSwift
Intro to UIKit • Made by Many
Ad

Recently uploaded (20)

PPT
Teaching material agriculture food technology
PDF
Machine learning based COVID-19 study performance prediction
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Approach and Philosophy of On baking technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Machine Learning_overview_presentation.pptx
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
sap open course for s4hana steps from ECC to s4
Teaching material agriculture food technology
Machine learning based COVID-19 study performance prediction
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Electronic commerce courselecture one. Pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Approach and Philosophy of On baking technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Machine Learning_overview_presentation.pptx
The AUB Centre for AI in Media Proposal.docx
Per capita expenditure prediction using model stacking based on satellite ima...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Assigned Numbers - 2025 - Bluetooth® Document
Unlocking AI with Model Context Protocol (MCP)
Chapter 3 Spatial Domain Image Processing.pdf
MYSQL Presentation for SQL database connectivity
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Programs and apps: productivity, graphics, security and other tools
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
sap open course for s4hana steps from ECC to s4

SwiftUI - Performance and Memory Management

  • 1. Andrei Solovev, iOS Tech Lead at Seven Peaks Software SwiftUI Performance and Memory Management
  • 2. SwiftUI SwiftUI automatically updates the a ff ected parts of the interface. As a result, the framework automatically performs most of the work traditionally done by view controllers.
  • 3. Topic • ObjectWillChange and @Publishe d • NavigationLink performanc e • Creating @ObservedObject with parameters
  • 4. ObjectWillChange • ObservableObjec t • @StateObject and objectWillChang e • @StateObject and @Publishe d • @ObservedObject and @Publishe d • Unused @ObservedObject
  • 5. ObservableObject A type of object with a publisher that emits before the object has changed. By default an ObservableObject synthesizes an objectWillChange publisher that emits the changed value before any of its @Published properties changes . class Contact: ObservableObject { @Published var name: String } https://developer.apple.com/documentation/Combine/ObservableObject
  • 6. @StateObject and objectWillChange objectWillChange A publisher that emits before the object has changed. var objectWillChange: Self.ObjectWillChangePublisher { get } https://developer.apple.com/documentation/combine/observableobject/ objectwillchange-2oa5v
  • 7. @StateObject and @Published Avoid the overhead of a published property when you don’t need it. Only publish properties that both can change and that matter to the user interface.
  • 8. @ObservedObject and @Published SwiftUI might create or recreate a view at any time, so it’s important that initializing a view with a given set of inputs always results in the same view. As a result, it’s unsafe to create an observed object inside a view.
  • 9. Unused @ObservedObject SwiftUI monitors observable object changes, not a particular property and View will be updated even if you don’t use the property that changed
  • 10. NavigationLink A view that controls a navigation presentation. A NavigationLink de fi nes a destination for a navigation-based interface and allows the user to perform that navigation. Optionally, you can use the NavigationLink to perform a navigation programmatically • LazyVie w • Deep Navigatio n
  • 11. LazyView To solve the problem when a parent View creates all children Views that could be used for navigation we can introduce LazyView, that would take factory closure that creates a new View and execute it on rendering, means only when this view is going to be presented on the screen.
  • 12. Deep Navigation Sometimes there are multi-step flow when you have to present multiple Views with next button and then do some action on the last screen, most common case it’s a Sign Up where used could enter Email on first screen, Password on seconds, then OTP and Success in the end.
  • 13. Creating @ObservedObject with parameters • @StateObjec t • @ObservedObjec t • Caching @ObservedObjec t
  • 14. @StateObject A property wrapper type that instantiates an observable object. Create a state object in a View, App, or Scene by applying the @StateObject attribute to a property declaration and providing an initial value that conforms to the ObservableObject protocol : https://developer.apple.com/documentation/swiftui/stateobject
  • 15. @ObservedObject A property wrapper type that subscribes to an @observable object and invalidates a view whenever the observable object changes . https://developer.apple.com/documentation/swiftui/observedobject
  • 16. Caching @ObservedObject Everytime @ObservableObject emits changes every View that owns this object as @EnvironmentObject, @StateObject or @ObservedObject will be recreated and the body will be executed automatically by SwiftUI, even if this view doesn’t use the property that was changed. It could be done multiple times per user action or some event triggered. That means we have to keep View’s init() and body as simple as possible and keep free from any kind of calculations and object creations.