Hey first of just to let you all know i'm new to Core-Data so be essay with me, Ok I have a very basic login system which I have an entity named "Account" and 2 attribute named: "email", "password".
Let's start from the problem and move on from there:
Now I was able to upload/insert data into sqlite database and also use "NSpredicate" to authorize users and passwords when user try to login in. The app have 3 windows Home,Login,SignUp; My problem now is: to retrieve the specific user (that is logged in) "email" to be displayed into "UIlabel" on the Home screen.
What I did for that is created an array to store the results just look here (This is on the "Home" screen):
NSArray *results = [context executeFetchRequest:request error:&error];
for (AccountBase *account in results) {
_label.text = [NSString stringWithFormat:@"%@", account.email];
}
The problem:
I only get the last user who Signup "email" and if for instance, now new user signup and is email is "mail.com" then, he would see an older mail named "gmail.com" for example.
My Code for signup:
-(IBAction)signup:(id)sender{
NSManagedObjectContext *context = [self managedObjectContext];
if (self.contactdb) {
// Update existing device
// [self.contactdb setValue:self.fullname.text forKey:@"fullname"];
//[self.contactdb setValue:self.email.text forKey:@"email"];
//[self.contactdb setValue:self.phone.text forKey:@"phone"];
} else {
// Create a new device
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:context];
[newDevice setValue:self.email.text forKey:@"email"];
[newDevice setValue:self.password.text forKey:@"password"];
NSLog(@"%@",newDevice);
//[newDevice setValue:self.phone.text forKey:@"phone"];
}
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
[self dismissViewControllerAnimated:YES completion:nil];
}
The problem:
No problem, as far as I know, The info is been send very good to the database.
My code for login:
- (IBAction)processLogin:(id)sender {
NSManagedObjectContext *context = [self managedObjectContext];
// CORE DATA
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:context];
// set entity for request
[request setEntity:entity];
// filter results using a predicate
NSPredicate *pred =[NSPredicate predicateWithFormat:(@"email = %@"), _emailfld.text];
NSPredicate *pred2 =[NSPredicate predicateWithFormat:(@"password = %@"), _passwordfld.text];
// set predicate for the request
[request setPredicate:pred];
[request setPredicate:pred2];
NSError *error = nil;
// store DB usernames in results array
NSArray *results = [context executeFetchRequest:request error:&error];
NSLog(@"The returned results are %@",results);
// check text field against results stored in DB
for (Account *anAccount in results) {
if ([anAccount.email isEqualToString:_emailfld.text] || [anAccount.password isEqualToString:_passwordfld.text]){
// NSLog(@"Your username exists");
NSLog(@"Your pin is correct");
[self performSegueWithIdentifier:@"showhome" sender:sender];
}
else if (![anAccount.email isEqualToString:_emailfld.text] || [anAccount.password isEqualToString:_passwordfld.text]){
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"Your username is bad");
}
else{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(@"Username dosent exitst");
}
}
}
The problem:
No problem code is working if user enter the right info he is been moved to the home screen.
But and its a big But, the problem is that I need a unique identifier to be working with inside core data and I know its not like mysql database with the primary key. Also I heard about Object-ID but I have no idea on how to use it. Found zero info that would help on google and zero examples.
The main Problem:
What I need is after the user is login to get is object id of is email and then in the Home screen to get this id back and then print is email into a "UIlabel".
Now I don't now how exactly core data work with unique identifier's so I need a method to accomplish this task and to make the different between all "email's" in my sqlite database to the right user.
You have some problems you aren't aware of. Primarily, you shouldn't be storing passwords in core data because it isn't secure. Also, when you set a predicate on a fetch request it replaces any existing predicate so your login check only looks at the password, not the email address (because that predicate is replaced).
For your first stated problem, you don't see the last user to sign up unless you have a date or a count and a sort descriptor on the fetch request. You actually see a random user. What you should really be doing is using a table view to display all user email addresses (or not showing any) for the login.
If the fetch for login returns something, you don't then need to check it because you have already predicated that it's correct.
After login, you should return the user or store it somewhere. The other answer says in the app delegate, which will work, but is bad practice. You might want to create a singleton user controller which holds the logged in user (and controls access to it).
If you need a unique I'd in the app for each user then you should create a UUID and add it to a new attribute on your entity.