gpt4 book ai didi

ios - 为什么我的核心数据迁移因 EXC_BAD_ACCESS 而崩溃

转载 作者:行者123 更新时间:2023-11-29 00:12:08 26 4
gpt4 key购买 nike

您还可以使用 a post I made that relates to the same task :

我正在尝试将旧数据存储迁移到新的对象模型,因此轻量级数据迁移无法正常工作。我正在使用一个映射模型,然后使用一些自定义的 NSEntityMigrationPolicy 子类,这些子类主要用于定义我可以从映射模型调用的辅助方法。

我一直边做边学,迁移过程的每一步都会失败,然后我修复它,然后它走得更远,然后我就卡住了。

所以我似乎已经通过了验证步骤并且模型将被保存,但是现在在 NSMigrationManager 上调用它时我遇到了严重的崩溃:

                 [manager migrateStoreFromURL:sourceStoreURL
type:type
options:nil
withMappingModel:mappingModel
toDestinationURL:destinationStoreURL
destinationType:type
destinationOptions:nil
error:error];

它是那些无用的崩溃之一,因为它是 EXC_BAD_ACCESS,我无法找出它的确切位置或原因。我知道调试器中的堆栈跟踪看起来像:

    (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

因此,基于第 1 帧,我猜测它正在尝试将 NSNumber 迁移到标量。这可能吗?我想知道是否有人已将旧商店迁移到允许标量的新商店。

最佳答案

它崩溃的原因是因为在映射模型中定义了一些最可能引用自定义代码的东西,而该代码失败了。

如果您有一个不直接的实体映射,我会建议从该实体映射中删除所有属性映射,然后覆盖:

func createDestinationInstances(forSource sInstance: NSManagedObject, in mapping: NSEntityMapping, manager: NSMigrationManager) throws 

您可以在其中准确指定属性应该是什么。它也可能与这些自定义方法之一有关,传回 NSManagedObject 的 SUBCLASS 实例,您在迁移期间不应这样做。

您可以在原始问题的堆栈跟踪中看到,尝试将一个值转换为另一个“coerceValueForKey...”失败

例如:

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))

// my new data model added a to-one association to a Song that wasn't present previously. So we have to create it,
// but not yet associate it. We just provide a means to associate it, via the 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 about this. It's used for serialization.
structure.set(text: text)
songData.setValue(structure.serialize(), forKey: #keyPath(SongData.nsdata))

// my new data model added a to-one association to a Song that wasn't present previously. So we have to create it,
// but not yet associate it. We just provide a means to associate it, via the filename/songFilename
let viewPreferences = NSEntityDescription.insertNewObject(forEntityName: SongViewPreferences.entity().name!, into: manager.destinationContext)
viewPreferences.setValue(sInstance.value(forKey: "filename"), forKey: #keyPath(SongViewPreferences.songFilename))

// important to associate these!
manager.associate(sourceInstance: sInstance, withDestinationInstance: dInstance, for: mapping)
return
}

关于ios - 为什么我的核心数据迁移因 EXC_BAD_ACCESS 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46178853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com