2012-06-14 13 views
26

Cómo puedo eliminar un objeto que había agregado antes con este código. Es una sección de favoritos, en el comienzo, agrego una estrella gris que agrega un objeto que viene de una búsqueda. Luego se pone amarillo y el método hacia atrás debe ser amarillo estrella = elimina.Eliminar objeto en Core Data

Pero no tengo ni idea de cómo hacerlo.

Gracias de antemano

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

} 

actualizar

yo hicimos este método .. Se hace el trabajo :)

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

} 
+0

usted tiene que salvar managedObject al fin de realizar cambios en coredata –

Respuesta

60

Es bastante sencillo :)

[context deleteObject:favorisObj]; 

Y el objeto malo se ha ido.

actualización

Acababas lo invierte con algo como esto si necesita un botón para eliminar el objeto.

-(IBAction)removeFavoris:(id)sender { 

    AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 

    NSManagedObjectContext *context = [appDelegate managedObjectContext]; 

    [context deleteObject:favorisObj]; 
} 
+0

Ok, pero ¿cómo me puede hacer un método que es lo contrario de lo anterior? – Tidane

+0

Tengo que declarar el favorisObj como arriba ?? Todavía no elimina nada :( – Tidane

+0

Por encima de ti estás creando el favorisObj y luego le agregas cosas. Debes tomar el mismo objeto y pasarlo como argumento a '[context deleteObject: arg]' –

24

No olvide guardar el contexto después de haber eliminado un NSManagedObject. Entonces aquí está el código general;

NSManagedObjectContext * context = [self managedObjectContext]; 
[context deleteObject:objectToDelete]; 

NSError * error = nil; 
if (![context save:&error]) 
{ 
    NSLog(@"Error ! %@", error); 
} 

En su caso, debe tener el fragmento después del bucle for.

for (NSManagedObject *managedObject in array) { 
    [moc2 deleteObject:managedObject]; 
} 
NSError * error = nil; 
if (![context save:&error]) 
{ 
    NSLog(@"Error ! %@", error); 
}