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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:48:57+00:00 2026-06-15T18:48:57+00:00

We have a background thread that needs to do some fetching.. but it doesnt

  • 0

We have a background thread that needs to do some fetching.. but it doesnt need any data — only the objectIDs

originally we did this using a specific newly created blank managed context just for this.

NSFetchRequest *request = [DKDocumentDetails requestAllWithPredicate:predicate inContext:ctx];
[request setResultType:NSManagedObjectIDResultType];
self.objectIDs = [DKDocumentDetails executeFetchRequest:request inContext:ctx];
...

but recently I found out, I can also do this on the PST itself, without any context AS I dont want Managed Objects, but only IDs

NSFetchRequest *request = [DKDocumentDetails requestAllWithPredicate:predicate inContext:mainctx /*used in the wrong thread but only for getting entity description*/];
[request setResultType:NSManagedObjectIDResultType];

NSError *error = nil;
self.objectIDs = [pst executeRequest:request inContext:nil error:&error];
...

so in my tests it never crashed and in the docs I dont see why it shouldnt work either… I mean I dont get unsaved stuff and I cannot get objects, but used this way…

It is faster and looks elegant but is it safe or not?

  • 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-15T18:48:58+00:00Added an answer on June 15, 2026 at 6:48 pm

    I’ve been thinking about your question all day. Here is what I’ve come up with. As others have pointed out, NSPersistentStoreCoordinator objects are not thread safe. When a bunch of NSManagedObjectContext objects on various threads use the same NSPersistentStoreCoordinator, they do so by locking and unlocking the NSPersistentStoreCoordinator.

    However, you are worried about just reading data, and thread safe NSManagedObjectID data at that. Is that ok?

    Well, the Apple documentation On Concurrency with Core Data mentions something similar to what you are doing:

    For example, you can configure a fetch request to return just object IDs but also include the row data (and update the row cache)—this can be useful if you’re just going to pass those object IDs from a background thread to another thread.

    Ok, but do we need to lock the Coordinator?

    There is typically no need to use locks with managed objects or managed object contexts. However, if you use a single persistent store coordinator shared by multiple contexts and want to perform operations on it (for example, if you want to add a new store), or if you want to aggregate a number of operations in one context together as if a virtual single transaction, you should lock the persistent store coordinator.

    That seems to be pretty clear that if you are performing operations on a persistent store from more than one thread, you should lock it.

    But wait – these are just read operations, shouldn’t they be safe? Well, apparently not:

    Core Data does not present a situation where reads are “safe” but changes are “dangerous”—every operation is “dangerous” because every operation has cache coherency effects and can trigger faulting.

    Its the cache we need to worry about. That’s why you need to lock – a read in one thread can cause data in another thread to get messed up through inadvertent cache changes. Your code never gave you problems because this is probably really rare. But its those edge cases and 1-in-1,000,000 bugs that can do the most damage…

    So, is it safe? My answer:

    • If nothing else is using your persistent store coordinator while you read, yes, you are safe.
    • If you have anything else using the same persistent store coordinator, then lock it before you get the object IDs.
    • Using a managed object context means the locking is automatically taken care of for you, so its also a fine possibility, but it looks like you don’t need to use it (and I agree it is nice to not make one just to get a few Object IDs).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have a background thread that does some periodic heavy data management. Because
I have numerous functions that are called from a background thread, but then need
I have a background worker thread that is constantly syncing data to/from a remote
I have a background thread running that fires events, but how can I ensure
I have a background thread and the thread calls some methods that update the
I have a helper class that should only ever run in a background thread.
if (reader.is_lazy()) goto tldr; I have a background thread that does some I/O-intensive background
I have about 4 threads. One thread keeps checking some data that the other
I have an OpenGL app that needs to perform some computations in the background
I have some UI code that needs to be updated from my background presenter

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.