gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:00:42 25 4
gpt4 key购买 nike

我正在尝试编辑我的一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com