Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:36:36+00:00 2026-06-11T10:36:36+00:00

I have a background thread doing some processing, and saving to Core Data. In

  • 0

I have a background thread doing some processing, and saving to Core Data. In my app delegate’s applicationShouldTerminate, I wait on a semaphore which is released when the background thread completes its work. This is to avoid killing the thread in the middle of its work and leaving things in an inconsistent state.

Unfortunately, this causes a deadlock. Here is how the background job operates:

_context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_context setParentContext:_parentContext];

[_context performBlock:
^{
    // ... long-running task here ...

    NSError * error;
    [_context save:&error]; // deadlock here if main thread is waiting on semaphore

    // ... release semaphore here ...
}];

If the user quits the app while the background thread is still working, it deadlocks. The problem seems to be that [_context save:&error] is calling dispatch_sync (or an equivalent) into the main thread – but the main thread is already waiting for this thread to release the semaphore, and so isn’t able to run the block.

Since a child context save appears to block on the main thread, how can this pattern (main thread waiting for child to complete and save its context) be achieved?

Main thread:

#0  0x00007fff882e96c2 in semaphore_wait_trap ()
#1  0x00007fff876264c2 in _dispatch_semaphore_wait_slow ()
#2  0x00000001001157fb in +[IndxCore waitForBackgroundJobs] at /Users/mspong/dev/Indx/IndxCore/IndxCore/IndxCore.m:48
#3  0x00000001000040c6 in -[RHAppDelegate applicationShouldTerminate:] at /Users/mspong/dev/Indx/Indx/Indx/RHAppDelegate.m:324
#4  0x00007fff9071a48f in -[NSApplication _docController:shouldTerminate:] ()
#5  0x00007fff9071a39e in __91-[NSDocumentController(NSInternal) _closeAllDocumentsWithDelegate:shouldTerminateSelector:]_block_invoke_0 ()
#6  0x00007fff9071a23a in -[NSDocumentController(NSInternal) _closeAllDocumentsWithDelegate:shouldTerminateSelector:] ()
(snip)
#17 0x00007fff9048e656 in NSApplicationMain ()
#18 0x0000000100001e72 in main at /Users/mspong/dev/Indx/Indx/Indx/main.m:13
#19 0x00007fff8c4577e1 in start ()

Background thread:

#0  0x00007fff882e96c2 in semaphore_wait_trap ()
#1  0x00007fff87627c6e in _dispatch_thread_semaphore_wait ()
#2  0x00007fff87627ace in _dispatch_barrier_sync_f_slow ()
#3  0x00007fff8704a78c in _perform ()
#4  0x00007fff8704a5d2 in -[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:] ()
#5  0x00007fff8702c278 in -[NSManagedObjectContext save:] ()
#6  0x000000010011640d in __22-[Indexer updateIndex]_block_invoke_0 at /Users/mspong/dev/Indx/IndxCore/IndxCore/Indexer/Indexer.m:70
#7  0x00007fff87079b4f in developerSubmittedBlockToNSManagedObjectContextPerform_privateasync ()
#8  0x00007fff876230fa in _dispatch_client_callout ()
#9  0x00007fff876244c3 in _dispatch_queue_drain ()
#10 0x00007fff87624335 in _dispatch_queue_invoke ()
#11 0x00007fff87624207 in _dispatch_worker_thread2 ()
#12 0x00007fff88730ceb in _pthread_wqthread ()
#13 0x00007fff8871b1b1 in start_wqthread ()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:36:37+00:00Added an answer on June 11, 2026 at 10:36 am

    In applicationShouldTerminate: you can return NSTerminateLater to delay the termination until the background context has finished the save operation.

    When the saving is done, you call

    [[NSApplication sharedApplication] replyToApplicationShouldTerminate:YES];
    

    to quit the application.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In an android service I have created thread(s) for doing some background task. I
I have a background thread doing some work and a UI displaying the progress,
If I have a background thread doing some network stuff, and some of the
I have a background thread and the thread calls some methods that update the
If I have a background thread and having Join to wait for completing background
I have a background worker thread that is constantly syncing data to/from a remote
Hi all i have a background worker thread and some unmanaged code dlls ,
I have about 4 threads. One thread keeps checking some data that the other
I call obackgroundworker.CancelAsync(); on a background worker currently doing some work in another thread
All, I have a Task that runs on a background thread and does some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.