在我的代码中,我使用了简单的注册页面。这些字段包括名字、姓氏、电子邮件、密码和电话号码。在这种情况下,如果我注册了一个新的成员,意味着新给定的电子邮件id已经存储了,这意味着它不能存储在核心数据中。这就是我的情况。
这是我的代码,
//Appdelegate call
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];  
NSManagedObjectContext *context = [appDelegate managedObjectContext];
//Fetch Request
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Device"
                                              inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entityDesc];
NSLog(@"Your Request is : %@",request);
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(email = %@)", email];
[request setPredicate:pred];
NSError *error=nil;
NSArray *result = [context executeFetchRequest:request error:&error];
NSLog(@"Your Result is: %@",result);
if ([result count] > 0){
    if ([email isEqual:[[result valueForKey:@"email"]lastObject]])
    {
        [alertView AlreadyRegisterAlertView];
    }
    else{
        NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
        NSLog(@"Welcome for Register Section");
        [newDevice setValue:nameFile.fname forKey:@"fname"];
        [newDevice setValue:nameFile.surname forKey:@"sname"];
        [newDevice setValue:nameFile.eid forKey:@"email"];
        [newDevice setValue:nameFile.phone forKey:@"phone"];
        [newDevice setValue:nameFile.passd forKey:@"password"];
        [newDevice setValue:nameFile.imgProfile forKey:@"photo"];
        [alertView RegistrationSucessAlertView];
        NSLog(@"Your First Name is: %@",nameFile.fname);
        NSLog(@"Your Sur-Name is: %@",nameFile.surname);
        NSLog(@"Your Email id is: %@",nameFile.eid);
        NSLog(@"Your Phone Number is: %@",nameFile.phone);
        NSLog(@"Your Password is: %@",nameFile.passd);
        NSLog(@"Your Profile Image is: %@", nameFile.imgProfile);
    }
}
// Save the object to persistent store
if (![context save:&error]) {
    NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}在这段代码中,条件if([result count]>0)为false,这意味着我可以做什么。
发布于 2016-02-05 01:03:36
我看到的问题就在这条线上
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(email = %@)", email];
代码应该是
NSPredicate *pred = [NSPredicate predicateWithFormat:@"email == %@", email];
更新:
首先,我认为逻辑是不正确的。在实际保存之前,您正在尝试获取核心数据对象。您必须插入新实体并将其保存到核心数据。然后,如果需要,您必须获取该对象以进行任何进一步处理。
这就是为什么你的if条件每次都会失败。
https://stackoverflow.com/questions/35183555
复制相似问题