I could really, really use some help with a concept that continues to hurt my head: Initializing.

Hi Everyone,

This is a long read, but it comes from my biggest frustration and lack of knowledge in SwiftUI, and how it relates to my own project. It’s the thing about injecting un-initialized objects into views. So something like, “AccountViewMain()”, will need to have a value if there’s a variable in AccountViewMain, that’s not initialized. From what I know, every object needs to be initialized. I think I’m right about that. I’ll add my code below.

For the most part, I can make sense of this. Where I KEEP running into trouble is how this relates to core data, and more specifically some help I received from a code mentor I connected with. The mentor was great, but expensive. Christmas is coming up so I’ve got to be careful with how much I spend.

My project has several views that reference my core data persistent store. My mentor used Combine to make it so whenever a user clicks the add button, a new object is already stored in the persistent store. So that when you go back to the list that presents those different objects (in my case accounts), they all show up, even with nothing in them. If I add text to that object, it shows up. But the problem is that whenever a user just clicks to a new object(account), the app automatically creates it. Not a desired effect.

And I know why this happens. It’s because the button I click on runs this code he made. It’s a view that does not ask for injection, and I think that’s because the view my mentor created runs an initializer. This is all cool, but not entirely helpful.

I have another view that looks just like the one my mentor made, but it doesn’t do any of the adding. So I want mine to appear when the user clicks to add a new object, and then use some of the code my mentor wrote to save the new entry whenever a user clicks the save button. This is the desired effect.

Here’s where I run into issues, even though my core data persistent store gets initialized, when I try to add my own view, it keeps saying, “Missing Parameter for accountBackEnd…” So this is assuming that the view called AccountViewMain, has a line like “@ObservedObject var accountBackEnd: AccountBackEnd. For reference, AccountBackEnd is the name of my core data entity. I’ve set everything else up, so it’s all good, but like I said, I run into issues here.

When I add the appropriate objects between the braces, it seems like it just keeps asking me for more and more. Like I'll fix one, and then the next one down the line asks, until I'm at my @main view. When I add it here, it says something about needing to be initialized. I'm pretty sure there's not supposed to be one there. Right?

I’ve tried adding “*** = AccountBackEnd()” to the end of that line, but naturally that doesn’t work. I think it’s something to do with the “= ***()” meaning that it’s recreating that view. Not referencing it. To add to this, whenever I try to look up how to initialize online, I really don’t get anywhere.

Is there someone who could please help me understand what it is that I should be doing here?

I’ll add what I think is pertinent code below. Please let me know if there is anything I’m missing here as far as code goes.

Thank you all kindly!

The view that displays my list:

   
     import SwiftUI
    import CoreData


    struct DatabaseViewMain: View {
    
    @ObservedObject var refactoringAccount = RefactoringAccount.refactoringAccountSingleton
    
    
    var body: some View {
    
        VStack {
            
            //Creates the 'add account' button
            HStack {
                Spacer()
                
                NavigationLink {
                    AddAccountContainer()
                } label: {
                    SubHeaderViewIcon(subheaderIcon: "plus.square", subheaderIconColor: Color("EditButtonBlue"))
                }
                .padding()
            }
            
            
            //Creates the List view
            List {
                ForEach(refactoringAccount.savedAccounts) {account in
                    NavigationLink {
                        AccountDetailMain(accountBackEnd: account)
                        
                    } label: {
                        
                        DatabaseViewCell(
                            accountNameFromCell: account.accountCompanyName,
                            accountLastVisitedFromCell: account.accountCity,
                            accountTotalFromCell: account.accountLastDateVisited)
                        
                        //Text(account.accountCompanyName)
                    }
                    .foregroundColor(.gray)
                }
                .onDelete(perform: refactoringAccount.deleteAccount)
                .onAppear(perform: refactoringAccount.fetchAccount)
                
            }
            .listStyle(PlainListStyle())
                        
        }
        .navigationBarHidden(true)
    }
    }

The view that my mentor created:


    import SwiftUI
    import Combine
    import CoreData

    struct AddAccountContainer: View {
    
    @State var account: AccountBackEnd?
    
    func newAccount() -> AccountBackEnd {
        let context = RefactoringAccount.refactoringAccountSingleton.container.viewContext
        let newAccount = AccountBackEnd(context: context)
        return newAccount
    }
    
    var body: some View {        
        VStack() {
            if let account = self.account {
                AddAccountView(accountBackEnd: account)
            }
        }
        .onAppear {
            if account == nil { account = newAccount() }
        }
    }
    }

The View that I’d like to use:

    import SwiftUI
    import CoreData


    //View for user to add a new account with
    struct AddAccountView: View {    
    
    @ObservedObject var accountBackEnd: AccountBackEnd
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        
        ScrollView {
            
            VStack {
                
                Group {
                    MainAddHeaderView(mainTextField: "Account", iconField: "building")
                    
                    Spacer()
                        .frame(height: 5)
                    
                    HStack {
                        NavigationLink {
                            CancelButton()
                        } label: {
                            MainHeaderViewBottomLeft()
                        }
                        Spacer()
                    }
                    
                }
                
                
                Button(action: {
                    do {
                        try accountBackEnd.managedObjectContext?.save()
                        
                    } catch {
                        print("There was a problem saving: \(error)")
                    }
                    presentationMode.wrappedValue.dismiss()
                    print("It exited")
                })
                {
                    LargeSaveButtonBlue(saveButtonLabel: "Save Changes")
                }
                
                
                OneHundredPointSpacer()
                
                //Exit
                LargeDeleteButton(deleteButtonLabel: "Delete Changes")
                OneHundredPointSpacer()
            }
            .navigationBarBackButtonHidden(true)
            .navigationBarHidden(true)
        }
    }
    }


Your question is very long (too long for free mentors on the forum may be 😉). To get help on this forum, you should ask shorter question, even if you have to split your question and ask step by step.

For instance, you say:

It’s the thing about injecting un-initialized objects into views. 

So, limit your post to this question, explain with shorter code what you tried and did not succeed for this specific point.

I could really, really use some help with a concept that continues to hurt my head: Initializing.
 
 
Q