I have a Entity named "Users" with 2 attributes named "username" and "password".
I have another Entity named "Info" with 4 attributes named "avatar", "level", "loss", "win".
I have a relationship setup with the "User" entity named "userInfo" Destination: "Info" Inverse: "userDetails"
I have a relationship setup with the "Info" entity named "userDetails" Destination: "Users" Inverse: "userInfo"
Here is my code right now:
@IBAction func loginButtonPressed(_ sender: UIButton) {
// getting the inputs from the user
let username = usernameField.text!
let userPassword = passwordField.text!
// creating a request to fetch the results for the users
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Users")
request.returnsObjectsAsFaults = false
// now getting all the results
do
{
// saving results in results
let results = try context.fetch(request)
// now checking if there is any results
if results.count > 0
{
// creating a for loop to go through the results
for result in results as! [NSManagedObject]
{
// checking if username and password match
if username == (result.value(forKey: "username") as? String)! && userPassword == (result.value(forKey: "password") as? String)!
{
// if they do set "isLoggedIn" to true
UserDefaults.standard.set(true, forKey:"isLoggedIn")
UserDefaults.standard.synchronize()
// Now saving the username and info of the user to the main viewController to display the users data later
// I NEED TO SAVE FROM THE RELATIONSHIP DATA HERE
// dismissing this view to go to the main view
self.dismiss(animated: true, completion: nil)
}
else
{
// if username and passwords dont match then display it to the user.
alertMessage(userMessage: "Login failed username or password do not match!")
}
}
}
else
{
// if no results then display error message
alertMessage(userMessage: "Login Failed: No users registered!")
}
}
catch
{
// catch
}
}
after checking the if the username and password are correct I need to store the data from the relationship of that username and password to 4 variables. Named: win, loss, level, and avatar.
I am confused on how to get the correct data from the relationship
I've tried using the "result" variable to try to get this but no luck.
Thanks