【问题标题】:How do we update attribute value which is stored using Core Data? [duplicate]我们如何更新使用 Core Data 存储的属性值? [复制]
【发布时间】:2014-01-24 19:46:07
【问题描述】:

我在我的应用程序中使用了 Core Data 来保存用户的数据。单个用户拥有“用户名”、“密码”、“确认密码”、“手机号”。

我可以保存注册用户信息,还可以验证用户名和密码并登录到应用程序。现在,如果用户正确输入“用户名”、“手机号码”,我想更改我的“密码”。我该怎么做?

【问题讨论】:

  • 我知道完整的解决方案告诉你使用哪个字段作为主键
  • 为什么取消我的答案。

标签: ios iphone objective-c core-data


【解决方案1】:

也和保存注册用户信息一样,

// Set password and save
[user setPassword:newPassword];
[user.managedObjectContext save:nil];

【讨论】:

    【解决方案2】:

    从核心数据中检索对象后,您使用谓词更新对象然后保存它。

    例如:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Username = 'YourUserName' AND MobileNumber = 'TheMobileNumber'"];
    NSFetchRequest *request = [NSFetchRequest new];
    //setup request here
    [request setPredicate: predicate];
    
    NSArray *result = [ManagedObjectContext executeFetchRequest:request error:&errors]
    YourObject *object = (YourObject *)[result lastObject]; // assuming you only have one entry get the last object in the array or the only object.
    object.password = "your desired password";
    [ManagedObjectContext save:&error];
    

    【讨论】:

      【解决方案3】:

      将用户信息更新到数据库中

       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"];
      
              }
      

      【讨论】:

        【解决方案4】:

        这是我写的更改密码的代码。我使用“用户名”和“旧密码”更改我的密码。它工作正常。

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

        【讨论】:

        • 为什么删除已接受的答案
        • 抱歉,感谢您的回答。我也把我的代码放在我做了什么..
        • 感谢您的贡献,这个声誉是为了我的影响
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-16
        • 1970-01-01
        相关资源
        最近更新 更多