我正在尝试编辑其中一个coredata
表/实体中的项目。我不是百分百肯定如何做到这一点,但我认为它沿着这些方向。
首先创建上下文,然后使用谓词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 I need help saving the context?
// any help would be appreciated. For example I think I am to update the items like this:
myDictionary.num = @"1234";
myDictionary.name = @"new name";
}
}
我想我几乎就在那里,我只需要帮助保存上下文,以便覆盖以前的值。
答案 0 :(得分:1)
试试这个:
//loop through result set and update as required
for (Install *temp in myArray) {
temp.num = @"1234";
temp.name = @"New name";
}
//save
[context save:&error];