Ios 我们如何更新使用核心数据存储的属性值?
我在应用程序中使用了核心数据来保存用户数据。单个用户有“用户名”、“密码”、“确认密码”、“手机号码”Ios 我们如何更新使用核心数据存储的属性值?,ios,iphone,objective-c,core-data,Ios,Iphone,Objective C,Core Data,我在应用程序中使用了核心数据来保存用户数据。单个用户有“用户名”、“密码”、“确认密码”、“手机号码” 我可以保存注册用户信息,也可以验证用户名和密码,并登录到应用程序。现在我想改变我的“密码”,如果用户输入“用户名”,“手机号码”正确。如何执行此操作?这与保存注册用户信息相同 // Set password and save [user setPassword:newPassword]; [user.managedObjectContext save:nil]; 从核心数据检索对象后,使用谓
我可以保存注册用户信息,也可以验证用户名和密码,并登录到应用程序。现在我想改变我的“密码”,如果用户输入“用户名”,“手机号码”正确。如何执行此操作?这与保存注册用户信息相同
// Set password and save
[user setPassword:newPassword];
[user.managedObjectContext save:nil];
从核心数据检索对象后,使用谓词更新对象,然后保存它 例:
将用户信息更新到数据库中
NSEntityDescription *productEntity=[NSEntityDescription entityForName:@"Registration" inManagedObjectContext:context];
NSFetchRequest *fetch=[[NSFetchRequest alloc] init];
[fetch setEntity:productEntity];
NSPredicate *p=[NSPredicate predicateWithFormat:@"username == %@ AND mail_no == %@",user-username,mobile_no];
[fetch setPredicate:p];
//... add sorts if you want them
NSError *fetchError;
NSArray *fetchedProducts=[context executeFetchRequest:fetch error:&fetchError];
NSLog(@" update fetch data %@", fetchedProducts);
for (NSManagedObject *record in fetchedProducts) {
[record setValue:_mailTextField.text forKey:@"mailID"];
}
这是我用来更改密码的代码。我使用“用户名”和“旧密码”更改密码。很好用
NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username like %@ and password like %@",username,self.oldPass.text];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
if(matchingData.count<=0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error!" message:@"Old Password Does not Match" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
else
{
for(NSManagedObject *obj in matchingData)
{
[obj setValue:changedPass.text forKey:@"password"];
[obj setValue:confirmChangedPass.text forKey:@"confirmpassword"];
}
[context save:&error];
alert1 = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Password Changed Successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert1 show];
}
}
NSEntityDescription*entitydesc=[NSEntityDescription entityForName:@“User”inManagedObjectContext:context];
NSFetchRequest*request=[[NSFetchRequest alloc]init];
[请求集合实体:entitydesc];
NSPredicate*谓词=[NSPredicate谓词格式:@“类似用户名%@和密码%@”,用户名,self.oldPass.text];
[请求集谓词:谓词];
n错误*错误;
NSArray*matchingData=[context executeFetchRequest:请求错误:&error];
如果(matchingData.count)我知道完整的解决方案,请告诉我您使用了哪个字段作为主键为什么取消选中我的答案。为什么删除接受的答案对不起,谢谢您的答案。另外,我将我的代码放在我所做的事情上。感谢您的贡献。这个声誉是为了我的效果
NSEntityDescription *entitydesc = [NSEntityDescription entityForName:@"User" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc]init];
[request setEntity:entitydesc];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username like %@ and password like %@",username,self.oldPass.text];
[request setPredicate:predicate];
NSError *error;
NSArray *matchingData = [context executeFetchRequest:request error:&error];
if(matchingData.count<=0)
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error!" message:@"Old Password Does not Match" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
else
{
for(NSManagedObject *obj in matchingData)
{
[obj setValue:changedPass.text forKey:@"password"];
[obj setValue:confirmChangedPass.text forKey:@"confirmpassword"];
}
[context save:&error];
alert1 = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Password Changed Successfully" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert1 show];
}
}