Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么我的核心数据迁移会因EXC\u访问错误而崩溃_Ios_Objective C_Core Data_Migration - Fatal编程技术网

Ios 为什么我的核心数据迁移会因EXC\u访问错误而崩溃

Ios 为什么我的核心数据迁移会因EXC\u访问错误而崩溃,ios,objective-c,core-data,migration,Ios,Objective C,Core Data,Migration,您还可以从以下方面获得一些好处: 我正在尝试将旧的数据存储迁移到新的对象模型,因此轻量级数据迁移不起作用。我使用的是一个映射模型,然后是一些自定义的NSEntityMigrationPolicy子类,这些子类主要用于定义助手方法,然后我可以从映射模型调用这些方法 我一直在边做边学,迁移过程中的每一步都会失败,然后我会修复它,然后再进一步,然后我会陷入困境 因此,我似乎已经通过了验证步骤,模型将保存,但现在在NSMigrationManager上调用此命令时,我遇到了严重的崩溃:

您还可以从以下方面获得一些好处:

我正在尝试将旧的数据存储迁移到新的对象模型,因此轻量级数据迁移不起作用。我使用的是一个映射模型,然后是一些自定义的
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
子类的实例,这在迁移过程中是不应该做的

您可以在原始问题的堆栈跟踪中看到,尝试将一个值转换为另一个值时失败了“强制ValueForkey…”

例如:

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
}

它崩溃的原因是在映射模型中定义了引用最有可能的自定义代码的内容,而该代码失败了


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

func createDestinationInstances(forSource sInstance: NSManagedObject, in mapping: NSEntityMapping, manager: NSMigrationManager) throws 
在这里,您可以精确地指定属性应该是什么。这可能还与以下自定义方法之一有关:传回
NSManagedObject
子类的实例,这在迁移过程中是不应该做的

您可以在原始问题的堆栈跟踪中看到,尝试将一个值转换为另一个值时失败了“强制ValueForkey…”

例如:

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
}

问题应该是独立的。如果此问题需要其他详细信息,请在此问题中发布这些详细信息。不要把这些细节贴到另一个问题的链接上。我没有。如果有人在做类似的事情,而这是一个更大的任务中两个相互排斥的部分,我会添加它。问题应该独立解决。如果此问题需要其他详细信息,请在此问题中发布这些详细信息。不要把这些细节贴到另一个问题的链接上。我没有。我添加了它,以防有人在做类似的事情,而这是一个更大任务的两个相互排斥的部分。“我建议从实体映射中删除所有属性映射”--谢谢!我认为自定义映射会覆盖属性映射。我碰巧尝试列出的所有属性映射,即使实体设置为自定义映射。“我建议从该实体映射中删除所有属性映射”--谢谢!我认为自定义映射会覆盖属性映射。碰巧它尝试列出的所有属性映射,即使实体设置为自定义映射。