Pra Kash
Pra Kash

Reputation: 33

Crashing when trying saveContext for core data in dispatch_async main queue

I am saving context in main queue after deleting a object from core data. So I am doing check for main thread or not and if not in main thread doing it in dispatch_async. Using Concurrecny type to created NSManagedObjectContext is ConfinementConcurrencyType

Code using to Save context:

public static func saveContext(managedContext : NSManagedObjectContext){
    AppDelegate.getAppDelegate().log.debug("saveContext()")

    if NSThread.isMainThread() == true{
      do {

        try managedContext.save()
      }catch let error as NSError{
        AppDelegate.getAppDelegate().log.error("Could not save \(error), \(error.userInfo)")
      }
    }else{
      dispatch_async(dispatch_get_main_queue()) { () -> Void in
        do {

          try managedContext.save()
        }catch let error as NSError{
          AppDelegate.getAppDelegate().log.error("Could not save \(error), \(error.userInfo)")
        }
      }
    }
  }

Crash report from Crashlytics:

Crashed: com.apple.root.user-initiated-qos
0  Quickride                      0x1002609d0 static NotificationPersistenceHelper.deleteNotificationsOfAGroup(String, groupValue : String) -> () (NotificationPersistenceHelper.swift:162)
1  Quickride                      0x10019df64 specialized NotificationStore.removeOldNotificationOfSameGroupValue(String, groupValue : String) -> () (NotificationStore.swift:286)
2  Quickride                      0x10019a46c NotificationStore.saveNewNotification(UserNotification) -> UserNotification (NotificationStore.swift)
3  Quickride                      0x1001cb200 specialized NotificationHandler.saveNotification(UserNotification) -> UserNotification? (NotificationHandler.swift:58)
4  Quickride                      0x1001c7fe4 NotificationHandler.saveNotification(UserNotification) -> UserNotification? (NotificationHandler.swift:40)
5  Quickride                      0x1001c7e0c NotificationHandler.handleNewUserNotification(UserNotification) -> () (NotificationHandler.swift:29)
6  Quickride                      0x1003dfbb8 specialized UserNotificationTopicListener.onMessageRecieved(String?, messageObject : AnyObject?) -> () (UserNotificationTopicListener.swift:24)
7  Quickride                      0x1003df8e0 UserNotificationTopicListener.onMessageRecieved(String?, messageObject : AnyObject?) -> () (UserNotificationTopicListener.swift:18)
8  Quickride                      0x1003fd918 specialized UserMessageTopicListener.onMessageRecieved(String?, messageObject : AnyObject?) -> () (UserMessageTopicListener.swift:19)
9  Quickride                      0x1003fd104 UserMessageTopicListener.onMessageRecieved(String?, messageObject : AnyObject?) -> () (UserMessageTopicListener.swift:17)
10 Quickride                      0x10011f8ac static MessageDispatchUtils.dispatchMessageToListeners(String, mqttMessage : MQTTMessage, listenerList : [TopicListener]) -> () (MessageDispatchUtils.swift)
11 Quickride                      0x100181700 EventServiceProxy.(dispatchAllMessagesArrivedBeforeSessionWasInitialized() -> ()).(closure #1) (EventServiceProxy.swift)
12 libdispatch.dylib              0x192e31200 _dispatch_call_block_and_release + 24
13 libdispatch.dylib              0x192e311c0 _dispatch_client_callout + 16
14 libdispatch.dylib              0x192e41558 _dispatch_root_queue_drain + 1032
15 libdispatch.dylib              0x192e410ec _dispatch_worker_thread3 + 124
16 libsystem_pthread.dylib        0x1930392c8 _pthread_wqthread + 1288
17 libsystem_pthread.dylib        0x193038db4 start_wqthread + 4

Upvotes: 0

Views: 1318

Answers (1)

Mundi
Mundi

Reputation: 80273

You should not use the system's threading APIs but Core Data's own.

context.performBlockAndWait() {  // or performBloc() to return immediately
    do { try context.save() } catch {}
}

Upvotes: 1

Related Questions