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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:01:21+00:00 2026-06-10T01:01:21+00:00

I have a NSOperationQueue set to NSOperationQueueDefaultMaxConcurrentOperationCount . It is filled with NSOperation objects

  • 0

I have a NSOperationQueue set to NSOperationQueueDefaultMaxConcurrentOperationCount. It is filled with NSOperation objects (nothing weird so far). I subclassed the NSOperation to do some background tasks.

  1. Download data from the internet.
  2. Parse the data so I can read it.
  3. Create a NSManagedObject:

    [NSEntityDescription insertNewObjectForEntityForName:@”Channel” inManagedObjectContext:context];

  4. Save it with the context.

    [managedObjectContext save:&error]

I like this all to happen in the background so the UI won’t get blocked. I read this article about concurrency with core data, and as far as I understood it. The best way would be to create a new NSManagedObjectContext in every NSOperation, but share the same persistent store coordinator.

That’s easily done, however, when it comes to saving the context it says in the documentation it is error prone to do so. So my question is the following:

  1. If I have different operations running in the NSOperationQueue, could those operations interfere with each other while saving the managed object context? Or does it wait to execute the following operation till the saving has been complete?

  2. Can I safely save the context in a NSOperation? Or is it really bad practice?

I hope someone can shine a light on this matter, because I am really stuck at the moment.

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

    What you need to do is the following:

    1. Create a managed object context for each NSOperation. Create this new context on the main method, because this is when it’s executing on the right thread.
    2. Assign the context persistent store coordinator.
    3. Create an observer to receive the NSManagedObjectContextDidSaveNotification. This is the only way the main context will know at the time the changes were made on the NSOperation’s context. Make sure the merge call is made on the thread/block the merging context lives in. If you are merging with the main thread’s context, call the mergeChangesFromContextDidSaveNotification: method on the main thread with the notification from the NSOperation’s context.

    Also, ask yourself if you really want to have all these operations working concurrently. Per the documentation:

    The default maximum number of operations is determined dynamically by the NSOperationQueue object based on current system conditions.

    You do not have control over how many NSOperations will be operating at the same time. If this is not what you want, you might be better if you just go with a serial NSOperationQueue (maxConcurrentOperation=1), considering the fact that you are going to be locking the database to do the save, and also because you have networking being done as well.

    You can safely save inside the NSOperation’s main method, if you take the precautions mentioned above.

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

Sidebar

Related Questions

I have been using NSOperationQueue to download some data from server on background. i
Hello i am using NSOperationQueue to download images in the background. I have created
I have objects that extends NSOperation. I also have NSOperationQueue. I have a timer
I want to implement some background network requests using an NSOperationQueue . I have
I have some problems on elaborating a useful strategy to support background for NSOperationQueue
I have an NSOperationQueue to which i have added my custom class's objects(inherits from
I have a weird issue. Here is the setup: I have a NSOperationQueue which
I have some iPhone SDK 4.0 code which initializes an NSOperationQueue and then adds
I have a Download object which handles NSURLConnection . Then I have NSOperation object
I have a project which downloads images in background using NSOperationQueue . It was

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.