0

I want to add second entity in core data file named as "Register". there are two entity 1.Register 2.Hospital. when i add second entity,application crash and pointed error in Persistancestorecoordinator "abort()".

plese give answers related to Swift what is problem with AppDelegate?

SOMEONE GIVE ME DIFFERENT STACKOVERFLOW LINK BUT there is not problem related to update version or etc. -i want to add second entity in core data . is there any change in appdelegate.

Below code is in my AppDelegate:

lazy var managedObjectModel: NSManagedObjectModel = {
    // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
    let modelURL = NSBundle.mainBundle().URLForResource("Register", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
    }()

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
    // Create the coordinator and store
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("REDCROSSPROJECTS.sqlite")
    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."
    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) == nil {
        coordinator = nil
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

    return coordinator
    }()



 lazy var managedObjectContext: NSManagedObjectContext? = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    if coordinator == nil {
        return nil
    }
    var managedObjectContext = NSManagedObjectContext()
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
    }()

1 Answers1

1

Your problem is probably related to core data migration.

During development you can just erase the app from your iPad or simulator, and run it again.

When you need to release new versions of you app with modified core data models, you'll need to learn about Core Data versioning and migration.

Leonardo
  • 1,740
  • 18
  • 27
  • no. my problem is not related to core data migration.i want to add second entity for different view controller. so i know how to add second entity,there is problem with appdelegate..... did you know , what's a problem?? – kishan patel Oct 08 '15 at 17:39
  • I'm guessing you did the following: _1.Create a core data model with 1 entity 2. Build and Run 3. Change the model to add a new entity 4. Your app started to crash._ If this was it, you cant just erase the app and run it again, for now. The crash is happening because the store your app has and the store created from the model are different – Leonardo Oct 08 '15 at 17:51
  • Thanks for fast reply @leonardo sir.. how to change model for new entity. can you give me any link or reference for core data with two entity? – kishan patel Oct 08 '15 at 17:54
  • This is the tutorial I used to learn core data : [link](http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started) and there's a new one : [link](http://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial) . – Leonardo Oct 09 '15 at 11:26