CoreData crash on migration with NSMigrationConstraintViolationError = 134111 error
Asked Answered
B

0

6

I am creating a new NSPersistentStoreCoordinator for background long savings from the server. It worked fine but suddenly I see a crash in Production with the following error -

CRASH_INFO_ENTRY_0 fatal error: Background context creation failed with error Error Domain=NSCocoaErrorDomain Code=134110 "An error occurred during persistent store migration." UserInfo={sourceURL=file:///var/mobile/Containers/Data/Application/8EF27C05-1755-49EE-B174-8B163CC7CC1D/Documents/App.sqlite, reason=Cannot migrate store in-place: constraint violation during attempted migration, destinationURL=file:///var/mobile/Containers/Data/Application/8EF27C05-1755-49EE-B174-8B163CC7CC1D/Documents/.App.sqlite.migrationdestination_41b5a6b5c6e848c462a8480cd24caef3, NSUnderlyingError=0x170a56290 {Error Domain=NSCocoaErrorDomain Code=134111 "(null)" UserInfo={reason=constraint violation during attempted migration, NSUnderlyingException=Constraint violation, _NSCoreDataOptimisticLockingFailureConflictsKey=( "" )}}}: file /Users/*******/Desktop/7.50/APPS/LTG/project/modelBase/db/DatabaseManager.swift, line 67

Lookalike I have a constraints issue as this is the error I get -

NSMigrationConstraintViolationError = 134111, // migration failed due to a violated uniqueness constraint

Bu since the _NSCoreDataOptimisticLockingFailureConflictsKey is <null> I can not trace the issue.

This is my code -

    lazy var backgroundSyncContext:NSManagedObjectContext =  {
        let psc = NSPersistentStoreCoordinator(managedObjectModel: NSManagedObjectModel.MR_defaultManagedObjectModel()!)
        let context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
        context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        context.undoManager = nil
        context.persistentStoreCoordinator = psc
        let mOptions = [NSMigratePersistentStoresAutomaticallyOption: true,
        NSInferMappingModelAutomaticallyOption: true]

        let paths = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask)
        let applicationDocumentURL = paths.last!.absoluteString?.stringByReplacingOccurrencesOfString("%20", withString: " ")
        let documentPath = paths.last!

        let storeUrl = documentPath.URLByAppendingPathComponent("Prep4\(AppConfig.sharedInstance.examName).sqlite")

        do {

            try context.persistentStoreCoordinator?.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeUrl, options: mOptions)
            print("Created background context")

        } catch (let error){

            fatalError("Background context creation failed with error \(error)")
        }

        return context

    }()

EDIT

OK, I have a guess - is the latest version I added constraints to the model. Is there a chance that the old version on the user device contains duplicates that are nor valid with the new constraints so the the migration fails.

Does that sounds reasonable ?

Thanks Shani

Baird answered 7/9, 2016 at 20:21 Comment(7)
Did you version your Core Data model before pushing a new build to production?Lights
Yes, it did pass tests before releaseBaird
Edited my questionBaird
your hypothesis about existing duplicates makes sense. I think your only option might be to ship an update without the unique constraint with code to query and resolve the duplicates on first launch. Then update again with the unique constraints in the model.Lights
I am having the same problem. It appears that lightweight automatic migration is capable of adding a uniqueness constraint, but ONLY if there are NO duplicates already in the database. So far I have not found any way to handle this error gracefully (e.g., delete the duplicate objects during migration).Trudey
What we did is recreating the user data base if there is a conflict in the migration process.Baird
@Trudey I think you should put your comment as an answer.Zymogen

© 2022 - 2024 — McMap. All rights reserved.