简体   繁体   English

删除核心数据中的对象

[英]Delete object in Core Data

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.然后它变成黄色,向后的方法应该是星黄色=删除。

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];

} 

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];
}

Don't forget to save the Context after you have deleted a NSManagedObject.不要忘记在删除 NSManagedObject 后保存上下文。 So here is the general code;所以这是通用代码;

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM