magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.79k stars 1.79k forks source link

Save fails after migrating to MagicalRecord #1243

Open besi opened 8 years ago

besi commented 8 years ago

When my app exists and I want to save my MOC then I always get success = false but without an NSError.

  1. Create a default CoreData Master Detail application
  2. Setup cocoapods and add XCLogger as well as MagicalRecord pods
  3. Change the AppDelegate.swift like shown below.
  4. Run The app and Tap the + button a few times --> Timestamps are added to the list
  5. CMD + SHIFT + H to dismiss the App into the background
  6. The saveContext method is called but in the callback the success is set to false and error is nil

Since this is otherwise the default "Master Detail" application template I am wondering what I have to do to get the context to save or how to get some information about what is going wrong for the save.

I checked and MagicalRecord.currentStack() shows show me that the NSPersistentStorecoordinator etc. was created.

AppDelegate.swift

import UIKit
import CoreData
import MagicalRecord
import XCGLogger

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UISplitViewControllerDelegate {

    var window: UIWindow?

    let log = XCGLogger.defaultInstance()

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

        // Override point for customization after application launch.
        let splitViewController = self.window!.rootViewController as! UISplitViewController
        let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
        navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
        splitViewController.delegate = self

        let masterNavigationController = splitViewController.viewControllers[0] as! UINavigationController
        let controller = masterNavigationController.topViewController as! MasterViewController
        controller.managedObjectContext = managedObjectContext

        // I had to add this line because otherwise I would get a core-data-fault-exception (probably related to the objects not being saved
        NSFetchedResultsController.deleteCacheWithName("Master")

        return true
    }

    // lazy Magical Record NSManagedObjectContext
    lazy var managedObjectContext: NSManagedObjectContext = {
        MagicalRecord.setupAutoMigratingCoreDataStack()
        let moc = NSManagedObjectContext.MR_defaultContext()
        moc.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        return moc
    }()

    func applicationWillResignActive(application: UIApplication) {
        self.saveContext()
    }

    func applicationWillTerminate(application: UIApplication) {
        self.saveContext()
    }

    // MARK: - Split view

    func splitViewController(splitViewController: UISplitViewController, collapseSecondaryViewController secondaryViewController:UIViewController, ontoPrimaryViewController primaryViewController:UIViewController) -> Bool {
        guard let secondaryAsNavController = secondaryViewController as? UINavigationController else { return false }
        guard let topAsDetailController = secondaryAsNavController.topViewController as? DetailViewController else { return false }
        if topAsDetailController.detailItem == nil {
            // Return true to indicate that we have handled the collapse by doing nothing; the secondary controller will be discarded.
            return true
        }
        return false
    }
    // MARK: - Core Data stack

    // MARK: - Core Data Saving support

    func saveContext () {

        log.info("")
        NSManagedObjectContext.MR_defaultContext().MR_saveToPersistentStoreWithCompletion { (success, error)  -> Void in

            if success == false{
                // TODO:
                self.log.error("Could not save")
            }

            if let e = error{
                self.log.info(e.localizedDescription)
            }
        }
    }
}

Podfile

platform :ios, '9.0'

target 'MyApp' do
  use_frameworks!

  pod 'XCGLogger'
  pod 'MagicalRecord'

end

Internal reference vost#15

dkun7944 commented 7 years ago

@besi Did you ever figure this out?

besi commented 7 years ago

No. The project was discontinued.

On 10 May 2017, at 22:35, Daniel Kuntz notifications@github.com wrote:

@besi Did you ever figure this out?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.