My application uses locations data and presents it both as a table and in a map.
It starts with a Tab Bar Controller, each of it's views is a Navigation Controller (one for the table view, one for the map, etc...).
I want to share a UIManagedObject in order to use the same Managed Object Context so if the user updates at the table view, the data also gets updated for the map, so there is no need to update twice.
Originally i thought of subclassing the Tab Bar Controller and adding a UIManagedDocument as a property, and just passing it to each controller on the prepare for segue method. But i read that UITabBarController is not meant to be subclassed.
Another approach could be creating a View Controller, adding the Managed Document as property, and a Tab Bar to it. But i think that my storyboard would be unclear or inconsistent by showing some relationships graphically and others just in code.
Which one is the appropriate? Or is there a better way to do it?
Thanks in advance and best regards.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showGuide"])
{
UITabBarController *tabBarController = (UITabBarController *)[segue destinationViewController];
for (id vc in tabBarController.viewControllers) {
[vc setValue:_document forKey:@"document"];
}
}
I ran into this issue too and I settled on a separate document handler class that provides access to the loaded document via a block.
[[MYDocumentHandler sharedDocumentHandler] performWithDocument:^(UIManagedDocument *document) {
// Do stuff with the document, set up a fetched results controller, whatever.
}];
I've written up my solution and posted the code here: Core Data with a Single Shared UIManagedDocument
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With