ios - Why is my Core Data migration crashing with EXC_BAD_ACCESS -
you may use out of a post made relates same task:
i'm trying migrate , old data store new object model, , lightweight data migrations not going work. i'm using mapping model , custom nsentitymigrationpolicy subclasses used define helper methods can call mapping model.
i've been learning doing, , each step of migration process fail, fix it, goes bit further, stuck.
so seem have passed validation step , models save, i'm getting hard crash when calling on nsmigrationmanager:
[manager migratestorefromurl:sourcestoreurl type:type options:nil withmappingmodel:mappingmodel todestinationurl:destinationstoreurl destinationtype:type destinationoptions:nil error:error]; and it's 1 of unhelpful crashes because it's exc_bad_access , can't find out or causing it. know stack trace in debugger looks like:
(lldb) thread backtrace * thread #1, queue = 'com.apple.main-thread', stop reason = exc_bad_access (code=1, address=0x2c8) frame #0: 0x000000010ac97ac5 libobjc.a.dylib`objc_msgsend + 5 frame #1: 0x000000010b1911e3 coredata`_pfmanagedobject_coercevalueforkeywithdescription + 1187 frame #2: 0x000000010b16d9d7 coredata`_sharedimpl_setvfk_core + 231 frame #3: 0x000000010b1e4e18 coredata`-[nsentitymigrationpolicy createdestinationinstancesforsourceinstance:entitymapping:manager:error:] + 744 frame #4: 0x000000010b22aa67 coredata`-[nsmigrationmanager(internalmethods) _dofirstpassformapping:error:] + 407 frame #5: 0x000000010b22c1a3 coredata`-[nsmigrationmanager(internalmethods) _migratestorefromurl:type:options:withmappingmodel:todestinationurl:destinationtype:destinationoptions:error:] + 2003 frame #6: 0x000000010b228d59 coredata`-[nsmigrationmanager migratestorefromurl:type:options:withmappingmodel:todestinationurl:destinationtype:destinationoptions:error:] + 777 so based on frame#1, i'm guessing it's trying migrate nsnumber scalar. possible? i'm wondering if has migrated older store newer store scalars allowed.
the reason why crashing because defined in mapping model referenced custom code, , code failed.
if have entity mapping isn't straightforward, recommend removing property mappings entity mapping, override:
func createdestinationinstances(forsource sinstance: nsmanagedobject, in mapping: nsentitymapping, manager: nsmigrationmanager) throws where specify properties should be. might have 1 of these custom methods passing instance of subclass of nsmanagedobject, should not during migration.
you can see in stack trace of original question failed trying convert 1 value "coercevalueforkey..."
for example:
override func createdestinationinstances(forsource sinstance: nsmanagedobject, in mapping: nsentitymapping, manager: nsmigrationmanager) throws { let dinstance = nsentitydescription.insertnewobject(forentityname: mapping.destinationentityname!, into: manager.destinationcontext) let text = sinstance.value(forkey: "text") as! string? dinstance.setvalue(sinstance.value(forkey: "lastmodified"), forkey: #keypath(song.createdat)) dinstance.setvalue(sinstance.value(forkey: "lastmodified"), forkey: #keypath(song.updatedat)) dinstance.setvalue("txt", forkey: #keypath(song.fileextension)) dinstance.setvalue(sinstance.value(forkey: "filename"), forkey: #keypath(song.filename)) dinstance.setvalue(sinstance.value(forkey: "firstletteruppercase"), forkey: #keypath(song.firstletteruppercase)) dinstance.setvalue(sinstance.value(forkey: "name"), forkey: #keypath(song.title)) dinstance.setvalue(sinstance.value(forkey: "oldfilename"), forkey: #keypath(song.oldfilename)) dinstance.setvalue(self.songdatalengthoftext(text), forkey: #keypath(song.songdatalength)) dinstance.setvalue(text, forkey: #keypath(song.text)) dinstance.setvalue(songdatatype.plaintext.rawvalue, forkey: #keypath(song.typeraw)) dinstance.setvalue(nil, forkey: #keypath(song.userinfodata)) // new data model added to-one association song wasn't present previously. have create it, // not yet associate it. provide means associate it, via filename/songfilename let songdata = nsentitydescription.insertnewobject(forentityname: songdata.entity().name!, into: manager.destinationcontext) songdata.setvalue(sinstance.value(forkey: "filename"), forkey: #keypath(songdata.songfilename)) let structure = songdatastructure() // don't worry this. it's used serialization. structure.set(text: text) songdata.setvalue(structure.serialize(), forkey: #keypath(songdata.nsdata)) // new data model added to-one association song wasn't present previously. have create it, // not yet associate it. provide means associate it, via filename/songfilename let viewpreferences = nsentitydescription.insertnewobject(forentityname: songviewpreferences.entity().name!, into: manager.destinationcontext) viewpreferences.setvalue(sinstance.value(forkey: "filename"), forkey: #keypath(songviewpreferences.songfilename)) // important associate these! manager.associate(sourceinstance: sinstance, withdestinationinstance: dinstance, for: mapping) return }
Comments
Post a Comment