NSMergeConflict for NSManagedObject with single Ma

2019-02-21 02:35发布

问题:

I am working with coreData, I have one to many relationship between folders and files.

I am using only one MOC through out my application.I am just passing it to different

viewControllers , performing operations like add, edit, delete and then saving.

My rootViewController uses NSfetchResultsController, I create folders using it, save and display on my table.

saving I do it this way

        NSError *error;
        if (![self.managedObjectContext save:&error]) {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }

then whenever I select a folder, I open a file ViewController, while opening, I pass the MOC to file VC this way

         FileViewController *file = [[FileViewController alloc] initWithNibName:@"FileViewController" bundle:nil];

        file.managedObjectContext = self.managedObjectContext;
        file.controller = self.controller;

then I create a file inside FileVC and again save it in FileVC, this way

        NSError *error;
        if (![self.managedObjectContext save:&error]) {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }

by doing this, am I using two MOC's or a single MOC?

In my appdelegate.m, I also tried this

self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

            _navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootViewController];
            [self.managedObjectContext setMergePolicy:NSMergeByPropertyStoreTrumpMergePolicy];

            self.rootViewController.managedObjectContext = self.managedObjectContext;

Sometimes when I add a file inside a folder , I get "NSMergeConflict for NSManagedObject"

Please help

Regards Ranjit.

回答1:

I'm pretty sure you're using only the one managed object context. You'd have to create a second one to be using a second one. Although even if I'm right about that, I have no comment about the NSMergeConflict as I'm dealing with that myself right now.