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

  • Home
  • SEARCH
  • 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 5946463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:49:36+00:00 2026-05-22T16:49:36+00:00

I have two managedobjectcontext I fetch object from managedobjectcontext1 and want two save it

  • 0

I have two managedobjectcontext

I fetch object from managedobjectcontext1 and want two save it in the second object context while try to do that I get the error: “Illegal attempt to establish a relationship between objects in different contexts”. The code I use

    NSError * error;

    NSPersistentStoreCoordinator *persistentStoreCoordinator_OLD;
    NSPersistentStoreCoordinator *persistentStoreCoordinator_NEW;

    NSManagedObjectModel *managedObjectModel_OLD;
    NSManagedObjectModel *managedObjectModel_NEW;


    NSManagedObjectContext *managedObjectContext_OLD;
    NSManagedObjectContext *managedObjectContext_NEW;


    NSURL *storeUrl_OLD = [NSURL fileURLWithPath: [[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent: @"CoreDataTutorialPart4.sqlite"]]; //   CoreDataTutorialPart4.sqlite
    NSURL *storeUrl_NEW = [NSURL fileURLWithPath: [[self applicationPrivateDocumentsDirectory] stringByAppendingPathComponent: @"Newpaxera.sqlite"]]; //   CoreDataTutorialPart4.sqlite



    // ADJUST THE MODEL

    managedObjectModel_OLD = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];  

    managedObjectModel_NEW = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; 





    persistentStoreCoordinator_OLD = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel_OLD];
    if (![persistentStoreCoordinator_OLD addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl_OLD options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

    }
    else {
        NSLog(@"SUCCESS");
    }

    persistentStoreCoordinator_NEW = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel_NEW];
    if (![persistentStoreCoordinator_NEW addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl_NEW options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

    }
    else {
        NSLog(@"SUCCESS");
    }

    managedObjectContext_OLD = [[NSManagedObjectContext alloc] init];
    [managedObjectContext_OLD setPersistentStoreCoordinator: persistentStoreCoordinator_OLD];

    managedObjectContext_NEW = [[NSManagedObjectContext alloc] init];
    [managedObjectContext_NEW setPersistentStoreCoordinator: persistentStoreCoordinator_NEW];

NSFetchRequest *fetchRequest_Study = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity_Study = [NSEntityDescription 
                                   entityForName:@"Studies" inManagedObjectContext:managedObjectContext_OLD];
    [fetchRequest_Study setEntity:entity_Study];

    NSMutableArray *StudiesList = [managedObjectContext_OLD executeFetchRequest:fetchRequest_Study error:&error];


    for(int i =0 ; i < [StudiesList count] ; i++){

        Studies  *study = [StudiesList objectAtIndex: i ];

        Studies *study_NEW = (Studies *)[NSEntityDescription insertNewObjectForEntityForName:@"Studies" inManagedObjectContext:managedObjectContext_NEW];

        study_NEW.SudyID = study.SudyID;
        study_NEW.StudyDate=study.StudyDate;
        study_NEW.ModalityName=study.ModalityName;

        study_NEW.Studiesstudent = study.Studiesstudent ; // raise sigapart error here 

Studiesstudent is an object of another entity class

Any suggestions for how to resolve this? Xcode does not give an error in the other numerical or string data.

  • 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-22T16:49:37+00:00Added an answer on May 22, 2026 at 4:49 pm

    You cannot transfer managed objects between context, especially when they the context are initialized to other persistent stores. You need to clone the managed objects in the new context i.e. create new objects with the same attributes as the old.

    Are you sure you don’t need to migrate the persistent stores? It looks like you are trying to update an existing store to a new model. That is what migration is for.

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

Sidebar

Related Questions

I have two applications written in Java that communicate with each other using XML
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two identical tables and need to copy rows from table to another.
I have two classes, and want to include a static instance of one class
I have two spreadsheets... when one gets modified in a certain way I want
We have a project that has two one to many relationships. We encounter a
Have two fields start and end, both are dates, I want to write a
I have two UITableViewControllers. I'm pushing to the second one and calling the following
I have a managedObjectContext for the main application that has a bound NSArrayController and
I want to have the equivalent of two completely unrelated tables in my database,

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.