App首次在模拟器上运行良好,但是一旦将新数据保存到Core Data中,该应用将不会再次启动-在第一个视图加载未捕获的异常“ NSInternalInconsistencyException”之前崩溃,原因:“语句仍处于活动状态”。控制台中有75行预崩溃操作,但是没有什么能脱颖而出(对我那些不熟练的人而言)。
如果删除,则可以重复启动该应用程序,直到将新数据保存到Core Data。保存新数据后,简单地停止运行应用程序或退出模拟器无济于事,它在启动过程中仍然崩溃。
Stackoverflow和apple doc.s一直认为它与线程有关,但是我的代码有点简单-一切都在主线程上。我很想找到可以快速尝试或快速找出原因/解决方案的方法。
我在AppDelegate类中使用默认的xcode 7 Core Data堆栈。样本数据最初是通过一种方法加载到Core Data中的,然后再从core data中成功加载。保存新记录后,将从核心数据成功加载(而不是重新启动)新添加的数据。该问题仅在重新启动时发生。
在代码中,当实例化一个新的记录视图控制器时,我在prepareForSegue方法中实例化了具有单独实体的两个托管对象:
if segue.identifier == "newRecord"
{
let controller = (segue.destinationViewController as! NewRecordVC)
let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate // instantiate the delegate methods in AppDelegate
controller.managedContext = appDelegate!.managedObjectContext // create context from delegate methods
let recordEntity = NSEntityDescription.entityForName("RecordData", inManagedObjectContext: controller.managedContext)
let locationEntity = NSEntityDescription.entityForName("Location", inManagedObjectContext: controller.managedContext)
controller.location = Location(entity: locationEntity!, insertIntoManagedObjectContext: controller.managedContext)
controller.record = Record(entity: recordEntity!, insertIntoManagedObjectContext: controller.managedContext)
print("segueing")
}
在新的记录视图控制器中,定义了托管对象属性值,并在展开时保存了托管上下文。
if (segue.identifier == "UnwindSegue")
{
updateRecord() // managed object properties updated
do
{
try managedContext.save() // commit changes / save context
}
catch
{
print("There is some error.") // if error
}
}
当应用返回主视图时,新记录将从Core Data中获取并显示在表上。
但是当我重新启动应用程序时-悲伤。
最佳答案
我了解到,当某些东西被苹果“粘稠”时,最好远离它。
根据doc.s,将子类中的替代init更改为awakeFromInsert可以对其进行修复。谢谢Markus S. Zarra。