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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:22:05+00:00 2026-06-01T01:22:05+00:00

i am stuck with my first GCD and first core-data using application =) two

  • 0

i am stuck with my first GCD and first core-data using application =)

two views access the same data (which is handled by a single DAO).

if i wait for the current view to finish loading its content no problem occors when changing view.

however: if i change the view (its tabbased) while one controller tries to fetch data from my model, the new controller tries the same and the threads ‘collide’ and my application freezes.

the freeze occurs in this line of code of my DAO:

    NSArray *results = [managedObjectContext executeFetchRequest:fetch error:&error];    

reloadAllMonth() accesses the fetch routine of my DAO

how i load the data in the first controller:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
                [self reloadAllMonth];

                dispatch_async(dispatch_get_main_queue(), ^(void) {                        
                    [self.allMonthTable reloadData];    
                });

in the second viewcontroller the first thing i do is update my DAO, this of course uses (beneath others) the very same fetch routine i called before:

    [self.dataHandler updateData];

i have tried two approaches so far:

first using a c-semaphore:

-(NSArray *)fetchAllMonthExpenses{  
    //@return: array of all expenses in month (day & month type)


    NSNumber *monthNumber = [self getMonthNumber:[NSDate date]];
        NSEntityDescription *exp = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
    [fetch setEntity:exp];
    [fetch setPredicate:[NSPredicate predicateWithFormat:@"month == %@",monthNumber]];
    NSError *error = nil;
    sem_wait(&isLoading);
    NSArray *results = [self.managedObjectContext executeFetchRequest:fetch error:&error];  
    sem_post(&isLoading);
    return results;

}

second using the synchronized directive

-(NSArray *)fetchAllMonthExpenses{  
    //@return: array of all expenses in month (day & month type)


    NSNumber *monthNumber = [self getMonthNumber:[NSDate date]];
        NSEntityDescription *exp = [NSEntityDescription entityForName:@"Expense" inManagedObjectContext:managedObjectContext];
    NSFetchRequest *fetch = [[NSFetchRequest alloc]init];
    [fetch setEntity:exp];
    [fetch setPredicate:[NSPredicate predicateWithFormat:@"month == %@",monthNumber]];
    NSError *error = nil;
    @synchronized(self.class){
        NSArray *results = [self.managedObjectContext executeFetchRequest:fetch error:&error];  
        return results;
    }    
}

sadly both of the approaches did not work, the application freezes whatever i do.

so my question is: what am i doing wrong (as i mentioned first time using threads), what am i missing, where should i look?

this has been keeping me busy for 2 days now and i cant seem to wrap my head around it :/

  • 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-01T01:22:06+00:00Added an answer on June 1, 2026 at 1:22 am

    An NSManagedObjectContext and all of the NSManagedObjects inside it are not thread safe.

    Whatever thread you use to create the context, that needs to be the only thread where you do anything relating to that context. Even just reading values from one of the managed object must be done on that thread and not on any other thread.

    If you need two threads which both deal with the same database, you’ve got two options:

    • use dispatch_sync() to jump into the other thread momentarily to perform all read/write operations on the managed objects and/or the context

    Or:

    • create a second NSManagedObjectContext in the other thread for the same database, and keep any changes made to the two contexts in sync.

    The first option is much easier, but may remove much of the benefits of threading. The second option is harder, but it can be done, and there is a fairly good API for keeping two contexts on different threads in sync.

    Lookup the Core Data Programming Guide for more details.

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

Sidebar

Related Questions

I am making my first parallel application, but I am stuck with basics of
I'm trying to implement my first Facebook application, and I'm stuck on the very
I'm getting a little stuck with two entwined problems. First, I want to have
I'm digging into Reflection for the first time and I'm truely stuck. I've googled
i am stuck at scope resolution in python. let me explain a code first:
http://removed.com/jquery/# First time playing with jQuery, and kind of stuck already. I'm expecting the
I seem to be mentally stuck in a Flyweight pattern dilemma. First, let's say
Stuck on the first stage of a big project. I am trying to display
I am bit stuck with this one.. what i want is update app_name (first
For the first time, I began learning Javascript, however on the start I stuck

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.