nalexn / clean-architecture-swiftui

SwiftUI sample app using Clean Architecture. Examples of working with CoreData persistence, networking, dependency injection, unit testing, and more.
MIT License
5.57k stars 671 forks source link

@Enviroment vs @EnviromentObject #16

Closed ramtest55 closed 3 years ago

ramtest55 commented 4 years ago

I am pretty new to iOS development and I don't have background of Javascript and UIKit. I am learning SwiftUI directly.

Why did you used @Enviroment and not @EnviromentObject in your project? Can you please add comments in your code so it will more useful for beginners like me.

Thanks for creating this project and post related to it. It was very helpful and appreciated :)

nalexn commented 4 years ago

Hi @ramtest55 This app was using @EnviromentObject until I discovered certain performance issues. If you need I can search for the last commit in this repo with @EnviromentObject

nalexn commented 4 years ago

Good point about the comments. Could you give me a hint on which places lack them the most?

ramtest55 commented 4 years ago

Thanks @nalexn for the post link!

I thought, If many views are referring to central app state and central app state value changes, then the views currently on the screen only for those views values are recomputed and other views will lazily recompute values when needed. For example,

class AppState : ObservableObject{
    @Published var userDeatils : User = false
    @Published var authToken : AuthToken = ""
}

struct SignIn: View{
    @EnviromentObject var appState: AppState 
    ....
    $appState.authToken="..... fetched from api"
    $appState.userDeatils="... fetched from api "
}

struct ProfileUpdate: View{
    @EnviromentObject var appState: AppState 
    ....
    if(appState.userDeatils!=nill && appState.authToken!="")
}

Let's say I am on Profile page then updating user details will only recompute ProfileUpdate view and sign in view only be recomputed when I am that view, is my understanding correct?

One more thing you used struct for AppState, I thought of AppState will be class implementing ObservableObject protocol.

About comments, repository is clear. Mainly about App State because it's got updated in viewmodel and got used in view. Having more comments in SceneDelegate, RootView, AppState, DIContainer will be useful.

For example, in SceneDelegate we can add comment like

// Environment struct will be used by all the views.
// It contains AppState which is central repository to share data across all the views. 
// |func scene| only called when app open for first time and this object will create only once.
let environment = AppEnvironment.bootstrap()

Comment can have following things Why it created? What was your thinking behind it. Explaining how it's fit in an architecture. (by keeping in mind some new user might not have background about uikit and javascript world) More information is always better :)

Again, Thanks a lot for your large effort. I learnt a lot from your post!

ramtest55 commented 4 years ago

As a new developer, I feel @EnviromentObject will help me a lot and I dont have many views. Can you please point me to commit where you used @EnviromentObject with AppState as class.

nalexn commented 4 years ago

@ramtest55 this should be the last commit prior migration to the @Environment: https://github.com/nalexn/clean-architecture-swiftui/commit/ad65ef5a66591c1db6169457c88b557f7848f734