-2

I am working on an app using coreData and a log in. For that, I have a LoginView and LoginViewModel that I use to get the info to the ContentView. I keep getting this error message: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[abcText2Speech.UserEntity firstname]: unrecognized selector sent to instance 0x6000017f14c0'"

Here is my code where I think the issue lies :

this is from LoginViewModel:

func signup()-\> (UserEntity, Int){ @StateObject var dataController = DataController.shared buttonState = .loading(title: "Loading", systemImage: "person") DispatchQueue.main.asyncAfter(deadline: .now()+1.5){ if self.isValidEmail() && self.isValidPassword() && self.isValidName(){ self.isLoggedIn = true dataController.addUser(firstname: self.fname, lastname: self.lname, email: self.email, password: self.password) print("signed in") print(dataController.savedEntities[(dataController.savedEntities.count)-1].email ?? "email") print(self.isLoggedIn) self.buttonState = .enabled(title: "Login", systemImage: "checkmark.circle") } else{ self.buttonState = .disabled(title: "Fill out all fields to log in", systemImage: "exclamationmark.circle") } } print("Test") print(dataController.savedEntities[(dataController.savedEntities.count)-1].email ?? "email") return (dataController.savedEntities[(dataController.savedEntities.count)-1], 1) } 

and this all prints with no issue.

this is the relevant part of sign up view

ActionButton(state: $model.buttonState, onTap:{ signup = model.signup() user = signup.0 }, backgroundColor: .primary , foregroundColor: Color(UIColor.systemBackground) ) NavigationLink (destination: LoginView()){ Text("Already have a profile? Click here to Log in") } } } label: { Label("Welcome!", systemImage: "hand.wave") } .padding() .textFieldStyle(.plain) let keyboard = abcTextViewModel() NavigationLink (destination: ContentView(viewModel: keyboard, currentUser: user) , isActive: $model.isLoggedIn){ EmptyView() }` 

and this is the part of Content view that breaks it

Text(currentUser.firstname ?? "name") 

these are the details of UserEntity

and this is the DataController code

import Foundation import CoreData class DataController: ObservableObject{ static let shared = DataController() let container: NSPersistentContainer @Published var savedEntities: [UserEntity] = [] init(){ container = NSPersistentContainer(name: "Model") container.loadPersistentStores { description, error in if let error = error { print("Core Data failed to load: \(error.localizedDescription)") } } fetchUsers() } func fetchUsers() { let request = NSFetchRequest<UserEntity>(entityName: "UserEntity") do { savedEntities = try container.viewContext.fetch(request) } catch let error { print("Error while fetching: \(error)") } } func addUser(firstname: String, lastname: String, email: String, password: String) { let newUser = UserEntity(context: container.viewContext) newUser.firstname = firstname newUser.lastname = lastname newUser.email = email newUser.id = password saveData() } func saveData(){ do { try container.viewContext.save() fetchUsers() } catch let error { print("Error saving: \(error)") } } } 

any help would be greatly appreciated, I have been stuck on this one error for quite a while

This is where the code sends the error (on the struct abcText2SpeechApp line):

import SwiftUI @main struct abcText2SpeechApp: App { var body: some Scene { WindowGroup{ SignUpView() .environment(\.managedObjectContext, DataController.shared.container.viewContext) } } } 

I tried printing the user details in login view model which worked, however that did not carry over to content view.

4
  • Please spend some time formatting your code correctly.
    – matt
    CommentedJul 1, 2024 at 19:08
  • Please pinpoint the actual line of code in which the crash occurs.
    – matt
    CommentedJul 1, 2024 at 19:13
  • What is currentUser and how is it set?CommentedJul 2, 2024 at 6:24
  • current user is set to be the output of the signup() function as shown in the codeCommentedJul 2, 2024 at 16:15

1 Answer 1

0

That error message means you are trying to call a method on an object that has been deallocated. You're using a function, method, or accessor on a variable, but that variable doesn't have a valid object anymore.

If the line is

Text(currentUser.firstname ?? "name") 

Then it means that currentUser does not have a valid object. Maybe it did once, but it doesn't when the crash happens.

Why this happens is not clear from the code you posted. There is no currentUser variable anywhere in the code except on that one line, so it's impossible to say where that variable is defined, where it gets its value, or why the object might be deallocated.

3
  • Hi, thank you for the help! Current user is defined in the 1st and 2nd code block — user is the result of the signup function and then ContentView is called with currentUser passed in as a parameter. It is accessed by contentView as an observable object like this: @ObservedObject var currentUser: UserEntityCommentedJul 2, 2024 at 21:26
  • Yes, and it’s getting deallocated somewhere. This isn’t a Core Data problem, it’s down to basic memory management trouble.CommentedJul 3, 2024 at 0:33
  • 1
    The signup function makes an asynchronous call so what is returned is not the newly created user but something else and perhaps later this returns a different user from savedEntities when the asynchronous call is complete?CommentedJul 3, 2024 at 6:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.