I have a UITableView that lists comments from various users. I save all of the comments to Core Data. I have also implemented NSFetchedResultsController. One of the properties of comments is username – the user who made the comment.
I only want users to be able to delete their own comments. Here is my code:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
//Only be able to delete user generated cells
Comment *comment = [[self fetchedResultsController]objectAtIndexPath:indexPath];
if (comment.username != self.username) {
return NO;
}
return YES;
}
performfetch is called in viewDidLoad
When the CommentsViewController is loaded and new comments are created, the functionality works. However, if there were any previous comments, those comments cannot be edited even though the user has created them.
Any thoughts on what might be going on?
I think the issue is the deep equality check on the username. Assuming those are strings, you don’t want to test if they are the very same object (they will be in one fetch, and won’t be between runs). You want to test shallow equality…