ipatch
ipatch

Reputation: 4043

Core Data - how to check if user exists in database?

So I'm trying to create a simple (if such one exists) login process for my app, and I am getting an error with the below block of code.

 NSManagedObject *context = _managedObjectContext;

NSFetchRequest *request= [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account"   inManagedObjectContext:context];
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"username==%@",self.textFieldUsername.text];
[request setEntity:entity];
[request setPredicate:predicate];

NSError *error = nil;

// Below line is giving me error

NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];
if (array != nil) {
    NSUInteger count = [array count]; // may be 0 if the object has been deleted.
    NSLog(@"Username may exist, %@",count);
}

else {
    NSLog(@"Username does not exist.");
}

`

The above code right now is ran when the user clicks the Login button.

Xcode is giving me an error of: No visible @interface for 'NSManagedObject' declares the selector 'executeFetchRequest:error.'

When I read the above statement it just seems greek to me. The name of the file I am working on is ViewControllerWelcome.m and can be viewed in it's entirety at the following link. If it matters, I am using the stock boilerplate core data code.

Bonus: How do I get objective-c code highlighting when I post on here (SO)?

Upvotes: 2

Views: 2712

Answers (1)

graver
graver

Reputation: 15213

Change:

NSManagedObject *context = _managedObjectContext;

To

NSManagedObjectContext *context = _managedObjectContext;

and

NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];

should be

NSArray *array = [context executeFetchRequest:request error:&error];

P.S. If you want the username search to be case insensitive use "username ==[c] %@" for the predicate...

Upvotes: 3

Related Questions