ios - 如何编辑核心数据值然后保存上下文以覆盖旧值?

标签 ios core-data nsdictionary

我正在尝试编辑我的一个 coredata 表/实体中的项目。我不是 100% 确定如何执行此操作,但我认为它是按照这些思路进行的。

首先创建上下文,然后使用谓词fetchrequest 实体从表/实体中选择确切的项目。然后将这些值保存到正确的 var 类型中更新值然后一些如何用新值覆盖现有项目。

最后一部分是我卡住的地方,这是我目前拥有的代码:

- (void)editSelectedInstall:(NSString *)invGuid {
    NSManagedObjectContext *context = [self managedObjectContext];


    if (context == nil) {
        NSLog(@"Nil");
    }
    else {


        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Install" inManagedObjectContext:context];
        [fetchRequest setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"invGUID==%@",invGuid];
        [fetchRequest setPredicate:predicate];

        NSError *error = nil;
        NSArray *myArray = [context executeFetchRequest:fetchRequest error:&error];
        NSMutableDictionary *myDictionary = [myArray objectAtIndex:0];
        NSLog(@"%@", myDictionary);




// Here I will update the values & then save the context? 
// For example I think I am to update the items like this:

myDictionary.num = @"1234";
myDictionary.name = @"new name";
    }


}

我想我快完成了,我只需要帮助保存上下文,以便它覆盖以前的值。

最佳答案

试试这个:

//loop through result set and update as required
for (Install *temp in myArray) {
    temp.num = @"1234";
    temp.name = @"New name";
}

//save
[context save:&error];

关于ios - 如何编辑核心数据值然后保存上下文以覆盖旧值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19856325/

相关文章:

ios - 检查 Swift 中是否存在 XML 元素

ios - 休息套件 : addSQLitePersistentStoreAtPath produce extra record

ios - swift&parse.com : converting String to AnyObject?

objective-c - UITableView 滞后于第一次显示包含 UITextView 的单元格

ios - 当父 UIViewController 调整容器 View 的大小时, subview Controller 不会自动布局

ios - 如何让 NSFetchedResultsController 返回更多部分?

ios - 避免在从 `UITableView` 进行后台更新时跳入 `NSFetchedResultsController`

ios - 我想将图像、名称、价格存储在一个数组中,如果我对价格值进行排序,则名称和图像必须正确排列

iphone - iPhone 词典有问题吗?

ios - NSDictionary writetofile无法正常工作