Error with self-migrating stack - Chapter 6

I’ve a problem with this part of the book, my code is correct (I’ve even tried it with the book’s source code) and I’m getting always the fatalError(“Can only handle migrations to version 4!”). My DB is not empty because I first added info with version 2. I checked the error and the problem seems that .isVersion# always is false.

Does anyone know what’s the problem?

I’m reading the last version of the book and currently using Xcode 9.3.

Thanks in advance.

@barrera Thanks very much for your question!

Is it possible for you to show us some of your code which is causing the error?

My code is exactly as the one provided with the book, both have the same problem.

I’m guessing the problem is between these parts of the code:

I’m getting always this fatal error, because currentModel.isVersion4 is always false.

func performMigration() {
    if !currentModel.isVersion4 {
      fatalError("Can only handle migrations to version 4!")
    }
}

I made a test printing the values of currentModel.isVersion1, 2, 3, and 4, and I’m getting always false

The code for isVersion# is this:

var isVersion4: Bool {
    return self == type(of: self).version4
}

The class var version4 is this:

class var version4: NSManagedObjectModel {
    return uncloudNotesModel(named: "UnCloudNotesDataModel v4")
}

And the method uncloudNotesModel is this:

class func uncloudNotesModel(named modelName: String) -> NSManagedObjectModel {
    let model = modelURLs(in: "UnCloudNotesDataModel")
      .filter { $0.lastPathComponent == "\(modelName).mom" }
      .first
      .flatMap(NSManagedObjectModel.init)

    print(modelURLs(in: "UnCloudNotesDataModel"))

    return model ?? NSManagedObjectModel()
}

Hope it helps :confused:

It sounds like you need to set which version is the current version. This is set to version 4:

image

If you click on one of the versions, then pick the file inspector over on the right, you can see a dropdown that lets you set which version is current:

image

This topic was automatically closed after 166 days. New replies are no longer allowed.