I have a simple application with the following premise and a few minor issues:
- I have 4 Tabs, each with a table view displaying different information and each with a plus button going modally to the same view controller, allowing users to add in information to the app.
- The user adds a Name, Title, Date and Amount in the view controller and when they press save, it gets saved to the Core Data table view.
The first tab displays ALL for bits of information above (Name, Title, Date, Amount) in a custom table cell with labels, etc. That works well.
The second tab displays only information on the Name, the third tab displays only the Titles. This way, a user sees everything in the first Tab, but only the "Names" in the second tab which they can go select to gather information about that name.
The app is working well when there are only one entry per name (or date or title), but the moment I add a second entry, it duplicates in the Name tab.
For example, if I have an entry for John Smith with only a single entry, it will display that in the Name tab with one entry and when I go in, it shows me only transactions that have John's name attached to it (1 in this case). All good.
However, if I add another Entry for John (spelt exactly the same), it treats it as a separate entry and suddenly, the Name tab now has 2 John's.
I have not put any unique IDs in the attributes of the Core Data Model, but do I have to do this, or could I just use a predicate to ask "if this already exists, don't display it twice" in the Name Tab Bar?
Any assistance would be massively appreciated!
Edit:
Here is the fetchRequest for the Name tab:
- (NSFetchedResultsController *)fetchedResultsController
{
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Transaction" inManagedObjectContext:managedObjectContext];
fetchRequest.entity = entity;
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"whoBy.name" ascending:NO];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:sort];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
As we can see, there are no predicates or anything similar. What I want is for a simple check to see "if name exists, don't add it twice" to the Name table view. I hope this makes sense?