ios 删除核心数据中的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11039575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete object in Core Data
提问by Tidane
How I can delete an object which I had added before with this code. Its a favorites section, in the begin, I add a gray star which adds an object coming from a fetch. Then It turns yellow and the backwards method should be star yellow = deletes.
如何删除之前使用此代码添加的对象。它是一个收藏夹部分,在开始时,我添加了一个灰色的星星,它添加了一个来自 fetch 的对象。然后它变成黄色,向后方法应该是星黄色=删除。
But I have no idea how to do this.
但我不知道该怎么做。
-(IBAction)inFavoris:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *favorisObj = [NSEntityDescription
insertNewObjectForEntityForName:@"Favoris"
inManagedObjectContext:context];
[favorisObj setValue:idTaxi forKey:@"idTaxi"];
[favorisObj setValue:nomTaxi forKey:@"nomTaxi"];
[favorisObj setValue:taxiCB forKey:@"cb"];
[favorisObj setValue:taxiAvion forKey:@"avion"];
[favorisObj setValue:taxiColis forKey:@"colis"];
[favorisObj setValue:taxiHandicape forKey:@"handicape"];
[favorisObj setValue:taxiHoraires forKey:@"horaire"];
[favorisObj setValue:lugagge forKey:@"lugagge"];
[favorisObj setValue:luxury forKey:@"luxury"];
[favorisObj setValue:languesParlees forKey:@"langues"];
[favorisObj setValue:taxiNote forKey:@"note"];
[favorisObj setValue:taxiPassengers forKey:@"passenger"];
[favorisObj setValue:taxiVote forKey:@"etoiles"];
[favorisObj setValue:taxiTel forKey:@"tel"];
[self.view addSubview:favorisB];
}
UPDATE
更新
I made this method.. It gets the job done :)
我做了这个方法..它完成了工作:)
-(IBAction)outFavoris:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString *testEntityId = idTaxi;
NSManagedObjectContext *moc2 = [appDelegate managedObjectContext];
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
fetch.entity = [NSEntityDescription entityForName:@"Favoris" inManagedObjectContext:moc2];
fetch.predicate = [NSPredicate predicateWithFormat:@"idTaxi == %@", testEntityId];
NSArray *array = [moc2 executeFetchRequest:fetch error:nil];
for (NSManagedObject *managedObject in array) {
[moc2 deleteObject:managedObject];
}
[self.view addSubview:favorisO];
}
回答by Ryan Poolos
Its quite simple :)
它很简单:)
[context deleteObject:favorisObj];
And the bad object is all gone.
坏的对象都消失了。
Update
更新
You'd just reverse it with something like this if you need a button to delete the object.
如果你需要一个按钮来删除对象,你只需用这样的东西来反转它。
-(IBAction)removeFavoris:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
[context deleteObject:favorisObj];
}
回答by Ohmy
Don't forget to save the Context after you have deleted a NSManagedObject. So here is the general code;
不要忘记在删除 NSManagedObject 后保存上下文。所以这里是通用代码;
NSManagedObjectContext * context = [self managedObjectContext];
[context deleteObject:objectToDelete];
NSError * error = nil;
if (![context save:&error])
{
NSLog(@"Error ! %@", error);
}
In your case it should have the snippet after the for loop.
在您的情况下,它应该在 for 循环之后包含代码段。
for (NSManagedObject *managedObject in array) {
[moc2 deleteObject:managedObject];
}
NSError * error = nil;
if (![context save:&error])
{
NSLog(@"Error ! %@", error);
}