Delete Core data object and tablecell - ios

i'm trying to delete an object in core data when you press swipe and press delete. The problem is it deletes the the tablecell, but when i go back and in again the deleted cells are back again. I guess thats because i only deleted the object in the NSMUtableArray (devices) and did not delete the core data object. How can i do this?
The saveTap where the objects are saved:
-(void)saveTap:(id)sender{
Entity *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:#"Entity" inManagedObjectContext:_managedObjectContext];
[newManagedObject setValue:self.textfield.text forKey:#"playlistName"];
[newManagedObject setValue:[NSNumber numberWithInt:selectedRowValue] forKey:#"idnumber"];
// Save the context.
NSError *error = nil;
if (![_managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[self fetchDevices];
NSLog(#"COUNT %d", [devices count]);
}
and the commiteditingstyle method
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.devices removeObjectAtIndex:indexPath.row];
[tableViewData reloadData]; }
}

Is [self.devices objectAtIndex:indexPath.row]; returning that NSManagedObject you want to delete?
Then you should make the second function like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_managedObjectContext deleteObject:[self.devices objectAtIndex:indexPath.row]];
[_managedObjectContext save:nil];
[self.devices removeObjectAtIndex:indexPath.row];
[tableViewData reloadData]; }
}

Instead of reloading while of UitableView you should only reloadrowatindexpaths: and before it [UITableView beganEditing];
And after Reloading rows
[UITableView endEditing];

Related

IOS/Objective-C: Deleting row and coreData in Editing Style method - UITable

I believe this is a simple task, but so far I'm only able to delete the row itself but not the CoreData. The code I'm trying to write - fetch and NSManaged Object is not working, and I'm a bit confused. Can someone help me out? What I've got:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:#"Spot"];
NSManagedObject *managedObject = [request objectAtIndexPath:indexPath];
[self.managedObjectContext deleteObject:managedObject];
[self.managedObjectContext save:nil];
//delets the row but not the core data
[_locations removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
Do it this way and in this order:
Get the object from the data source array by index(path).
Delete the object in the context.
Save the context and check the error.
On success remove the object from the data source array and delete the row in the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSManagedObject *objectToDelete = [_locations objectAtIndex:indexPath.row];
[self.managedObjectContext deleteObject: objectToDelete];
NSError *error;
[self.managedObjectContext save:&error];
if (error) {
NSLog(#"Could not save managed object context due to error: %#", error);
} else {
[_locations removeObject:objectToDelete];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
}

Objective C -> Records only delete from table view but not from core data

I use the following code to delete the records from my table view and from core data. But the records are only delete permanent from the table view but not from core data:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.managedObjectContext deleteObject:[self.fetchedRecordsArray objectAtIndex:indexPath.row]];
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(#"Whoops, couldn't save: %#", [error localizedDescription]);
}
tpAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
self.fetchedRecordsArray = [appDelegate getAllRecords];
[tableView endUpdates];
}
}
In CoreData, first you have to fetch objects that has to be deleted, using NSFetchRequest and NSPredicate.
Then, you should iterate through NSManagedObject and call deleteObject.

accessing indexPath.row field variables

In xcode, I am trying to add some way to log what is deleted from a tableview.
Here is my delete code
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete object from database
[context deleteObject:[self.contacts objectAtIndex:indexPath.row]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Can't Delete! %# %#", error, [error localizedDescription]);
return;
}
NSManagedObject *device = [self.contacts objectAtIndex:indexPath.row];
NSLog(#"Deleting %# ", [device valueForKey:#"name1"]);
// Remove contact from table view
[self.contacts removeObjectAtIndex:indexPath.row];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
all fairly standard stuff except for these two lines
NSManagedObject *device = [self.contacts objectAtIndex:indexPath.row];
NSLog(#"Deleting %# ", [device valueForKey:#"name1"]);
I'm trying to access the data in the fields in this row before it is deleted, but NSLog returns null. I've tried other variations, and had one version that returned the variables in the previous row!
What am I missing here?
You are trying to print an object after you have deleted it and committed this action by saving.
You could just move the following lines:
NSManagedObject *device = [self.contacts objectAtIndex:indexPath.row];
NSLog(#"Deleting %# ", [device valueForKey:#"name1"]);
above your call to [context save:&error]
It seems to me you are trying to access the data after you have deleted it. Move this
NSManagedObject *device = [self.contacts objectAtIndex:indexPath.row];
NSLog(#"Deleting %# ", [device valueForKey:#"name1"]);
before this
[context deleteObject:[self.contacts objectAtIndex:indexPath.row]];

UITableview cell delete. but deosn't reload cell

I want to refresh tagListTableView.
but it doesn't refresh.
When delete button on UITableview cell is tapped , object in coraData's date could be removed.
but, Deleting a cell in a table view is still exposed to the screen to blank.
I tried to [tagListTableView reloadData] on viewWillAppear, viewDidAppear, .....more.
What am I missing??
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
Voca_Tag *tag = (Voca_Tag *)[self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tagListTableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
[tag.managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
[tagListTableView reloadData];
[pickedTags removeObject:tag];
cell.accessoryType = UITableViewCellAccessoryNone;
NSError *error = nil;
if (![tag.managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
if (editingStyle == UITableViewCellEditingStyleDelete) {
Voca_Tag *tag = (Voca_Tag *)[self.fetchedResultsController objectAtIndexPath:indexPath];
UITableViewCell *cell = [tagListTableView cellForRowAtIndexPath:indexPath];
[cell setSelected:NO animated:YES];
[tag.managedObjectContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]]; // [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
//********
//REMOVE the object before reloading the table
[pickedTags removeObject:tag];
[tagListTableView reloadData];
cell.accessoryType = UITableViewCellAccessoryNone;
NSError *error = nil;
if (![tag.managedObjectContext save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
You should remove object from your pickedTags before you reload the tableview, you were doing is after reload.
It might help you.

Error after removing an object from ManagedContext and Removing cell from TableView

when I remove one row from my TableView with this code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
NSError *error = nil;
NSLog(#"Rows before deletion: %d", [[self.fetchedResultsController fetchedObjects] count]);
NSManagedObject *obj = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.managedContext deleteObject:obj];
NSLog(#"Rows before save: %d", [[self.fetchedResultsController fetchedObjects] count]);
if (![self.managedContext save:&error]) {
NSLog(#"Error while deleting time: %s", error.localizedDescription);
}
NSLog(#"Rows after save: %d", [[self.fetchedResultsController fetchedObjects] count]);
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
Then I get the following Error. What could be the reason? The count of objects is everytime the same. Why the context don't get this information of the removed object. Wasn't it automatic?
The error is that the count of rows after the row deletion must be plus or minus the removed/added rows.
Thanks
After saving your context, you a have to re-execute your request to "update" your self.fetchedResultsController.
if (![self.managedContext save:&error]) {
NSLog(#"Error while deleting time: %s", error.localizedDescription);
}
self.fetchedResultsController = ...
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
...

Resources