使用 CoreData 获取时崩溃



我的应用程序正在生产中,在尝试将 festRequest 的第一个元素投射到某个实体时,我在 Crashlytics 上遇到了一些崩溃。无论我多么努力,我都无法重现这种崩溃。

static func getSettings() -> NotificationSettingsMO {
var settings: NotificationSettingsMO!
let moc = DataController.shared.managedObjectContext
moc.performAndWait {
do {
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "NotificationSettings")
settings = try moc.fetch(fetchRequest).first as! NotificationSettingsMO
} catch {
print(error)
}
}
return settings

尝试将请求的第一个元素强制转换为 NotificationSettingsMO 时会发生崩溃,我确定该实体存在,因为我在创建用户时创建了它。这也只发生在一小部分用户身上,但由于它使应用程序崩溃,我想尝试找出导致它的原因。

编辑:我已经附加了崩溃日志

Crashed: com.apple.main-thread
0  MyApp                        0x100073360 specialized static NotificationSettingsMO.(getSettings() -> NotificationSettingsMO).(closure #1) (NotificationSettingsMO.swift:25)
1  MyApp                        0x10007309c partial apply for static NotificationSettingsMO.(getSettings() -> NotificationSettingsMO).(closure #1) (NotificationSettingsMO.swift)
2  CoreData                       0x18311d08c developerSubmittedBlockToNSManagedObjectContextPerform + 196
3  CoreData                       0x18311cf54 -[NSManagedObjectContext performBlockAndWait:] + 220
4  MyApp                        0x100072fa8 specialized static NotificationSettingsMO.getSettings() -> NotificationSettingsMO (NotificationSettingsMO.swift)
5  MyApp                        0x1000d6190 AppDelegate.getDataOnLaunch() -> () (AppDelegate.swift)
6  MyApp                        0x1000da4bc specialized AppDelegate.application(UIApplication, didFinishLaunchingWithOptions : [UIApplicationLaunchOptionsKey : Any]?) -> Bool (AppDelegate.swift:99)
7  MyApp                        0x1000d3eb8 @objc AppDelegate.application(UIApplication, didFinishLaunchingWithOptions : [UIApplicationLaunchOptionsKey : Any]?) -> Bool (AppDelegate.swift)
8  UIKit                          0x1863f29c0 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 400
9  UIKit                          0x186622184 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2904
10 UIKit                          0x1866265f0 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1684
11 UIKit                          0x186623764 -[UIApplication workspaceDidEndTransaction:] + 168
12 FrontBoardServices             0x182bbf7ac __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36
13 FrontBoardServices             0x182bbf618 -[FBSSerialQueue _performNext] + 168
14 FrontBoardServices             0x182bbf9c8 -[FBSSerialQueue _performNextFromRunLoopSource] + 56
15 CoreFoundation                 0x1811d509c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
16 CoreFoundation                 0x1811d4b30 __CFRunLoopDoSources0 + 540
17 CoreFoundation                 0x1811d2830 __CFRunLoopRun + 724
18 CoreFoundation                 0x1810fcc50 CFRunLoopRunSpecific + 384
19 UIKit                          0x1863eb94c -[UIApplication _run] + 460
20 UIKit                          0x1863e6088 UIApplicationMain + 204
21 MyApp                        0x10001ee18 main (Measurement+Network.swift:26)
22 libdispatch.dylib              0x180c9a8b8 (Missing)

对象不存在,因此崩溃。 就我个人而言,我讨厌快速强制铸造。 可选的全部意义在于帮助您管理何时为零。 在代码中放入as!是要求程序崩溃。 我建议将其删除并替换为if let. 如果您 100% 确定它永远不会为零,那么您仍然应该使用if let并在 else 报告中崩溃服务它发生了(使用CrashlyticsrecordError- 它将与您的崩溃一起显示(。

至于你的说法:

我确定该实体存在,因为我在创建用户时创建了它

说白了——你错了。计算机永远是对的,人类永远是错的。

以下是可能的原因:

将其保存到核心数据时
  • 出错,我怀疑像大多数程序一样,当您将对象保存到核心数据时,您没有检查错误 - 或者如果您这样做了,您真的不知道如何处理错误。 如果你很聪明,你会记录它。 最常见的错误是硬盘空间不足。 如果对象一开始没有保存,那么当您获取它时它就不会在那里。
  • 该对象存在,但已删除。如果代码中有删除此对象的任何位置,请考虑在此代码运行之前删除此对象的可能性。 查找多线程和争用条件。
  • 对象尚未保存。如果应用程序中有任何多线程,则应考虑存在竞争条件的可能性。

这些情况中的任何一种都会导致 0-3% 范围内的崩溃。

最新更新