Swift - CoreData Passing PersistentController to many ViewControllers through UITabBarController and UINavigationControllers
<p>I'm developing my first app and was injecting ManagedObjectContext from AppDelegate.
Yesterday I found a new approach and created separated CoreDataStack like <a href="http://martiancraft.com/blog/2015/03/core-data-stack/" rel="nofollow">here</a>.
And my app storyboard is like that:
<a href="http://i.stack.imgur.com/Fwcel.png" rel="nofollow">My storyboard</a>
There are one <code>UITabBarController</code>, two <code>UINavigationControllers</code>. And you can see which <code>view controller's</code> use CoreData. </p>

<p><strong>Questions</strong>:</p>

<ol>
<li>Pass my CoreDataStack to every view controller, that uses CoreData. Is my approach is correct or not?</li>
</ol>

<p>Appdelegate's code:</p>

<pre><code>func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
coreDataStack = CoreDataStack(){
  (response) -&gt; Void in
  self.completeAnySetup()
}
//Pass NSManagedObjectContext to many VCs in TabBarVC
let mainTabBarVC: UITabBarController = self.window!.rootViewController as! UITabBarController
if let navControllerOne = mainTabBarVC.viewControllers![0] as? UINavigationController {
  let mainMenuVC = navControllerOne.viewControllers[0] as! MainMenuVC
  mainMenuVC.coreDataStack = self.coreDataStack
}
return true }
</code></pre>

<p>Then from <code>navControllerOne.viewControllers[0]</code> i inject it into next VC:</p>

<pre><code>override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == Segues.TrackedHistoryVC {
  if let trackedHistoryVC = segue.destinationViewController as? TrackedHistoryVC {
    trackedHistoryVC.hidesBottomBarWhenPushed = true
    let backItem = UIBarButtonItem()
    backItem.title = ""
    navigationItem.backBarButtonItem = backItem
    if let coreDataStack = coreDataStack {
      trackedHistoryVC.coreDataStack = coreDataStack
    }
  }
}}
</code></pre>

<p>And so on. But i think it's more complicated that just old way: <code>let appDelegate = ..., let psc = ....</code> .</p>

<ol start="2">
<li>Now after this manipulations after fetching data from CoreData it's give me just a empty object and also save an empty object. And i don't know why? 
For example my function to fetch objects:</li>
</ol>

<blockquote>
<pre><code>func fetchAndResult() {
    if let mainContext = mainContext {
      let fetchRequest = NSFetchRequest(entityName: "TrackedShipments")
      do {
        let results = try self.mainContext!.executeFetchRequest(fetchRequest)
        print(results)
        self.trackedShipments = results as! [TrackedShipments]
      } catch { print("Error") }
    }
  }
</code></pre>
</blockquote>

<ol start="3">
<li>A lot of people say that use CoreDataStack Singleton is not good because it's not safe or other reasons. Then is there any approach to make this Singleton be safer or may be it possible to create a singleton with multiple managedObjectContexts?</li>
</ol>

<p><strong>UPDATE</strong></p>

<ol>
<li><p>I looked a lot to find a right way and stopped on that:</p>

<blockquote>
<pre><code>private(set) var persistenceController: PersistenceController? = nil
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -&gt; Bool {
    persistenceController = PersistenceController(completion: { (result, failError) -&gt; Void in
    print("Persistent controller is loaded")
})
    guard let mainTabBarVC: UITabBarController = self.window!.rootViewController as? UITabBarController else {fatalError()}
    guard let navControllerOne = mainTabBarVC.viewControllers![0] as? UINavigationController else { fatalError() }
    guard let navControllerTwo = mainTabBarVC.viewControllers![1] as? UINavigationController else { fatalError() }
    guard let mainMenuVC = navControllerOne.topViewController as? MainMenuVC else { fatalError() }
    guard let myCabinetVC = navControllerTwo.topViewController as? MyCabinetVC else { fatalError() }
    mainMenuVC.persistenceController = persistenceController
    myCabinetVC.persistenceController = persistenceController
    return true
  }
</code></pre>
</blockquote></li>
</ol>

<p>It works very well. So only appdelegate can set persistence controller(Core Data stack), and other view controllers can only read it.</p>

<ol start="2">
<li>I found answer for that. It was very stupid mistake. After creating new Core Data stack i did't give <code>addPersistentStoreWithType</code> a right <code>store URL</code>.</li>
</ol>