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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:28:42+00:00 2026-06-03T13:28:42+00:00

My application uses multiple threads with one managed object context per thread. For clarity

  • 0

My application uses multiple threads with one managed object context per thread.

For clarity I will refer to the different managed object contexts as: moc1, moc2, …, etc.

Let’s assume we have two models with a simple one-many relationship:

User 1----* Document

When a user logs in I fetch the corresponding model from one of the contexts (eg. moc1).

(pseudo code)

UserModel *globalLoggedUser = ( Fetch the logged in user using moc1 )

I then store this user so that I can reference it later.

In another part of the application I need to loop through thousands of items from an array and create Document objects for it. Each document then needs to be bound to the current user. This happens in a different background thread (which has its own context)

for( NSString *documentName in documents) {
    ( Create new document using moc2 )

    ** THIS IS WHERE MY PROBLEM IS **
    // What I currently do:
    UserModel *tempUser = ( Fetch the logged in user using moc2 )
    ( bind new document to tempUser )

    // What I would like to do: 
    ( bind new document to globalLoggedUser )

    // Note that in this case tempUser and globalLoggedUser are the same user, except they are attached to different contexts.

}

As you can see, I would like to avoid having to fetch a new user into the current context each time.

The problem is, globalLoggedUser is part of moc1, whereas the new document is part of moc2 (or moc3, moc4, etc, depends on the thread).

So what’s the best way to go about this? How can I globally save/cache an object and then use that same object to bind relationships in different contexts without incurring a penalty of having to fetch each time?

Thanks for any help you can provide.

  • 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-03T13:28:44+00:00Added an answer on June 3, 2026 at 1:28 pm

    You are correct that you can’t use the same NSManagedObject across threads.

    From the Core Data Programming Guide:

    Using thread confinement, you should not pass managed objects or managed object contexts between threads. To “pass” a managed object from one context another across thread boundaries, you either:

    • Pass its object ID (objectID) and use objectWithID: or existingObjectWithID:error: on receiving managed object context.

      The corresponding managed objects must have been saved—you cannot pass the ID of a newly-inserted managed object to another context.

    • Execute a fetch on the receiving context.

    I think you’d be fine if you just fetched the logged in user with moc2 before you run the ‘document’ loop, as I don’t see any reason to do the fetch each time inside the loop. (Is there some reason you are doing that?)

    Don’t worry about binding anything to the UserModel from thread 1, the tempUser you get from moc2 is referencing the same data in the database as globalLoggedUser.

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

Sidebar

Related Questions

I have a application which uses .net Thread-pool to have multiple threads.It uses log4net
I'm working on an application that uses multiple threads to process its data. The
My application uses multiple windows I want to hide one specific window in case
I am working on all application that uses multiple threads that access a sql
I have a Java JDBC application that uses multiple threads to retrieve information from
I'm developing an application that uses multiple threads to do some intensive calculations as
I have an unmanaged Win32 C++ application that uses multiple C++ DLLs. The DLLs
I am developing a multi threaded application, that uses both threads and Async tasks
First the problem: I'm working on the application that uses multiple FragmentLists within a
I have a Silverlight 4 application that uses multiple tabs. On Tab 1 I

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.