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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:30:18+00:00 2026-06-14T18:30:18+00:00

I have an iOS app which is accessing a core data sql database from

  • 0

I have an iOS app which is accessing a core data sql database from two threads. Thread A (the main UI thread) updates a core data record, and Thread B then attempts to read from the Entity collection that Thread A has just updated. Trouble is, Thread B is not ‘seeing’ the change that Thread A persisted.

Thread B is created by adding an NSOperation subclass object to an NSOperationQueue. The main method of the NSOperation subclass looks like this:

-(void) main {

    // NEED to create the MOC here and pass to the methods.
    NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSConfinementConcurrencyType];

    [moc setUndoManager:nil];

    [moc setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy]; // Had been working for months 

    [moc setPersistentStoreCoordinator:getApp().persistentStoreCoordinator];

    [self doTheWorkWithMOC:moc]; // Actually performs updates, using moc


    moc = nil;



}

Later, Thread B saves its changes as follows:

@try {
                // register for the moc save notification - this is so that other MOCs can be told to merge the changes
                [[NSNotificationCenter defaultCenter] 
                 addObserver:getApp() 
                 selector:@selector(handleDidSaveNotification:)
                 name:NSManagedObjectContextDidSaveNotification 
                 object:moc];

                NSError* error = nil;
                if ([moc save:&error] == YES)
                {
                    NSLog(@"%s SAVED FINE",__FUNCTION__);

                }else {
                    NSLog(@"%s NOT saved, error=%@ %@",__FUNCTION__,error,[error localizedDescription]);

                }

                // unregister from notification
                [[NSNotificationCenter defaultCenter] 
                 removeObserver:getApp() 
                 name:NSManagedObjectContextDidSaveNotification 
                 object:moc];





            }
            @catch (NSException * e) {
                NSLog(@"%s Exception: %@",__FUNCTION__, e);

            }

The main UI appdelegate contains the following code to handle the save notification:

- (void)handleDidSaveNotification:(NSNotification*) note 
{

    @try {

        // Notifications run on the same thread as the notification caller.
        // However, we need to ensure that any db merges run on the main ui thread.
        // Hence:
        [self performSelectorOnMainThread:@selector(mergeContexts:) withObject:note waitUntilDone:NO]; 


    }
    @catch (NSException * e) {
        NSLog(@"appDelegate handleDidSaveNotification Exception: %@", e);
    }

}
-(void)mergeContexts:(NSNotification*) note 
{
    if ([__managedObjectContext tryLock]==YES)
    {
        [__managedObjectContext mergeChangesFromContextDidSaveNotification:note];

        [__managedObjectContext unlock];
    }

}

It all works fine most of the time.

However, I have one iPad where the changes written by Thread B are not detected when Thread A reads the database.

Can anyone see anything in my code which would cause this?

Many thanks

  • 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-14T18:30:19+00:00Added an answer on June 14, 2026 at 6:30 pm

    Journeyman,

    First, you should move to using the queue based MOCs introduced in iOS v5 and Lion. This will make it much easier to keep your two MOCs in sync. You will no longer need to use the locking system.

    Second, once you’ve moved to the queued MOCs, then it is quite straightforward to keep them in sync in response to the “did save” notifications.

    Third, why are you always adding and removing the observer for the did save notifications? Doesn’t that look suspicious to you? Clearly, you are missing some update between the MOCs.

    Andrew

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

Sidebar

Related Questions

I have a function in an iOS app which reads in some data from
I have an iOS app which I need to upload the binary of. From
I have an iOS app which gets some JSON from a server (in the
Overview I have an iOS app which sends local notifications at specific dates. I
I have a memory leak in my iOS app which sometimes causes it to
I have an iOS app with a UITableView that has flexible width, which allow
1)I am creating a ios app in which I have to scan a pool
I have been struggling with setting up an iOS (Objective-C) app which utilizes the
I have enrolled in the iOS developer's program. I've developed an app which I
I have an iOS-App which uses ARC. I don't use InterfaceBuilder, all UI is

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.