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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:47:25+00:00 2026-05-27T14:47:25+00:00

I need some help in using objects from Core Data with GCD; I seem

  • 0

I need some help in using objects from Core Data with GCD; I seem to get NSManagedObjects that are aren’t faulted into the main thread, even when I access their properties. Would appreciate some help.

This is what I’m doing: on launch, I need to load a list of Persons from the Core Data DB, do some custom processing in the background, then reload the table to show the names. I am following the guidelines for Core Data multi-threading by only passing in the objectIDs into the GCD queues. But when I reload the tableview on the main thread, I never see the name (or other properties) displayed for the contacts, and on closer inspection, the NSManagedObjects turn out to be faults on the main thread, even though I access various properties in cellForRowAtIndexPath. The name property is visible in the background thread when I NSLog it; and it’s also showing correctly on the main thread in NSLogs in cellForRowAtIndexPath. But they don’t show in the tableView no matter what I do. I tried accessing the name property using the dot notation, as well as valueForKey, but neither worked.

Here’s my code …. it’s called from the FRC initializer:

- (NSFetchedResultsController *)fetchedResultsController 
{
    if (__fetchedResultsController != nil) 
    {
        return __fetchedResultsController;
    }

    __fetchedResultsController = [self newFetchedResultsControllerWithSearch:nil]; // creates a new FRC

    [self filterAllContactsIntoDictionary: __fetchedResultsController];
    return [[__fetchedResultsController retain] autorelease];
}  


- (void) filterAllContactsIntoDictionary: (NSFetchedResultsController *) frc
{

    NSArray *fetchedIDs = [[frc fetchedObjects] valueForKey:@"objectID"];
    NSArray *fetched = [frc fetchedObjects];

    if (filterMainQueue == nil) {
        filterMainQueue = dispatch_queue_create("com.queue.FilterMainQueue", NULL);
    }
    dispatch_async(self.filterMainQueue, ^{

        NSManagedObjectContext *backgroundContext = [[[NSManagedObjectContext alloc] init] autorelease];
        [backgroundContext setPersistentStoreCoordinator:[[self.fetchedResultsController managedObjectContext] persistentStoreCoordinator]];
        NSMutableArray *backgroundObjects = [[NSMutableArray alloc] initWithCapacity: fetchedIDs.count];

        // load the NSManagedObjects in this background context
        for (NSManagedObjectID *personID in fetchedIDs)
        {
            Person *personInContext = (Person *) [backgroundContext objectWithID: personID];
            [backgroundObjects addObject:personInContext];
        }
        [self internal_filterFetchedContacts: backgroundObjects]; // loads contacts into custom buckets

        // done loading contacts into character buckets ... reload tableview on main thread before moving on
        dispatch_async(dispatch_get_main_queue(), ^{

            CGPoint savedOffset = [self.tableView contentOffset];
            [self.tableView reloadData];
            [self.tableView setContentOffset:savedOffset];

        });
    });
}

What am I doing wrong here? Is there any other way to explicitly make the Person objects fire their faults on the main thread? Or am I doing something wrong with GCD queues and Core Data that I’m not aware of?
Thanks.

  • 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-05-27T14:47:26+00:00Added an answer on May 27, 2026 at 2:47 pm

    Why not take the easy route, since you are not saving anything new ?
    Instead of creating an extra context for the background thread and working with IDs, use the main managedObjectContext in the background thread after locking it.

    for example:

    - (void) filterAllContactsIntoDictionary: (NSFetchedResultsController *) frc
    {
    
        if (filterMainQueue == nil) {
            filterMainQueue = dispatch_queue_create("com.queue.FilterMainQueue", NULL);
        }
        dispatch_async(self.filterMainQueue, ^{
    
            NSManagedObjectContext *context = ... // get the main context.
            [context lock];     // lock the context.
    
            // do something with the context as if it were on the main thread.
    
            [context unlock];   // unlock the context.
    
            dispatch_async(dispatch_get_main_queue(), ^{
               CGPoint savedOffset = [self.tableView contentOffset];
               [self.tableView reloadData];
               [self.tableView setContentOffset:savedOffset];
           });
       });
    }
    

    This works for me when I call a method with performSelectorInBackground, so I guess it should work for GCD dispatch too.

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

Sidebar

Related Questions

I need some help on compiler flags in c++. I'm using a library that
I need some help using regex in java. Im trying to achieve 2 different
I need some help with overlaying views using the prism framework.Its a little more
I need some help extracting the following bits of information using regular expressions. Here
Need some help from javascript gurus. I have one page where http://www.google.com/finance/converter is embedded
Hi I need to pass some objects to and from .Net and a Flex
I have a set of DAOs ( Data access objects ) that need to
Below is some code that I'm using to create objects with Visual Basic: For
Need some help about with Memcache. I have created a class and want to
Need some help, please. I have a line of horizontal thumbnails loaded as ONE

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.