Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];  }  
like image 472
Tidane Avatar asked Jun 14 '12 18:06

Tidane


People also ask

What is delete rule in Core Data?

A delete rule defines what happens when the record that owns the relationship is deleted. Select the notes relationship of the Category entity and open the Data Model Inspector on the right. By default, the delete rule of a relationship is set to nullify. Core Data supports four delete rules: No Action.

What is Persistentcontainer in Core Data?

NSPersistentContainer simplifies the creation and management of the Core Data stack by handling the creation of the managed object model ( NSManagedObjectModel ), persistent store coordinator ( NSPersistentStoreCoordinator ), and the managed object context ( NSManagedObjectContext ).

What is NSManagedObjectContext in Core Data?

An object space to manipulate and track changes to managed objects.

What is Nsfetchrequest in Swift?

A description of search criteria used to retrieve data from a persistent store.


2 Answers

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]; } 
like image 88
Ryan Poolos Avatar answered Sep 20 '22 03:09

Ryan Poolos


Don't forget to save the Context after you have deleted a 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 (NSManagedObject *managedObject in array) {     [moc2 deleteObject:managedObject]; } NSError * error = nil; if (![context save:&error]) {     NSLog(@"Error ! %@", error); } 
like image 40
Ohmy Avatar answered Sep 19 '22 03:09

Ohmy