使用 CoreData 获取时崩溃

Crash when fetching with 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)

该对象不存在,因此崩溃。我个人讨厌在 swift 中强制施法。 optionals 的全部意义在于帮助你在某些东西为 nil 时进行管理。将 as! 放入您的代码中会要求程序崩溃。我建议删除它并用 if let 替换它。如果你 100% 确定它永远不会为零,那么你仍然应该使用 if let 并且在其他情况下向崩溃服务报告它发生了(使用 Crashlytics recordError - 它会显示和你的崩溃一起起来)。

至于你的说法:

I'm sure the entity exists since I create it when the user is created

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

以下是可能的原因:

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

任何这些情况都会导致 0-3% 运行ge 崩溃。