0

So I have been updating my code for Swift 3.0 and when the app is opened I sync with iCloud and execute a fetch request.

However I get the optional error for the following

let appDelegate = UIApplication.shared.delegate as! AppDelegate
     appDelegate.syncWithCompletion { (completed) -> Void in
}

Which executes the following function in the appDelegate

func syncWithCompletion(_ completion:@escaping (_ completed:Bool) -> Void) {

      if !ensemble.isLeeched {
            ensemble.leechPersistentStore { error in
                if error != nil {
                    print("cannot leech \(error!.localizedDescription)")
                    completion(false)
                }
                else {
                    print("leached!!")
                    completion(true)
                }
            }
      }
      else {
            ensemble.merge{ error in
                if error != nil {
                    print("cannot merge \(error!.localizedDescription)")
                    completion(false)
                }
                else {
                    print("merged!!")
                    completion(true)
                    //NSNotificationCenter.defaultCenter().postNotificationName("Updated-DB", object: nil)
                }
           }
      }
}

I am not sure what the optional error is for, Xcode updated the function header when migrating.

UPDATE:

So apparently the following have a nil value

enter image description here

But I am unsure what is causing this since the Swift 3 migration

A.Roe
  • 973
  • 3
  • 15
  • 34

1 Answers1

1

So I found that the error was because of the didFinishLaunchingWithOptions function in the delegate. The function was not migrated correctly.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: Any]?) -> Bool {

I created a new project and used the following instead

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

and it worked. This is because the following was not being called therefore my core data stack was not set up

self.setupCoreData()
A.Roe
  • 973
  • 3
  • 15
  • 34
  • Why don't you use the (automatically) lazy initialized Core Data Stack as suggested in the Apple template? – vadian Sep 06 '16 at 14:42