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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:03:56+00:00 2026-06-08T14:03:56+00:00

I am having a problem in saving the data in Core Data while im

  • 0

I am having a problem in saving the data in Core Data while im multithreaded application. The scenario is the following:

I have some http requests which runs in background threads us NSOperation. When the data arrives which is in JSON I tried to save those data in Core Data.
I have created the separate NSManagedObjectContext for each thread now when I try to save the data it never saves properly. Sometime half data is saved sometime none.

Any guesses why this is hapaneing

+ (void) initialize {
    contextfactory = [[ThreadedContext alloc] init];
}

- (id) init {
    if((self = [super init])) {
    }
    [self setupObjectModel];
    [self setupStoreCoordinator];
    return self;
}

- (void) setupObjectModel {
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
}    

- (NSString*) sharedDocumentsPath {
    static NSString *SharedDocumentsPath = nil;
    if (SharedDocumentsPath)
        return SharedDocumentsPath;

    // Compose a path to the <Library>/Database directory
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    return  [[libraryPath stringByAppendingPathComponent:@"Database"] retain];

}

- (NSURL *) storeUrl {

    NSString * const kCoreDataSQLiteName = @"XPPS.sqlite";
    // Get the paths to the SQLite file
    NSString *storePath = [[self sharedDocumentsPath] stringByAppendingPathComponent:kCoreDataSQLiteName];
    return [NSURL fileURLWithPath:storePath];
}

- (void) setupStoreCoordinator {
    NSError *error = nil;
    if(!_persistentStoreCoordinator)
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: _managedObjectModel];
    [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL: [self storeUrl] options:nil error:&error];
    if(error) NSLog(@"%@", error);
}

- (NSManagedObjectContext *) createObjectContext {
    NSManagedObjectContext *output = [[[NSManagedObjectContext alloc] init] autorelease];
    [output setPersistentStoreCoordinator:_persistentStoreCoordinator];
    return output;
}

+ (NSManagedObjectContext *) buildContext {
    return [contextfactory createObjectContext];
}

+ (void) createStoreCoordinator {
    [contextfactory setupStoreCoordinator];
}

+ (NSString *) storePath {
    return [[contextfactory storeUrl] path];
}

This is the code for getting the managed context for the each thread. It is derived from NSObject and not NSOperation. Any guess how I can fix this issue?

  • 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-08T14:03:57+00:00Added an answer on June 8, 2026 at 2:03 pm

    Did you see this apple example?

    Your code seems good but it is only one important think to create NSManageObject for every background thread. This example can show you how can you use the NSManagedObjectContextDidSaveNotification and save your data on a safe way.

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

Sidebar

Related Questions

am having problem in Saving some data in my Java code. I have like
i'm having a problem while encrypting some data in the file. i'm using simple
I'm using Core data on my app ok, i have a problem when saving
I am having problems while saving a pre-persisted JDO object in google-app-engine data store.
I'm having some trouble saving some data after using PHP's simplexml_load_file function. I want
I'm having a strange problem saving data with cakePHP. I'll explain as I go.
I have a database having 28 rows just for saving app data. My goal
I have a WPF application which saves its data to XML files in a
Okay, I'm having a problem saving after I've deleted all the objects I have
I am fairly new at core data and am having a problem. My app

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.