Illegal attempt to establish a relationship between objects in different contexts - iphone

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.

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.

Related

Core Data Crash during Save without error. Only one Context and only main thread

I'm simply trying to delete three nsmanagedobject from an entity, as i already do in other application,where all went ok. This time core data seems to crash during save, there's no error, i'm using the app delegate context in all the app, so i think that isn't a multiple context problem... I have tried a check with [NSThread isMainThread] , during the save i'm in the main thread, so isn't a threading problem... What's wrong in my code?
-(void)EraseStore{
if (contesto == nil) {
contesto = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSError *error=nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Store" inManagedObjectContext:contesto];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [contesto executeFetchRequest:fetchRequest error:&error];
NSString *Store1=[NSString stringWithFormat:#"%#",#"55"];
NSString *Store2=[NSString stringWithFormat:#"%#",#"63"];
NSString *Store3=[NSString stringWithFormat:#"%#",#"11"];
for (int i=0; i<[fetchedObjects count]; i++) {
NSString *StoreId=[NSString stringWithFormat:#"%#",[[fetchedObjects objectAtIndex:i]valueForKey:#"id"]];
if ([StoreId isEqualToString:Store1] || [StoreId isEqualToString:Store2]|| [StoreId isEqualToString:Store3]) {
[contesto deleteObject:[fetchedObjects objectAtIndex:i]];
}
}
NSError *error1 = nil;
BOOL success = [contesto save:&error1];
if (!success) {
NSLog(#"Unresolved error2 %#, %#", error1, [error1 userInfo]);
}
else{
NSLog(#"saved");
}
}
How I create my context:
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext_ != nil) {
return managedObjectContext_;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext_;
}
The error message I am getting:
Unresolved error2 Error Domain=NSCocoaErrorDomain Code=133020 "The operation couldn’t be completed. (Cocoa error 133020.)" UserInfo=0x23cf00 {conflictList=( "NSMergeConflict (0x23cdc0) for NSManagedObject (0x234960) with objectID '0x2da8b0 ' with oldVersion = 1 and newVersion = 2 and old object snapshot =....
EDIT
In database structure i haven't set any relationship between tables.

An instance of NSFetchedResultsController requires a non-nil fetchRequest and managedObjectContext?

I'm getting this error while trying to use Core Data:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An instance of NSFetchedResultsController requires a non-nil fetchRequest and managedObjectContext'
I traced the error back to this in my modal view controller which contains the Table View:
- (void)setupFetchedResultsController
{
NSString *entityName = #"Location";
NSLog(#"Setting up a Fetched Results Controller for the Entity named %#", entityName);
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
// ERROR OCCURS IN THE LINE BELOW vvv
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
}
This is what I have in my AppDelegate:
- (void)setupFetchedResultsController
{
NSString *entityName = #"Location";
NSLog(#"Setting up a Fetched Results Controller for the Entity named %#", entityName);
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self.fetchedResultsController performFetch:nil];
}
I get no errors there, and here's more of the App Delegate:
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil)
{
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
{
// Using abort() right now just for development
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Core Data stack
- (NSManagedObjectContext *)managedObjectContext
{
if (__managedObjectContext != nil)
{
return __managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil)
{
__managedObjectContext = [[NSManagedObjectContext alloc] init];
[__managedObjectContext setPersistentStoreCoordinator:coordinator];
}
return __managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel
{
if (__managedObjectModel != nil)
{
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:#"Model" withExtension:#"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"Forecaster.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
return __persistentStoreCoordinator;
}
#pragma mark - Application's Documents directory
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
and in my Model.xcdatamodeid:
Any help would be great! Thanks!
Here is the Method:
- (void)setupFetchedResultsController
{
NSManagedObjectContext *moc = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:#"Location" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
{
// Deal with error...
}
else
{
// your data is in array
}
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"FailedBankInfo"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (FailedBankInfo *info in fetchedObjects) {
NSLog(#"Name: %#", info.name);
FailedBankDetails *details = info.details;
NSLog(#"Zip: %#", details.zip);
}

Core Data not persisting in iOS 5

V. iOS 5
I have my Model2.xcdatamodelId containg 2 Entities. My Model is named 2, because I already have a Singleton named Model for some managing.
So from that, I have Model2.h and Model2.m.
My Problem : The first time, my Model2 is initialise, I put in some default data, then I commit. Works good, it says that my Model2 has been saved correctly. After what I am reading my data, data are shown from the database.. so it is successfuly in the database. BUT.. when closing and killing my app, my app seems to have lost all data.. and start over again with creating default data cause it is empty...
TIP: I'm thinking my problem could be about the initialize.. whithin those lines:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSURL * storeUrl = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent: #"ProjectXYZ.db"]];
Since ProjectXYZ.db doesn't exist, it should create it?.. that's the part where I'm getting lost.. but it seems to worked on another project I've worked on... :S
Here's my Model2.h
#import <CoreData/CoreData.h>
#import "Photos_Trophies.h"
#import "Trophies.h"
#interface Model2 : NSObject
// High-level methods.
+ (void) commit;
...
// Object Retrieval
+ (NSArray*) trophies;
...
// Object Creation
+ (id) trophiesWithTitle:(NSString *)title;
#end
And my Model2.m
#import "Model2.h"
#import <UIKit/UIKit.h>
static NSManagedObjectContext * ctx;
#implementation Model2
+ (void) initialize {
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSURL * storeUrl = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent: #"ProjectXYZ.db"]];
NSError * error = nil;
ctx = [[NSManagedObjectContext alloc] init];
ctx.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
ctx.undoManager = [[NSUndoManager alloc] init];
if (![ctx.persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(#"%#", error);
}
//TEMPORARY... these are default trophies for example
if ([[Model2 trophies] count] == 0) {
[self trophiesWithTitle:#"Saved Trophy Test 1"];
[self trophiesWithTitle:#"Saved Trophy Test 2"];
[self trophiesWithTitle:#"Saved Trophy Test 3"];
[self commit];
}
}
+ (void) commit {
NSError* error = nil;
if (![ctx save:&error]) {
NSLog(#"ERREUR DANS COMMIT: %#", error.localizedDescription);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors)
NSLog(#"DetailedError: %#", [detailedError userInfo]);
} else
NSLog(#"%#", [error userInfo]);
} else
NSLog(#"Model2 SAVED");
}
+ (NSArray*) trophies {
NSFetchRequest* req = [[NSFetchRequest alloc] init];
req.entity = [NSEntityDescription entityForName:#"Trophies" inManagedObjectContext:ctx];
req.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"trophies_title" ascending:YES]];
NSError* error = nil;
NSArray* objects = [ctx executeFetchRequest:req error:&error];
return objects;
}
+ (id) trophiesWithTitle:(NSString *)title {
Trophies * trophies = [NSEntityDescription insertNewObjectForEntityForName:#"Trophies" inManagedObjectContext:ctx];
trophies.trophies_title = title;
return trophies;
}
Your problem is this: NSInMemoryStoreType You need to use a persistent backing store.
Do something like this:
NSURL *storeUrl = [NSURL fileURLWithPath:[self persistentStorePath]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
error:&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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object model
Check the error message to determine what the actual problem was.
*/
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
NSAssert(error == nil, #"error creating persistentStoreCoordinator");
}

problems saving to persistent storage iphone

I am writing an iphone app; I need to save the data to persistent storage, but I am having the problems...
I created an entity in xcdatamodel and I have no problem getting the default value set for my attribute. In my testing, I go thru the code several times below and can change this attribute (it's an integer) while the application is active. However, when I close and reopen the app, my integer (myInt) is back to the default value when I expected it to be the last value I updated it to.
Here is what I am doing:
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSEntityDescription *myTestEntity = [NSEntityDescription
entityForName:#"TestEntity"
inManagedObjectContext:managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:timeStampEntity];
NSError *error = nil;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil) {
// TODO: Handle the error
}
for (id oneObject in fetchedObjects)
{
NSInteger myInt = [[oneObject valueForKey:#"myStoredInt"]intValue];
myInt++;
[oneObject setValue:[NSNumber numberWithInt: myInt] forKey:#"myStoredInt"];
NSError *error = nil;
if (![managedObjectContext save:&error]) {//TODO: Handle error
}
}
[fetchRequest release];
I was hoping that line:
if (![managedObjectContext save:&error])
would do the job and store the changes to the persistent storage, but it does not! (Having this line inside or outside the for loop does not change the result)
What am I doing wrong?
Thank you very much for your help!
It will be helpful if you could show your app delegate code. My guess is that somewhere you are reseting the data your self. maybe in the method that you load the start data.
3 notes for now:
you don't need to place the save
method inside the loop. you should
place it after all the changes are
made and just once.
check if you have a save method called in the (if not add them):
- (void)applicationWillTerminate:(UIApplication*)application {
[self saveContext];
}
- (void)applicationDidEnterBackground:(UIApplication*)application {
[self saveContext];
}
if it dose not help try to see if you are loading the default values on every lunch of the app.
GOOD LUCK
and welcome
EDIT
Hey, you can do that -
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: #"anyname.sqlite"]];
//check if the store already exist?
BOOL firstRun = NO;
if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path] isDirectory:NULL]) {
firstRun = YES;
}
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
if(firstRun){
//add your default data here
}
}

How to refresh a TableView when data is inserted in the AppDelegate?

To pre-populate my Database for a small Iphone-App I do a check in the persistentStoreCoordinator Method in the AppDelegate if the sqlite-file is already there or not. If not I add some data into a table after the db is created. But the data should not directly shown in the RootViewController but in another TableView which can be accessed by pressing a Option in the RootViewController.
The Problem is that the data are saved but are not shown in the TableView. But if terminate and restart the app the data are there.
I have passed the managedObjectContext from my appDelegate to the the RootViewController and from there to the TableViewController. So the managedObjectContext is the same everywhere.
Here is the Code where I pre-populate the DB (in this example just one sample row):
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:#"TestDBApp.sqlite"];
NSString *storePath = [storeURL relativePath];
BOOL noDb = false;
NSFileManager *fileManager = [NSFileManager defaultManager];
if( ![fileManager fileExistsAtPath:storePath]) {
noDb = true;
}
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
if( noDb) {
noDb = false;
[self populateDB];
}
return persistentStoreCoordinator_;
}
- (void) populateDB {
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Kunde" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"oxid" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:#"Root"];
NSError *error = nil;
if (![aFetchedResultsController performFetch:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
[self insertNewKunde:aFetchedResultsController];
[self saveContext];
[aFetchedResultsController release];
[sortDescriptors release];
[sortDescriptor release];
[fetchRequest release];
}
- (void)insertNewKunde:(NSFetchedResultsController *)fetchedResultsController {
// Create a new instance of the entity managed by the fetched results controller.
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[fetchedResultsController fetchRequest] entity];
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
// If appropriate, configure the new managed object.
[newManagedObject setValue:[[NSDate date] description] forKey:#"oxid"];
[newManagedObject setValue:#"Max" forKey:#"fname"];
[newManagedObject setValue:#"Mustermann" forKey:#"lname"];
// Save the context.
NSError *error = nil;
if (![context save:&error]) {
NSLog(#"Unresolved error %#, %#", error, [error userInfo]);
abort();
}
}
The managedObjectContext is passed from the AppDelegate to the RootViewController as:
- (void)awakeFromNib {
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;
}
and to the other TableViewController:
- (void)awakeFromNib {
kundenVC = [[KundenViewController alloc] initWithManagedObjectContext:self.managedObjectContext];
}
Can anyone help me or give a hint?
I also tried to reload the tableView through the viewWillAppear-Method. But no effect.
It is very difficult to guess what is happening but my hunch is that you are not calling self.populateDB until the RootViewController is actually created. This means the RootViewController does not see that data because it is not created.
I could be wrong but I guess that your last line in delegate's awakeFromNib method is eventually calling populateDB through self.managedObjectContext -> persistentStoreCoordinator -> self.populateDB.
Please try to add a line right at the top of awakeFromNib looking like this:
(void)awakeFromNib {
NSManagedObjectContext *context = self.managedObjectContext;
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = context;}
This way I hope the your problem is solved because self.managedObjectContext will call the getter (- (NSManagedObjectContext)managedObjectContext;) instead of the field. Inside there (if you use the generated code) there is a call to the getter of ** persistentStoreCoordinator** which eventually calls you populateDB method.
In case that does not work go ahead and set a breakpoint inside your populateDB. When the debugger stops you should see at the method call stack where you populateDB is actually called from and that might help to figure out your problem.
-Andy
Well, the important part you left out to answer your question. What is [self saveContext] and how are you displaying the records in your table view.
Because your data is saved I assume that the code given here works. The only guess I can make is to use reset on the NSManagedObjectContext and then refetch.
BTW I like the "insertNewKunde" which seems to be the new German grammar ;-)