ios - Core Data Migration: Could not cast value of type 'NSManagedObject_MyType' to 'MyModule.MyType' -


i'm doing 'medium-weight' core data migration. i'm using mapping model migrate 1 legacy store / data model different store , different model (i.e. different .xcdatamodeld) files, , using custom nsentitymigrationpolicy objects applicable.

where had sorts of objects unrelated on object graph, want have master object library enable me wipe out of associated data (using cascade delete rule).

i've run problems during migration because of custom method in nsentitymigrationpolicy subclass:

class legacytomodernpolicy: nsentitymigrationpolicy {  func libraryformanager(_ manager: nsmigrationmanager) -> library {      let fetchrequest: nsfetchrequest<library> = nsfetchrequest(entityname: library.entity().name!)      fetchrequest.predicate = nil     fetchrequest.sortdescriptors = [nssortdescriptor(key: "filename", ascending: true)]     fetchrequest.fetchlimit = 1      {         // fail here if nsfetchrequest<library>         let results = try manager.destinationcontext.fetch(fetchrequest)           log.info("results: \(results)")          if results.count == 1 {             // can fail here if nsfetchrequest<nsmanagedobject>             return results.first! as! library           } else {             let newlib = library(context: manager.destinationcontext)             return newlib         }      } catch {         log.error("error fetching: \(error.localizeddescription)")     }      let newlib = library(context: manager.destinationcontext)     return newlib   } } 

an exception thrown, , error message is:

could not cast value of type 'nsmanagedobject_library_' (0x6100000504d0) 'songbooksimple.library' (0x101679180).

the question is, why happening, , matter? because migration happening, perhaps it's enough return nsmanagedobject correct entity description ?

the reason during migration, should not using instances of nsmanagedobject subclasses. need express of these in form of nsmanagedobject. code above must become:

class legacytomodernpolicy: nsentitymigrationpolicy {  static func find(entityname: string,                  in context: nsmanagedobjectcontext,                  sortdescriptors: [nssortdescriptor],                  predicate: nspredicate? = nil,                  limit: int? = nil) throws -> [nsmanagedobject] {      let fetchrequest: nsfetchrequest<nsmanagedobject> = nsfetchrequest(entityname: entityname)     fetchrequest.predicate = predicate     fetchrequest.sortdescriptors = sortdescriptors     if let limit = limit {         fetchrequest.fetchlimit = limit     }      {         let results = try context.fetch(fetchrequest)         return results     } catch {         log.error("error fetching: \(error.localizeddescription)")         throw error     } }  func libraryformanager(_ manager: nsmigrationmanager) -> nsmanagedobject {      {         var library: nsmanagedobject? = try legacytomodernpolicy.find(entityname: library.entity().name!,                                                 in: manager.destinationcontext,                                                 sortdescriptors: [nssortdescriptor(key: "filename", ascending: true)],                                                 with: nil,                                                 limit: 1).first         if library == nil {             let dinstance = nsentitydescription.insertnewobject(forentityname: library.entity().name!, into: manager.destinationcontext)              // awakefrominsert not called, have things did there, here:             dinstance.setvalue(library.libraryfilename, forkey: #keypath(library.filename))             dinstance.setvalue(nsdate(timeintervalsince1970: 0), forkey: #keypath(library.updatedat))             library = dinstance         }          return library!      } catch {         fatalerror("not sure why failing!")     } }} 

you can read more less-than-fun experiences core data migrations here.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -