I am working on a iOS application, the model is very simple (Folders and Documents), but I am having problems for making it working with NSFetchedResultsController.
The app uses a UITableView in a NavigationController for showing a list of folders and documents contained in a folder. The user can browse the folders hierarchy with a drilldown effect. Also, the user can copy/paste folders and documents, so any document or folder could have many parents.
This is my current model:
AbstractElement{
elementName:string
}
Folder{
parents<<---->>Folder.folders
folders<<---->>Folder.parents
documents<<---->>Document.parents
}
Document{
numPages:int
parents<<---->>Folder.documents
}
This model works fine if I fill the UITableView with an NSArray created from the current folder:
NSArray * currentListOfElements =
[[self.currentFolder.documents sortedArrayUsingDescriptors:descriptors]
arrayByAddingObjectsFromArray:
[self.currentFolder.folders sortedArrayUsingDescriptors:descriptors]];
The problem is that I need to fill the UITableView from a NSFetchedResultsController, getting folders and documents with a single NSFetchRequest. The solution is something like that:
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"AbstractElement" inManagedObjectContext:self.context];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"%@ IN self.parents", self.currentFolder]];
...create the NSFetchedResultsController using fetchRequest...
But this doesn’t work because AbstractElement doesn’t have “parents” relationship.
If I move parent relationship from Folder and Document to AbstractElement the problem is how to set the inverse relation:
AbstractElement{
elementName:string
parents<<---->> ??
}
Folder{
folders<<---->>Folder.parents
documents<<---->>Document.parents
}
Document{
numPages:int
}
Is this the right aproach? How could I use NSFetchedResultsController with my model? I don’t want to remove inverse relations because of the extra code that I need to implement.
Thanks.
Louissmr,
You say,
Hence, What is wrong with having 2 to-many relations in the
AbstractElement. Call them, say,parentsandchildren. It meets your criteria. Each is the inverse of the other.Andrew