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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:08:16+00:00 2026-05-27T15:08:16+00:00

i´m currently learning Core Data. Core Data is great but i can not explain

  • 0

i´m currently learning Core Data. Core Data is great but i can not explain the behaviour with a second managed object context in a background thread.

I have an entity called TestEntity with 2 attributes (testId and testDescription)
On the main thread i fetch the entity with the testId = 1 and store this managed object into an instance variable.

    NSEntityDescription *entityDescription = [NSEntityDescription
                                          entityForName:@"TestEntity" inManagedObjectContext:self.managedObjectContext];

NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];

NSNumber *testId = [NSNumber numberWithInt:1];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"testId == %@", testId];
[request setPredicate:predicate];

NSError *error = nil;
NSArray *array = [self.managedObjectContext executeFetchRequest:request error:&error];

t1 = [[array objectAtIndex:0] retain];

TestThread *tt = [TestThread new];

NSOperationQueue *queue = [NSOperationQueue new];
[queue addOperation:tt];

[queue waitUntilAllOperationsAreFinished];
NSLog(@"%@", [t1 valueForKey:@"testDescription"]);

Then is start a NSOperation with a NSOperationQueue called TestThread.
In the main method of this TestThread i create a second managed object context, fetch the same entity (testId = 1) like the main thread, change the testDescription property and save the new context without any errors.

    tgAppDelegate *delegate = [[NSApplication sharedApplication] delegate];

self.context = [[[NSManagedObjectContext alloc] init] autorelease];
[context setPersistentStoreCoordinator:delegate.persistentStoreCoordinator];


NSEntityDescription *entityDescription = [NSEntityDescription
                                          entityForName:@"TestEntity" inManagedObjectContext:self.context];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];

// Set example predicate and sort orderings...
NSNumber *testId = [NSNumber numberWithInt:1];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"testId == %@", testId];
[request setPredicate:predicate];

NSError *error = nil;

NSArray *array = [context executeFetchRequest:request error:&error];
TestEntity *t1 = [array objectAtIndex:0];

t1.testDescription = @"abc";
[context save:&error];
if (error) {
    // do something
}

The behaviour i can not explain my self is, that the NSLog output after

[queue waitUntilAllOperationsAreFinished];

has the same string value which updated in my background thread.

abc

My understanding of Core Data and Multithreading is that i have to do an explicit merge between contextes.
In my test app there is no merge.

Can anyone explain why this happenes?

  • 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-05-27T15:08:17+00:00Added an answer on May 27, 2026 at 3:08 pm

    By default, a fetch request will return objects as faults so their content is not actually loaded until you access it (you can learn more about faulting under this link).

    To see your expected behavior, try logging the original value of t1.testDescription before you start the second thread.

    You can also set

    self.context.returnObjectsAsFaults = NO;
    

    but this might have a negative impact on the memory footprint of your app.

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

Sidebar

Related Questions

I'm currently learning to use Core Data on iOS , in my test application
I'm currently learning C#, but I am stuck on something and I can't find
I'm currently learning the network programming with C/C++... I have experience .NET but not
I'm currently learning clojure, but I was wondering how to get and store user
As I'm currently learning Django / Python, I've not really been using the concept
Currently learning Linq to Entity. I been successful, but came stumped with the orderby
I'm currently learning about jsf 2.0 from core jsf 2.0 book + glassfish +
I'm currently learning about jsf 2.0 from core jsf 2.0 book + glassfish +
I am currently learning regex but need this quite urgently. I have a set
I'm currently learning Python and I'm trying to make a small scraper but I'm

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.