iosuitableviewnspredicatensfetchedresultscontroller

Core Data: Can a NSPredicate in fetchResultsController return multiple objects for each row, into my Table View?


Can a fetchedResultsController return an Array of Arrays? Or An array of NSSets?

Code below is an example, of what I'm trying to do. Is it possible?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //...
    NSSet *objects = [_fetchedResultsController objectAtIndexPath:indexPath];
    //...
}

The reason I'm doing this, is: if the user swipes the cell, and deletes it. I need all objects from that row deleted. And I also need to display data on that Cell, from calculation made on all the clocks for that Day/Row.

Here's my Core Data Model:

enter image description here

Each row must contain all Clock objects, for a given day, based on it's clockIn property. clockIn is a NSDate object. One row should represent one day.

Could I get help figuring out a predicate for this? Is it even possible?

Also, I'm already using sections in my Table View. So these are out of the question. There is one solution that I rather no go for, which is to create Year, Month and Day, Entities. This would fix. But it seems odd that I need to do that, considering the my clockIn property should give me everything I need.


Solution

  • No, it can't.

    As stated here: NSPredicate something equivalent of SQL's GROUP BY

    "CoreData is an object graph management framework, not a SQL data store. Thus, you should—as quickly as possible—get yourself out of the SQL mindset."

    And in regard to the question in hands:

    "NSPredicate is intended as a—no surprise—predicate for qualifying objects in the object graph. Thus you can fetch all the objects that match a query, but that is not the same as grouping those results. If you want to do aggregate operations, use Key-Value coding's collection operators. Under the hood, Core Data may convert these to sensible SQL, but that's exclusively an implementation detail."