Reputation: 2571
A core database keeps track of user info. The goal is to login using the core data, simple enough. Simulating the app and logging in works perfectly. Xcode does not show any errors or warnings.
Console output shows: Failed to call designated initializer on NSManagedObject class 'Login' Can I ignore this output ??
Login.h and Login.m are created by Xcode itself from the data model.
Login.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class User;
@interface Login : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * password;
@property (nonatomic, retain) User *user;
+ (User *)loginWithEmail:(NSString *)email withPassword:(NSString *)password inManagedObjectContext:(NSManagedObjectContext *)context;
@end
Login.m
#import "Login.h"
#import "User.h"
@interface Login ()
- (User *)isValidEmail:(NSString *)email inManagedObjectContext:(NSManagedObjectContext *)context;
@end
@implementation Login
@dynamic password;
@dynamic user;
- (User *)isValidEmail:(NSString *)email inManagedObjectContext:(NSManagedObjectContext *)context
{
User *user = nil;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
request.predicate = [NSPredicate predicateWithFormat:@"email = %@", email];
NSError *error = nil;
user = [[context executeFetchRequest:request error:&error] lastObject];
[request release];
return user;
}
+ (User *)loginWithEmail:(NSString *)email withPassword:(NSString *)password inManagedObjectContext:(NSManagedObjectContext *)context
{
Login *loginHelper = [[Login alloc] init];
User *user = nil;
if ((user = [loginHelper isValidEmail:email inManagedObjectContext:context])) {
if ([user.login.password isEqualToString:password]) {
// correct login
} else {
// invalid password
user = nil;
}
} else {
// user does not exist
user = nil;
}
[loginHelper release];
return user;
}
@end
Upvotes: 0
Views: 1158
Reputation: 4897
My understanding is that you typically don't alloc/init NSManagedObjects or subclasses of NSManagedObjects explicitly- CoreData handles instantiating and deallocating managed objects as necessary - but you are attempting to alloc/init your own subclass in your loginWithEmail method. So that's probably why you're getting the error.
In the broader sense, this implementation seems to blur the lines between what should be a cut-and-dry data model (your NSManagedObject subclass), and the application logic of "logging in" - so I'd recommend reconsidering your architecture just a bit to more firmly reflect model-view-controller principles! Happy coding.
Upvotes: 1