Swift:在applicationWillTerminate后获取persistentStoreCoordinator时核心数据崩溃

问题描述:

我们已将应用更新为新版本。Swift:在applicationWillTerminate后获取persistentStoreCoordinator时核心数据崩溃

有一个新功能添加到新版本,我们为该功能添加了一个新的coredata表。

应用程序发布后,错误继续,没有答案。 会有人遇到相同的错误或相同的问题,并解决了这个错误?

Thread 0 Crashed: 
0 libsystem_kernel.dylib    0x000000018981f014 __pthread_kill + 8 
1 libsystem_c.dylib     0x0000000189793400 abort + 140 
2 [AppName]       0x00000001002d0c58 AppDelegate.(persistentStoreCoordinator.getter).(closure #1) (AppDelegate.swift:318) 
3 [AppName]       0x00000001002c7dec AppDelegate.persistentStoreCoordinator.getter (AppDelegate.swift:334) 
4 [AppName]       0x00000001002d0cbc AppDelegate.(managedObjectContext.getter).(closure #1) (AppDelegate.swift:338) 
5 [AppName]       0x00000001002c8054 AppDelegate.managedObjectContext.getter (AppDelegate.swift:342) 
6 [AppName]       0x00000001002c8198 AppDelegate.saveContext() (AppDelegate.swift:347) 
7 [AppName]       0x00000001002c7780 AppDelegate.applicationWillTerminate() (AppDelegate.swift:295) 
8 [AppName]       0x00000001002c77d8 @objc AppDelegate.applicationWillTerminate() (AppDelegate.swift:0) 
9 UIKit        0x0000000190792704 <redacted> + 244 
10 UIKit        0x00000001909947cc <redacted> + 792 
11 UIKit        0x0000000190997fdc <redacted> + 292 
12 UIKit        0x0000000190989d50 <redacted> + 560 
13 UIKit        0x00000001906f90b4 <redacted> + 168 
14 CoreFoundation      0x000000018a7fe0c0 <redacted> + 32 
15 CoreFoundation      0x000000018a7fbcf0 <redacted> + 372 
16 CoreFoundation      0x000000018a7fc180 <redacted> + 1024 
17 CoreFoundation      0x000000018a72a2b8 CFRunLoopRunSpecific + 444 
18 GraphicsServices     0x000000018c1de198 GSEventRunModal + 180 
19 UIKit        0x00000001907717fc <redacted> + 684 
20 UIKit        0x000000019076c534 UIApplicationMain + 208 
21 [AppName]       0x00000001002d1a94 main (AppDelegate.swift:17) 
22 ???         0x000000018970d5b8 0x0 + 0 
+0

在Core Data中添加新字段,实体会使您以前的Core数据模型无效,从而导致崩溃。如果你删除应用程序并重新安装它应该工作。 –

+0

该堆栈跟踪显示应用程序在尝试保存更改时崩溃。没有其他有用的信息。这是您尝试保存更改的唯一时间吗?如果你在其他时间保存,会发生什么?除了堆栈跟踪之外,还有哪些错误消息? –

首先,您应该发布恶意代码,例如, AppDelegate中的getters(行号参见堆栈跟踪)。

一般来说,等到应用程序终止时可能需要很长时间(因为iOS会让iOS 4保持活跃状态​​,我认为)。你可以将逻辑转换为applicationDidEnterBackground,但我认为这也是容易出错的,因为如果应用程序崩溃,就不会调用它。

您应该尽可能经常保存上下文(但不是更常见:-),例如:每次视图控制器被解雇或者等等。