28

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

} 
1
  • you have to save managedObject at last to make changes into coredata Commented Oct 11, 2017 at 11:04

2 Answers 2

63

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];
}
Sign up to request clarification or add additional context in comments.

7 Comments

Ok, but how I can make a method which is the opposite from the above ?
I have to declare the favorisObj like above?? It still doesn't delete anything :(
Above you are creating the favorisObj then adding stuff to it. You need to take the same object and pass it in as the argument to [context deleteObject:arg]
yea, you have to have an object to pass in. you can't just type the name. you need a pointer to it.
Yes. Objects are very tied to their context. If you need to access it from another context in any way, save the object with its original context, then fetch it from the new context before performing those changes.
|
26

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.