Hi Everyone,
I'm trying to get my SwiftUI project to work with Core Data, and to be refactored. I'm using property wrappers for its fetch request.
I keep getting this error in the log: "Accessing StateObject's object without being installed on a View. This will create a new instance each time."
As far as I know, it's something to do with having multiple @StateObject requests. Is this correct?
What am I doing wrong here?
I'll try and add my relevant code below. Any help here is greatly appreciated. Alternatively I can send you a link to my GH .
my refactoring\modelview file
import SwiftUI
class RefactorAccounts: ObservableObject {
//Fetch request for pulling from persistent store.
@Environment(\.managedObjectContext) var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \AccountBackEnd.accountCompanyName, ascending: true)],
animation: .default)
var accounts: FetchedResults<AccountBackEnd>
//Takes object from @StateObject, presents as var
@StateObject var accountBackEnd = AccountBackEnd()
//Calls to Class for published objects
@StateObject var publishedClasses =
PublishedClassAccount()
//Used for button/nav calls out
@Environment(\.presentationMode) var presentationMode
//Add account Function
func addAccount() {
withAnimation {
//Adds to persistence store
let newAccount = AccountBackEnd(context: viewContext)
newAccount.accountCompanyName = publishedClasses.accountNameForCoreData
PersistenceController.shared.save
{
error in
if let error = error {
print(error.localizedDescription)
return
}
print("Successfully saved account.")
}
self.presentationMode.wrappedValue.dismiss()
publishedClasses.accountNameForCoreData = ""
}
}
}
The file that displays clickable data
//Refactoring
@StateObject var refAccouunts = RefactorAccounts()
//Used for button/nav calls out
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
HStack {
Spacer()
NavigationLink {
AddAccountView()
} label: {
SubHeaderViewIcon(subheaderIcon: "plus.square", subheaderIconColor: Color("EditButtonBlue"))
}
.padding()
}
List {
ForEach(refAccouunts.accounts, id: \.id) {account in
NavigationLink {
AccountDetailMain()
} label: {
Text(account.accountCompanyName ?? "")
}
.foregroundColor(.red)
}
.onDelete(perform: deleteAccounts)
}
.listStyle(PlainListStyle())
}
.navigationBarHidden(true)
}
View that is used to add data, from sub views.
@ObservedObject var refAccouunts = RefactorAccounts()
//Used for button/nav call out
@Environment(\.presentationMode) var presentationMode
var body: some View {
ScrollView {
VStack {
Group {
MainAddHeaderView(mainTextField: "Account", iconField: "building")
}
Group{
AddAccountCompanyName()
ViewSpacer()
AddAccountAddress()
ViewSpacer()
AddAccountDetails()
ViewSpacer()
Button(action: refAccouunts.addAccount) {
LargeSaveButtonBlue(saveButtonLabel: "Save Changes")
}
}
ViewSpacer()
LargeDeleteButton(deleteButtonLabel: "Cancel Changes")
ViewSpacer()
}
}
.environmentObject(refAccouunts.publishedClasses)
.navigationBarHidden(true)
}
Specific sub-view
//Refactoring
@ObservedObject var refAccouunts = RefactorAccounts()
//Used for button/nav call out
@Environment(\.presentationMode) var presentationMode
var body: some View {
VStack {
HStack {
SubHeaderViewText(subheaderTitle: "Account")
NavigationLink {
AddContactView()
} label: {
SubHeaderViewIcon(subheaderIcon: "plus.square", subheaderIconColor: Color("EditButtonBlue"))
}
}
Spacer()
.frame(height: 20)
Group {
VStack (alignment: .leading) {
Text("Account Name")
.frame(alignment: .leading)
.modifier(AddRangerHeaderStyle())
.font(.custom("SF Pro Display Light", fixedSize: 20))
.foregroundColor(Color("DarkerGrey"))
TextField("Account Name", text: refAccouunts.$publishedClasses.accountNameForCoreData)
.textFieldStyle(AddRangerTextFieldStyle())
HStack {
RangerTextField(labelText: "First Name", placeHolder: "")
RangerTextField(labelText: "Last Name", placeHolder: "")
}
RangerTextField(labelText: "Position", placeHolder: "")
}
}
}