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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:06:42+00:00 2026-06-17T01:06:42+00:00

I perform some work using an NSBlockOperation and inside this block the persistent store

  • 0

I perform some work using an NSBlockOperation and inside this block the persistent store loses its persistent stores.

the following code:

- (void) eraseCoreData_ManagedObjectsInArray:(NSArray *) arrayOfManagedObjectsToDelete usingManagedContext:(NSManagedObjectContext *) managedObjectContext
{
    NSLog(@"Managed object context is %@", managedObjectContext);
    NSLog(@"----->Persistent store of the deletion context has %d stores", [managedObjectContext.persistentStoreCoordinator.persistentStores count]);

    // add the operation to the queue with a block
    [self.coreDataDeletionQueue addOperationWithBlock:^{

    for (NSManagedObject *objectToDelete in arrayOfManagedObjectsToDelete) {
        [managedObjectContext deleteObject:objectToDelete];
    }

    NSLog(@"Managed object context in operation block is %@", managedObjectContext);
    NSLog(@"Persistence store coordinator in operation block is %@", managedObjectContext.persistentStoreCoordinator);
    NSLog(@"Persistent store of the deletion context in operation block has %d stores", [managedObjectContext.persistentStoreCoordinator.persistentStores count]);

    NSError *saveError = nil;
    if (![managedObjectContext save:&saveError]) {
        NSLog(@"Couldn't save Core-Data state for the deleted objects result: %@", [saveError localizedDescription]);
    }
    }];
}

logs this out:

Managed object context is < NSManagedObjectContext: 0xa46ff10 >

—–>Persistent store of the deletion context has 1 stores

Managed object context in operation block is < NSManagedObjectContext: 0xa46ff10 >

Persistence store coordinator in operation block is < NSPersistentStoreCoordinator: 0xa1a45e0 >

Persistent store of the deletion context in operation block has 0 stores

* Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘This
NSPersistentStoreCoordinator has no persistent stores. It cannot
perform a save operation.’

I am not sure why this sudden lose of stores, could it be due to the fact that this is running in a test suite that I set to use an in memory store and the memory is not shared with this NSOperationQueue that executes the blocks?

  • 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-17T01:06:44+00:00Added an answer on June 17, 2026 at 1:06 am

    My guess you are using your code in the wrong manner.

    First, each thread must have its own context. So, inside the block operation you need to create a new context and use it within that block.

    NSManagedObjectContext *blockContext = [[NSManagedObjectContext alloc] init];
    [blockContext setPersistentStoreCoordinator:[managedObjectContext persistentStoreCoordinator]];
    

    Then, if you need to share objects within contexts, use NSManagedObjectID and not NSManagedObject.
    Before creating that block save the main context or you will have only temporary ids. Otherwise use - (BOOL)obtainPermanentIDsForObjects:(NSArray *)objects error:(NSError **)error to obtain permanent ids without saving.

    NSArray *objectIDs = [arrayOfManagedObjectsToDelete valueForKey:@"objectID"];
    

    Within the block use - (NSManagedObject *)existingObjectWithID:(NSManagedObjectID *)objectID error:(NSError **)error to retrieve the object you are interested in through blockContext.

    Your code could look like

    NSArray *objectIDs = [arrayOfManagedObjectsToDelete valueForKey:@"objectID"];
    
    // add the operation to the queue with a block
    [self.coreDataDeletionQueue addOperationWithBlock:^{
    
        NSManagedObjectContext *blockContext = [[NSManagedObjectContext alloc] init];
        [blockContext setPersistentStoreCoordinator:[managedObjectContext persistentStoreCoordinator]];
    
        NSError* error = nil;
        for (NSManagedObject *objectID in objectIDs) {
            // some error handling here...
            NSManagedObject* objectToDelete = [existingObjectWithID:objectID error:&error];
            [blockContext deleteObject:objectToDelete];
        }
    
        if (![blockContext save:&error]) {
            NSLog(@"Couldn't save Core-Data state for the deleted objects result: %@", [error localizedDescription]);
        }
    }];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Talend to perform some ETL and I'm getting the following error when
I'm currently using a Timer and TimerTask to perform some work every 30 seconds.
I'm using ThreadPoolExecutor in SomeService to perform some work on data. The problem is
I am using RandomAccessFile to perform some writes to a file as part of
A winforms dialog is using BackgroundWorker to perform some asynchronous operations with significant success.
I have a MSBuild script that starts PowerShell to perform some work on remote
i want to perform some test by using Json . I am using google
I'm using dm-observer to observe my dm models, and I need to perform some
Instead of selecting an item first and then using a button to perform some
Yesterday I found this great ORM and would like to perform some testings on

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.