Swift - CoreData通过UITabBarController和UINavigationControllers

时间:2016-02-23 10:45:58

标签: ios iphone swift uitableview core-data

我正在开发我的第一个应用程序,并从AppDelegate注入ManagedObjectContext。 昨天我找到了一种新的方法,并创建了像here这样的独立CoreDataStack。 我的应用故事板是这样的: My storyboard 有一个UITabBarController,两个UINavigationControllers。您可以看到哪个view controller's使用CoreData。

问题

  1. 将我的CoreDataStack传递给使用CoreData的每个视图控制器。我的方法是否正确?
  2. Appdelegate的代码:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    coreDataStack = CoreDataStack(){
      (response) -> 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 }
    

    然后从navControllerOne.viewControllers[0]我将它注入下一个VC:

    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
        }
      }
    }}
    

    等等。但我认为这种方式更为复杂:let appDelegate = ..., let psc = ....

    1. 现在在从CoreData获取数据之后进行此操作后,它只给我一个空对象并保存一个空对象。我不知道为什么? 例如我的获取对象的函数:
    2. 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") }
          }
        }
      
      1. 很多人说使用CoreDataStack Singleton并不好,因为它不安全或其他原因。那么是否有任何方法可以使这个Singleton更安全,或者可以创建一个包含多个managedObjectContexts的单例?
      2. 更新

        1. 我看了很多,找到了正确的方法并停下来:

          private(set) var persistenceController: PersistenceController? = nil
              func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
              persistenceController = PersistenceController(completion: { (result, failError) -> 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
            }
          
        2. 效果很好。因此,只有appdelegate可以设置持久性控制器(Core Data stack),而其他视图控制器只能读取它。

          1. 我找到了答案。这是一个非常愚蠢的错误。创建新的核心数据堆栈后,我没有给addPersistentStoreWithType一个正确的store URL

0 个答案:

没有答案