2017-07-18 131 views
1

我的應用程序正在生產中,並且在嘗試將festRequest的第一個元素轉換爲某個實體時,Crashlytics發生了一些崩潰。無論我多努力嘗試,我都無法重新創建這個崩潰。使用CoreData獲取時發生崩潰

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) 
+0

你可以發佈崩潰日誌嗎?堆棧跟蹤一定會有助於確定究竟發生了什麼。 –

+0

考慮如果您的獲取請求返回0結果會發生什麼。 – Jonah

+0

「我已經在Crashlytics上發生了幾次崩潰」 您可以附上崩潰報告嗎? –

回答

1

對象不存在,因此崩潰。我個人很恨HATE強制鑄造。可選項的全部要點是幫助您管理什麼時候什麼是零。將as!放入代碼中會要求程序崩潰。我建議刪除它並用if let替換它。如果你100%肯定它永遠不會是零,那麼你仍然應該使用if let,並在其他報告崩潰服務,它發生了(使用CrashlyticsrecordError - 它會顯示隨着你的崩潰)。

至於你的語句:

我敢肯定,實體存在,因爲我創造它時創建的用戶

說穿了 - 你錯了。電腦總是正確的,人類總是錯誤的。

以下是可能的原因:

  • 時出錯保存到核心數據我懷疑,最喜歡的程序時,保存對象,以核心數據你沒有檢查錯誤 - 或如果你真的這樣做,你不知道該怎麼處理這個錯誤。如果你很聰明,你會記錄它。最常見的錯誤是硬盤空間不足。如果對象沒有保存在第一個地方,那麼當你取回它時它不會在那裏。
  • 對象在那裏,但被刪除。如果您的代碼中有任何地方刪除了此對象,那麼請考慮它在此代碼運行之前被刪除的可能性。尋找多線程和競爭條件。
  • 該對象尚未保存。如果您在應用程序中有任何多線程,您應該考慮存在競爭條件的可能性。

任何這些情況都會導致0-3%範圍內的崩潰。

+0

即使我仍然不確定它爲什麼會發生,我要將此答案標記爲正確。我正在做的是保存在後臺上下文中,然後將該上下文合併到主要上下文中。合併發生在主要上下文的performAndWait塊中,並且提取也發生在類似的塊中。我相信CoreData存在錯誤,我無法複製它。 –

相關問題