2

我看到了很多关于 Core Data 更新的问题。实际上我正在创建一个简单的应用程序联系人列表应用程序。它包含添加、编辑、删除和更新功能。这是我的更新代码。它可以工作并更新,但它会更新所有联系人列表。我只需要更新特定的联系人。

- (IBAction)updatePressed:(id)sender 
{
    delegate = [[AppDelegate alloc]init];
    delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    name2 = emailtxt1.text;
    email2 = nametext1.text;
    mobile2 = numbertxt1.text;
    dict = [[NSMutableDictionary alloc] init];
    [dict setObject:nametext1.text forKey:@"NAME"];
    [dict setObject:emailtxt1.text forKey:@"EMAIL"];
    [dict setObject:numbertxt1.text forKey:@"MOBILE"];
    [delegate UpdateDiary:dict];
}

- (void)UpdateDiary:(NSMutableDictionary *)dictionary
{
    NSLog(@"update book Details Function Entered");

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Diary"inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSArray *mutableFetchResult = [[[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy] autorelease];

    if (mutableFetchResult == nil) {
        NSLog(@"Fetch result error %@, %@", error, [error userInfo]);
    }

    for (Diary *ob2 in mutableFetchResult) 
    {
        {
        ob2.name = [dictionary objectForKey:@"NAME"];
        ob2.email=[dictionary objectForKey:@"EMAIL"];
        ob2.phone=[dictionary objectForKey:@"MOBILE"];
        }
    }

    if(![self.managedObjectContext save:&error])
    {               
        if([error localizedDescription] != nil)
        {           
        NSLog(@"%@",error);
        }
        else
        {

        }
    }
}
4

2 回答 2

1

您需要在获取请求上设置谓词。这就是它知道您想要哪个对象的方式,而不仅仅是获取所有对象。

您可以执行以下操作:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"email == %@", anEmailAddress];

如果您这样做了,那么执行获取请求的结果将只是与您在谓词中设置的电子邮件地址匹配的对象。

当然,请注意,如果有多个对象具有相同的电子邮件地址,则获取请求将获取所有这些对象。

一个更好的应用程序设计可能是,当你进入编辑表单时,保留你正在编辑的 Core Data 对象,可能在你的视图控制器的一个属性中。(我认为你会在那个时候拥有它,因为你需要知道用什么来填充字段。)这样你就不需要在用户尝试提交编辑时执行提取——你可以只使用你随身携带的物品。

于 2012-08-09T14:31:17.613 回答
0
- (void)UpdateBook:(NSMutableDictionary *)dictionary
    {


     NSLog(@"update book Details Function Entered");



     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];


     NSEntityDescription *entity = [NSEntityDescription entityForName:@"Book"inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"bookID = %@", [dictionary objectForKey:@"vID"]];

     NSArray *mutableFetchResult = [[[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy] autorelease];

     if (mutableFetchResult == nil) {
        NSLog(@"Fetch result error %@, %@", error, [error userInfo]);
    }

    for (Book *ob2 in mutableFetchResult) 
    {

        {

            ob2.name = [dictionary objectForKey:@"VName1"];
            ob2.author=[dictionary objectForKey:@"VAuthor1"];
            ob2.discription=[dictionary objectForKey:@"VDiscription1"];
            ob2.bookID=[dictionary objectForKey:@"vID"];

        }
    }

    if(![self.managedObjectContext save:&error])
    {               
        if([error localizedDescription] != nil)
        {           
            NSLog(@"%@",error);
        }
        else
        {

        }
    }
}
于 2012-09-22T10:54:29.177 回答