使用核心数据的配置文件屏幕

Profile screen by using core data

嘿,首先让大家知道我是 Core-Data 的新手,所以请和我一起写文章, 好的,我有一个非常基本的登录系统,我有一个名为 "Account" 的实体和两个名为:[ 的属性=47=]"email","password".

让我们从问题开始,从这里开始:

现在我可以 upload/insert 将数据存入 sqlite 数据库,并在用户尝试登录时使用 "NSpredicate" 授权用户和密码。 该应用程序有 3 windows Home,Login,SignUp;我现在的问题是: 检索特定用户(已登录)"email" 以显示到 "UIlabel" 主页 屏幕上。

我为此创建了一个数组来存储结果,看看这里(这是在 "Home" 屏幕):

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

for (AccountBase *account in results) {

        _label.text = [NSString stringWithFormat:@"%@", account.email];

    }

问题:

我只得到最后一个 Signup "email" 的用户,例如,现在新用户注册并且是电子邮件是 "mail.com" 那么,他会看到一个名为 "gmail.com" 的旧邮件。

我的注册码:

-(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];

}

问题:

没问题,据我所知,信息已经很好地发送到数据库了。

我的登录密码:

    - (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");
        }

    }


}

问题:

如果用户输入正确的信息,他将被移至主屏幕,则没有问题代码正在运行。

但是,这是一个很大的问题,问题是我需要一个唯一的标识符来处理内部核心数据,而且我知道它不像 mysql 具有主键的数据库。我也听说过 Object-ID 但我不知道如何使用它。 找到零个有助于 google 的信息和零个示例。

主要问题:

我需要的是用户登录后获取的是email的object id然后在Home界面获取这个 id 返回然后打印是通过电子邮件发送到 "UIlabel".

现在我不知道核心数据是如何与唯一标识符一起工作的,所以我需要一种方法来完成这个任务,并将我的 sqlite 数据库中的所有 "email's" 区分给正确的用户。

您可以将 属性 添加到您的 AppDelegate class 以存储登录用户的帐户对象 -

AppDelegate.h

@property (strong,nonatomic) Account *currentUser;

然后你可以在用户认证后设置这个-

- (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"); 
           AppDelegate *app=(AppDelegate *)[UIApplication sharedApplication].delegate;
           app.currentUser=anAccount;
 [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");
        }

    }

}

然后,您可以在任何需要访问当前用户的地方使用

*AppDelegate *app=(AppDelegate *)[UIApplication sharedApplication].delegate;
self.sometextfield.text=app.currentUser.email;

你有一些你没有意识到的问题。首先,您不应该将密码存储在核心数据中,因为它不安全。此外,当您在获取请求上设置谓词时,它会替换任何现有谓词,因此您的登录检查只会查看密码,而不是电子邮件地址(因为该谓词已被替换)。

对于您提出的第一个问题,您看不到最后一个注册的用户,除非您在提取请求中有日期或计数和排序描述符。您实际上看到了一个随机用户。您真正应该做的是使用 table 视图显示所有用户电子邮件地址(或不显示任何)用于登录。

如果获取登录 return 是什么东西,那么您不需要检查它,因为您已经断定它是正确的。

登录后,您应该return用户或将其存储在某处。另一个答案在 app delegate 中说,这会起作用,但是是不好的做法。您可能想要创建一个单例用户控制器来保存登录用户(并控制对它的访问)。

如果您需要在应用中为每个用户创建一个唯一的 I'd,那么您应该创建一个 UUID 并将其添加到实体的新属性中。