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

  • Home
  • SEARCH
  • 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 6684183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:54:36+00:00 2026-05-26T04:54:36+00:00

Part of my iOS project polls a server for sets of objects, then converts

  • 0

Part of my iOS project polls a server for sets of objects, then converts and saves them to Core Data, to then update the UI with the results. The server tasks happens in a collection of NSOperation classes I call ‘services’ that operate in the background. If NSManagedObject and its ~Context were thread safe, I would have had the services call delegate methods on the main thread like this one:

- (void)service:(NSOperation *)service retrievedObjects:(NSArray *)objects;

Of course you can’t pass around NSManagedObjects like this, so this delegate method is doomed. As far as I can see there are two solutions to get to the objects from the main thread. But I like neither of them, so I was hoping the great StackOverflow community could help me come up with a third.

  1. I could perform an NSFetchRequest on the main thread to pull in the newly added or modified objects. The problem is that the Core Data store contains many more of these objects, so I have to add quite some verbosity to communicate the right set of objects. One way would be to add a property to the object like batchID, which I could then pass back to the delegate so it would know what to fetch. But adding data to the store to fix my concurrency limitations feels wrong.

  2. I could also collect the newly added objects’ objectID properties, put them in a list and send that list to the delegate method. The unfortunate thing though is that I have to populate the list after I save the context, which means I have to loop over the objects twice in the background before I have the correct list (first time is when parsing the server response). Then I still only have a list of objectIDs, which I have to individually reel in with existingObjectWithID:error: from the NSManagedObjectContext on the main thread. This just seems so cumbersome.

What piece of information am I missing? What’s the third solution to bring a set of NSManagedObjects from a background thread to the main thread, without losing thread confinement?

  • 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-26T04:54:36+00:00Added an answer on May 26, 2026 at 4:54 am

    epologee,

    While you obviously have a solution you are happy with, let me suggest that you lose some valuable information, whether items are updated, deleted or inserted, with your mechanism. In my code, I just migrate the userInfo dictionary to the new MOC. Here is a general purpose routine to do so:

    // Migrate a userInfo dictionary as defined by NSManagedObjectContextDidSaveNotification
    // to the receiver context.
    - (NSDictionary *) migrateUserInfo: (NSDictionary *) userInfo {
    
        NSMutableDictionary *ui = [NSMutableDictionary dictionaryWithCapacity: userInfo.count];
    
        NSSet        *  sourceSet = nil;
        NSMutableSet *migratedSet = nil;
    
        for (NSString *key in [userInfo allKeys]) {
    
            sourceSet   = [userInfo valueForKey: key];
            migratedSet = [NSMutableSet setWithCapacity: sourceSet.count];
    
            for (NSManagedObject *mo in sourceSet) {
    
                [migratedSet addObject: [self.moc objectWithID: mo.objectID]];
            }
            [ui setValue: migratedSet forKey: key];
        }
        return ui;
    
    } // -migrateUserInfo:
    

    The above routine assumes it is a method of a class which has an @property NSManagedObjectContext *moc.

    I hope you find the above useful.

    Andrew

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

Sidebar

Related Questions

I have an iOS project which has static data (around 500 entries) This static
Having created my first iOS app as part of my MSc project I now
I'm running into a confusing part of IOS App distribution. So I need a
I'm developing the backend part of a social app. Clients are iOS/Android phones. The
I'm moving my first stpes in iOS development. I have followed the first part
Part of my web application is a background script that polls from a beanstalkd
I'm trying to produce gcd a files from an iOS Xcode 4.2 (4D199) project
I was looking through a Storyboard tutorial here: http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1 When I noticed that the
This is for an iOS project. I am reworking a section of my dataController
i'm currently working on an ios project with some people, one of us decided

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.