本文介绍了删除核心数据中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个实体在我的核心数据模型像这样: @interface选择:NSManagedObject @property(nonatomic,retain)NSString * book_id; @property(nonatomic,retain)NSString * contenu; @property(nonatomic,retain)NSNumber * page_id; @property(nonatomic,retain)NSNumber * nbrOfOccurences; @property(nonatomic,retain)NSString * next; @property(nonatomic,retain)NSString * previous; 我创建了许多 Selection 他们在核心数据,现在我想删除一些选择与一些标准。例如,如果符合以下条件,我想删除 Selection 对象: content = test page_id = 5 book_id = 1331313 我可以做这个? 解决方案 Mike Weller 写的是正确的。 首先,您需要创建一个 NSFetchRequest ,如下所示: NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; [fetchRequest setEntity:[NSEntityDescription entityForName:@SelectioninManagedObjectContext:context]]; 然后您必须为该请求设置谓词,如下所示: [fetchRequest setPredicate:[NSPredicate predicateWithFormat:@content ==%@ AND page_id ==%@ AND book_id ==%@,contentVal,pageVal, bookVal]]; 其中 NSString * contentVal = @test; NSNumber * pageVal = [NSNumber numberWithInt:5]; NSString * bookVal = @1331313; 我使用%@ 现在,您可以使用上一个请求在上下文中执行提取操作: NSError * error = nil; NSArray * results = [context executeFetchRequest:fetchRequest error:& error]; results 包含所有匹配的托管对象 最后,您可以抓取对象并在其上调用删除。 [context deleteObject:currentObj]; 完成后,您需要按照文档保存上下文。 正如在保存上下文之前一个新对象不保存到商店中一样,在保存上下文之前不会从商店中删除已删除的对象。 因此 NSError * error = nil; [context save:& error]; 请注意, save 方法返回一个bool值。因此,您可以使用类似以下的方法或向用户显示警报。来源 NSManagedObjectContext保存错误。 NSError * error = nil; if([context save:& error] == NO){ NSAssert(NO,@Save should not fail \ n%@,[error localizedDescription]); abort(); } I have an entity in my core data model like this:@interface Selection : NSManagedObject@property (nonatomic, retain) NSString * book_id;@property (nonatomic, retain) NSString * contenu;@property (nonatomic, retain) NSNumber * page_id;@property (nonatomic, retain) NSNumber * nbrOfOccurences;@property (nonatomic, retain) NSString * next;@property (nonatomic, retain) NSString * previous;I have created many Selections and saved them in Core Data and now I would like to delete some selections with some criteria. For example, I would like to delete a Selection object if matches the following: content = testpage_id = 5book_id = 1331313How I can do this? 解决方案 What Mike Weller wrote is right. I'll expand the answer a little bit.First you need to create a NSFetchRequest like the following:NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];[fetchRequest setEntity:[NSEntityDescription entityForName:@"Selection" inManagedObjectContext:context]];Then you have to set the predicate for that request like the following:[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"content == %@ AND page_id == %@ AND book_id == %@", contentVal, pageVal, bookVal]];whereNSString* contentVal = @"test";NSNumber* pageVal = [NSNumber numberWithInt:5];NSString* bookVal = @"1331313";I'm using %@ since I'm supposing you are using objects and not scalar values.Now you perform a fetch in the context with the previous request:NSError* error = nil;NSArray* results = [context executeFetchRequest:fetchRequest error:&error];results contains all the managed objects that match that predicate.Finally you could grab the objects and call a deletion on them.[context deleteObject:currentObj];Once done you need to save the context as per the documentation. Just as a new object is not saved to the store until the context is saved, a deleted object is not removed from the store until the context is saved.HenceNSError* error = nil;[context save:&error];Note that save method returns a bool value. So you can use an approach like the following or display an alert to the user. Source NSManagedObjectContext save error.NSError *error = nil;if ([context save:&error] == NO) { NSAssert(NO, @"Save should not fail\n%@", [error localizedDescription]); abort();} 这篇关于删除核心数据中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-14 12:49