1
struct GroceryList: View {
  @ObservedObject var coreDataViewModel:CoreDataViewModel = CoreDataViewModel() 
  @StateObject var userPreferences: UserPreferences = UserPreferences()
  @State private var selectedItemsInList: [GroceryItem] = []
  @State private var selection = Set<UUID>()//for check box
  @State private var activeTab:Int = 0 //for active tab

The above View gets initialized 3 times as and when the @State variables selection and activeTab gets initialized. I have a init() in GroceryList: View like given below:

 init() 
{
   print("Grocery List View")
}

Grocery List View gets printed thrice.

Since I instantiate and initialize a CoreData View Model firstly, its fetchItems() in CoreDataViewModel init is also done thrice.

How to avoid initialization happening multiple times?

This is my first SwiftUI app in the development phase with self-learning.Any pointers greatly appreciated.

1
  • 1
    We do not control how many times SwiftUI can create view, it is a struct (ie. value), so it can be many times... just don't have view's init heavy, preferably do nothing there except initializing properties.. Commented Aug 29, 2021 at 9:03

1 Answer 1

2

CoreDataViewModel initializes 3 times most certainly because you have @ObservedObject, where you have to have @StateObject. Or you can inject this view model as a parameter, leaving @ObservedObject. Or you can use @EnvironmentObject in conjunction with .environmentObject() view modifier.

Also, if by "view initializes" 3 times, you mean its onAppear gets called multiple times - this means your view does not have stable identity. Check Demystifying SwiftUI talk to learn more, but briefly, you need to ensure that view has a stable structural identity, and/or explicit .id() identity.

Sign up to request clarification or add additional context in comments.

4 Comments

How to give a View stable identity? Hope the developer.apple.com/videos/play/wwdc2021/10022 helps. Any inputs also highly valuable.
This talk is probably the most useful one about SwiftUI. Clears a lot of things, especially about animation and poor performance.
Making CoreDataViewModel as @EnvironmentObject did it. Now VM does not get fetched 3 times. But view still inits thrice.
View's init effectively means it or its part rendering. For that to investigate you would need to carefully check what state changes. You can also employ a new tool let _ = Self._printChanges() available in iOS 15+.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.