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 9114977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:19:21+00:00 2026-06-17T04:19:21+00:00

For some reason using dispatch_async and Core Data together causes my app to completely

  • 0

For some reason using dispatch_async and Core Data together causes my app to completely freeze, but not crashed.

Symptom

The user interface is not responsive. The app does not crash.

The app uses [[UIAccelermeter sharedAccelerometer] setDelegate: self] to get accelerometer values continuously. The “accelerometer:didAccelerate:” callback runs on main thread. When this issue occurs, the callback no longer executes.

Therefore the main thread appears to be “frozen”.

Context

I use NSURLConnection in my app to send HTTP requests to my server. In connectionDidFinishLoading, where response data is handled, it takes about 2 seconds to execute and the user interface would be unresponsive during those 2 seconds, because it runs in the main thread.

After some research, it seems that gcd/dispatch_async is the best solution for this situation to execute the data handling in the background. So, in connectionDidFinishLoading, I added the following gcd calls:

dispatch_queue_t queue;
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    [self handleTheData: connection];
});

This works fine sometimes, but it would cause app to freeze randomly – Even though same code is used, with the same data in Core Data storage, repeated tests show that symptom can be observed X% of the time. The chance of issue happening is affected by a few factors, explained further below.

What does handleTheData() do?

It examines the response data from NSURLConnection, which is JSON formatted string. The string is 3 lists of entity, and each entity is an array of array of numbers. So there are codes to convert the JSON formatted string into 3 NSMutableArray of custom object, where each object is a subclassed NSManagedObject.

During the convertion, since these ojects are “temporary” in that they may or may not get stored permanently, they are created in “nil” NSManagedObjectContext, like so:

SampleEntity *newEntity= 
(SampleEntity *) [[NSManagedObject alloc] initWithEntity:entity 
                       insertIntoManagedObjectContext:nil
               ];

This way, they will not be mixed with the main context.

Then these new objects are compared with existing entities in data store. So there are some codes to fetch from data store repeatedly to see if a particular new entity already existed, and depending on the data of the new entity, the new entity may be saved to data store, or the existing entity may be updated.

[managedObjectContext save: &error] is called often to ensure that changes are saved permanently.

Anything else running in background?

As mentioned in Symptom section, Accelerometer runs in the background in the main thread using [[UIAccelermeter sharedAccelerometer] setDelegate: self]

AudioUnits have also been set up to run in the background (on a separate “priority” thread according to Apple docs) to produce sound.

Findings on cause of issue

It was found that the chance of issue happening is affected by the following factors:

  • If some debugging code is commented out, such as NSLog(@”fetchedResult: %@”, fetchedResult), the chance of issue happening decreases (but it still occurs).
  • If accelerometer is not used, the issue almost never occur (but not 100% sure as I did a limited number of tests).
  • If accelerometer is used, the chance of issue happening is about 50/50.
  • If some of the Core Data codes such as [sampleEntity setXYZ..] is commented out, the issue almost never occurs even though accelerometer is used.

Second Issue

Also, even if the main thread is not frozen, ANOTHER ISSUE occurs. The data handling code would end half way through without throwing errors.

  • What that means is, data handling will only be partially done and there is no error thrown, and the main thread continues to run fine.
  • This second issue occurs frequently (but not always) whenever the main issue does not occur.

Interesting problem isn’t it? 🙂

Edit

  • 3/1/2013 – I previously thought that the cause of issue was iOS 6. Sorry, that was completely wrong. Modified description to reflect new findings.
  • 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-17T04:19:22+00:00Added an answer on June 17, 2026 at 4:19 am

    I ended up lodging a Technical Support Incident with Apple development support. I supplied them with a sample project as well, which shows the same symptom.

    After a few days, I got a reply from them which pointed me to CoreData Programming Guide, and pointed out the fact that codes ran by dispatch queues need to have their own private NSManagedObjectContext.

    https://developer.apple.com/documentation/coredata/using_core_data_in_the_background

    Once I followed the advice, issue seems to be resolved!

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

Sidebar

Related Questions

For some reason that I don't understand, video websites do not play when using
I am trying to validate email using javascript but for some reason it is
I have just tried using the NSArchiver but for some reason I am getting
For some reason OpenCover is not covering tests using moles on Windows Server 2003
I'm trying to parse data from yahoo finance using JSON. For some reason the
I'm using categories with Core Data. Some of the tutorials I've read and lectures
For some reason I'm using MySQL locally, and have Postgres on my heroku app.
I just installed FuzzyFinder for gvim, though for some reason it's using an ultra
For some reason when I generate this using jquery to fill in my table.
For some reason, Vim keeps using soft-tabs (tabs as spaces) everytime I'm working on

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.