DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Avatar

Aleksei Pichukov

Technical PO at Raiffeisen Bank International

DE

Joined Sep 2023

Stats

Reputation: 136
Pageviews: 11.5K
Articles: 5
Comments: 1
  • Articles
  • Comments

Articles

article thumbnail
iOS Application Security for Beginners
This article provides a brief overview of techniques that can be used in your mobile iOS application to keep it secure enough for the vast majority of cases.
March 6, 2024
· 1,625 Views · 1 Like
article thumbnail
Why You Might Need To Know Algorithms as a Mobile Developer: Emoji Example
Explore algorithms and data structure usage with the example of finding emojis for word or phrase visual explanation using a natural language framework.
January 30, 2024
· 2,079 Views · 3 Likes
article thumbnail
Make @Observable Wrapper for Better State Control in Swift
The New Apple Observation framework has some limitations. Let's take a look at a way to keep a value type for the model and use the Observation framework in the Swift application.
December 14, 2023
· 1,991 Views · 1 Like
article thumbnail
How To Integrate a Web Component Into a Mobile App While Preserving Native UX
How to integrate a Web Component into a Mobile (iOS) application and don't break a "Native" UX spirit. What should you look at to keep the app consistent.
October 31, 2023
· 2,285 Views · 2 Likes
article thumbnail
SwiftData Dependency Injection in SwiftUI Application
Refactoring the default SwiftData + SwiftUI project example. Removing SwiftData from SwiftUI and replacing Environment with a vanilla Dependency Injection.
September 27, 2023
· 3,505 Views · 2 Likes

Comments

SwiftData Dependency Injection in SwiftUI Application

Oct 26, 2023 · Aleksei Pichukov

Hello, thanks for your comment. One of the points in the article is that you don't really need to use Environment to pass data through layers in the app. If you need to pass the whole ViewModel to several layers deeper in the view hierarchy, I would double-check if you planned your architecture in this part well enough. The ViewModel should be responsible only for the views it is connected with. When the view is complicated and you are splitting it into several views, you might have a case to pass the ViewModel one layer down (maybe it's better to think about separate view models or states for such cases as well), but you don't need Environment for it. In case you want to use Environment, there is no need to extend EnvironmentValues. All you need is to use the environmentObject function that takes ObservableObject as a parameter and you can have something like this:

```swift

final class ViewModel: ObservableObject { ... }

struct FirstView: View {

// You can have your viewModel injected through init or created here

...

var body: some View {

SecondView()

.environmentObject(viewModel) // <----- here you inject it

}

}

struct SecondView: View {

@EnvironmentObject var viewModel: ViewModel

...

}

```

User has been successfully modified

Failed to modify user

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: