0

I'm getting Illegal attempt to establish a relationship 'userInfo' between objects in different contexts while creating relation between two objects. But they have common Persistent coordinator and same context. Please help me to find the issue.

- (void)parser:(NSDictionary *)attributeDict
{

    NSEntityDescription *ent = [NSEntityDescription entityForName:@"ThreadInfo" inManagedObjectContext:self.managedObjectContext];
    // create an ThreadInfo managed object, but don't insert it in our moc yet
    ThreadInfo *info = [[ThreadInfo alloc] initWithEntity:ent insertIntoManagedObjectContext:nil];
    self.threadinfo = info;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserInfo" inManagedObjectContext:self.managedObjectContext];
fetchRequest.entity = entity;

// narrow the fetch to these two properties
NSPredicate *threadPredicate = [NSPredicate predicateWithFormat:@"userEmail == %@",[attributeDict valueForKey:@"userEmail"]];
[fetchRequest setPredicate:threadPredicate];

NSError *error = nil;

NSArray *fetchedItems = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

for (UserInfo *info in fetchedItems)


{


            if([[attributeDict allKeys] containsObject:@"userEmail"])
            {
                if([attributeDict valueForKey:@"userEmail"]!=[NSNull null])
                {
                    self.threadinfo.userEmail=[attributeDict valueForKey:@"userEmail"];
                }
            }

            if([[attributeDict allKeys] containsObject:@"thread"])
            {
                if([attributeDict valueForKey:@"thread"]!=[NSNull null])
                {
                    self.threadinfo.threadID=[attributeDict valueForKey:@"thread"];
                }
            }
   if(info!=nil)
    {
        self.threadinfo.userInfo=info;// **CRASH HAPPENED AT THIS LINE**
        [info addThreadDetailsObject:self.threadinfo];
    }


          ThreadInfo *tInfo = nil;

         [self.managedObjectContext insertObject:tInfo];

        if ([self.managedObjectContext hasChanges]) {

        if (![self.managedObjectContext save:&error]) {

            NSLog(@"Unresolved error %@", error);
            abort();
        }


}

.h

@interface batchSave : NSOperation
@property (nonatomic, strong) ThreadInfo *threadinfo;
@property (nonatomic, strong) UserInfo *userInfos;

CRASH

 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'userInfo' between objects in different contexts source = <ThreadInfo: 0x17dd8280> (entity: ThreadInfo; id: 0x17dda6a0 <x-coredata:///ThreadInfo/t908A5944-FCD9-422B-80D7-83B71D243FB52> 
Jan
  • 1,744
  • 3
  • 23
  • 38

1 Answers1

0

ThreadInfo has a nil context based on your use of the -initWithEntity: insertIntoManagedObjectContext: You should be passing in a context here.

Also, you should not be using -initWithEntity: insertIntoManagedObjectContext:. It is recommended you use the class method +[NSEntityDescription insertNewObjectForEntityForName: inManagedObjectContext:] and you should not pass nil for the context.

Update

Your code still does not make sense.

You -init... the ThreadInfo with a nil context for no reason and then call -insertObject: later against a nil reference called tInfo.

If this is your actual code then it has numerous errors and I am having difficulty even tracking what you are trying to do.

As it stands, your created ThreadInfo entity does not have a context. That is causing the error.

  1. Stop using -initWithEntity: insertNewObjectForEntityForName: and use the method I recommended.
  2. Don't use -insertObject:, it is redundant if you pass the context immediately.

    self.threadinfo.userInfo=info;// **CRASH HAPPENED AT THIS LINE**
    [info addThreadDetailsObject:self.threadinfo];
    

Assuming you have a proper bi-directional relationship, these lines are doing the exact same thing and you don't need both.

Why are you storing threadinfo as an instance property? Looks like it should only ever be a local variable.

Change your -init... and the crash will be resolved. But you have other issues in this code.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • I have tried But i'm getting a crash in database save abort() , save error, Code updated – Jan May 27 '15 at 15:13
  • Also my data not saving to DB while using this fix . – Jan May 27 '15 at 15:23
  • `if ([self.managedObjectContext hasChanges]) { if (![self.managedObjectContext save:&error]) { NSLog(@"Unresolved error %@", error); abort(); }` This code comes only after several inserts to the managedobjectcontext in the original code. I was trying to implement batch save. But this error is not allowing me to do batch save. – Jan May 28 '15 at 12:32