Pregunta

I think I'm just missing something obvious here, but it's one of those frustrating things that's somehow eluding me.

I have a Core Data Entity called ProjectEntry. The ProjectEntry objects are displayed in uitableviews, using various attributes, arranged by date (attributes include things like "dateAsNSDate"[NSDate], "month"[NSString], "year"[NSString], "dayOfWeek"[NSString]).

I'm using an NSFetchedResultsController to populate the table views.

When I initially create and save the ProjectEntry object, the "dateAsNSDate" attribute is parsed and converted into various NSStrings. One string, also an attribute, is called "concatMonthAndYear". It takes the "month" and "year" strings and just joins them. So I get things such as "January 2014", "February 2015", etc.

I use the "concatMonthAndYear" as my cell.textLabel.text string to display in my tableview cells.

I use the NSDate attribute to sort the tableview rows (sortDescriptor), and the "year" attribute as my section headers (sectionNameKeyPath).

So right now, I'd have a tableview section called "2014", with tableview rows each representing a Core Data object, named things like "January 2014", February 2014", etc, in said section.

I can tap on one of those rows, segue to another tableview, and list all objects created in January 2014, for example, by using an NSPredicate on the second tableview.

However, on my first tableview, each Core Data object created is represented by its own tableview row. So I'll get multiple rows reading "January 2014" or "May 2015" or whatever. They're valid saved objects, and I want them, but I'd like to prevent a new row from being created if that "concatMonthAndYear" already exists. If a row titled "January 2014" already exists, I don't want a new row created. I want the new Core Data object stored, just not a new tableviewrow representing it. I only need one row with "January 2014", for example, to segue into a table listing ALL the entities from January 2014.

I know how to use an NSPredicate to get ALL the January 2014 objects into the second table, but how do I get JUST ONE object into the first table?

Is NSPredicate not the right device for that? Should I be somehow preventing a new cell from being created in the UITableView delegate methods? Each tableview row should be unique, and I'm stuck on whether it should be handled with the NSFetchedResults controller or in the tableview delegate methods?

Or some other way?

Can someone point in the right direction?

Table 1 Table 2

EDITED TO INCLUDE CODE:

- (void)setupFetchedResultsController
{
    // 1 - Decide which Entity
    NSString *entityName = @"ProjectEntry";
    NSLog(@"Setting up a Fetched Results Controller for the Entity named %@", entityName);

    // 2 - Request Entity
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];

[request setReturnsDistinctResults:YES];
[request setResultType:NSDictionaryResultType];
[request setPropertiesToFetch:@[@"monthYearTableSecHeader", @"year"]];

    // 3 - Filter it
    //request.predicate = [NSPredicate predicateWithFormat:@" "];

    // 4 - Sort it
    request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"year"
                                                                                                 ascending:NO],
                                                         [NSSortDescriptor sortDescriptorWithKey:@"dateAsNSDate"
                                                                                                                                             ascending:YES
                                                                                       selector:@selector(localizedCaseInsensitiveCompare:)], nil];


    //5 - Fetch
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.managedObjectContext
                                                                          sectionNameKeyPath:@"year"
                                                                                   cacheName:nil];

    [self performFetch];

}
¿Fue útil?

Solución

You could use

[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:@[@"concatMonthAndYear", @"year"]];

This will cause the fetch request to return distinct dictionary objects corresponding to "January 2014", etc. objects.

However, you cannot use a fetch request controller's delegate methods (to hear of updates to the data).

If you need to hear updates, I suggest you add a layer of indirection to your data, where MonthEntry is an object representing yearly months and have a one to many relationship with ProjectEntry, which is your normal entity. This way, you can set the fetch request entity to MonthEntry.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top