magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.79k stars 1.79k forks source link

Question: Multiple Entities in one NSFetchedResultsController #134

Closed georgbachmann closed 11 years ago

georgbachmann commented 12 years ago

Hello, I don't know if this question is specific to MagicalRecord or if it is a CoreData question.

What I am trying to archive is to represent a folder-structure with subfolders and files.

If love to have an NSFetchedResultsController with two sections. First: subfolders Second: files

I can make two seperate NSFetchedResultsController by doing something like this: NSFetchedResultsController* subFolderFetchedResultsController = [Folder fetchAllSortedBy:@"folderName" ascending:YES withPredicate:[NSPredicate predicateWithFormat:@"parent == %@", folder] groupBy:nil delegate:self];

and

NSFetchedResultsController* filesFetchedResultsController = [File fetchAllSortedBy:@"folderName" ascending:YES withPredicate:[NSPredicate predicateWithFormat:@"folder == %@", folder] groupBy:nil delegate:self];

but i'd prefer to have just one NSFetchedResultsController. Is there any solution on how i can make this working?

Thanks, Georg

georgbachmann commented 12 years ago

Ok, I already found the solution for my problem. I made a superclass for my NSManagedObjects, and make the Request like [SuperClass fetchAllSortedBy:.......]

But... I now have a new problem ;)

I have this one super class and in a TableViewController I would like to display online Items of a certain kind. So I want to display items of ClassA and ClassB but not ClassC. All of those classes inherit from this one superclass. Is there a possibility to tell the fetch with for example an NSPredicate that it should only care about results that are of ClassA and B?

I hope you do understand my problem?!? I guess it's similar to this problem here: http://stackoverflow.com/questions/2554894/is-it-possible-to-filter-an-nsarray-by-class

But unfortunately [NSPredicate predicateWithFormat:@"self isKindOfClass: %@",[ClassA class]]; is not working with MR.

Any help is appreciated!

Thanks!

tonyarnold commented 11 years ago

Hi @georgbachman — I assume you’ve long since resolved this problem, but for posterity’s sake, the best way I can think of is to embed the entity name into the model for just these types of objects. You’d need to be ready to migrate the data if you ever renamed an entity, but you could do it reasonably automatically using an abstract parent entity, with child entities that specify their type with a default value set to their entity name.

You’re not alone in wanting this.

Be sure to post back here if you found a better way to handle this in the time that’s passed!