无法覆盖核心数据iOS/objective C

无法覆盖核心数据iOS/objective C,ios,Ios,因此,我试图覆盖/更新从核心数据保存的值。当按下后退按钮时,获取文本字段数据,然后使用该数据覆盖数据。但它只是不断地添加新数据。下面是我在后退按钮中的代码: IF语句只是检查索引是什么,以便知道返回哪个视图控制器。goBackMVC只是将其带回某个视图控制器 - (IBAction)btnBack:(UIBarButtonItem *)sender { if (self.viewControllerIndex == 3) { NSLog(@"test");

因此,我试图覆盖/更新从核心数据保存的值。当按下后退按钮时,获取文本字段数据,然后使用该数据覆盖数据。但它只是不断地添加新数据。下面是我在后退按钮中的代码: IF语句只是检查索引是什么,以便知道返回哪个视图控制器。goBackMVC只是将其带回某个视图控制器

- (IBAction)btnBack:(UIBarButtonItem *)sender {

   if (self.viewControllerIndex == 3) {
        NSLog(@"test");
        [self saveDataMethod];
        [self goBackMVC];
        [self.navigationController popViewControllerAnimated:YES];
}

saveDataMethod:

- (void) saveDataMethod {
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    FavouriteItem *favouriteItem = [NSEntityDescription insertNewObjectForEntityForName:@"FavouriteEntity" inManagedObjectContext:context];

    favouriteItem.webName = self.txtName.text;
    favouriteItem.webURL = self.txtURL.text;
    favouriteItem.imageURL = self.txtImageURL.text;
    NSLog(@"favouriteItem.webName %@", favouriteItem.webName);

    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);

    }
}
我的问题是如何覆盖数据而不是仅仅添加数据?谢谢


编辑:我四处搜索过,很多解决方案都有数组,但我不允许使用数组这是因为您在核心数据中插入了一个新实体:

FavouriteItem *favouriteItem = [NSEntityDescription insertNewObjectForEntityForName:@"FavouriteEntity" inManagedObjectContext:context];
取而代之的是获取所需的实体:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Favorits" inManagedObjectContext:context]];
若要获取所需实体,请创建NSPredicate实例,以便在您有多个实体并在请求中使用时筛选所需实体:

NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:<Your filter string>];
[fetchRequest setPredicate:filterPredicate];

NSError *error = nil;
NSArray* entities = [context executeFetchRequest:fetchRequest  error:&error];
if ([entities count] == 1) {
   // Get the entity and update necessary fields and save in context
}

谢谢你的帮助,快速提问。根据您的筛选字符串,我是否应该使用my FavoriteItem.webName等所有三个属性作为字符串?这取决于哪些属性将一个实体定义为唯一的。其中没有一个属性将一个定义为唯一的,可能有5个条目具有所有相同的值(不太可能),但可能会将==1替换为>0并更新每个实体