Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep getting "This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation." upon closing my app (OSX)

i've checked several posts on the internet and stackOverflow but couldn't find an answer so far. This is my AppDelegate, as far as I know, those implementations are pretty standard.. i just added the following line and passed the arguments, but it didn't help..

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
  [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
  [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

I cleaned my project, that didn't help either. Also the ApplicationSupport folder does not get created. Is it possible that this is the cause of the problem? I didn't create the App with the option "use core data", but I provided the the necessary methods...

-(NSPersistentStoreCoordinator *)persistentStoreCoordinator{...} is at the bottom!

Help is greatly appreciated!

#import "WebLogClientAppDelegate.h"

// create anonymous catergories for uses in this class
@interface WebLogClientAppDelegate();
@property(nonatomic, readonly) NSString *applicationSupportFolder;
@property(nonatomic, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@end

@implementation WebLogClientAppDelegate
@synthesize autorPrefFeld, benutzerPrefFeld, passwortPrefFeld, hauptfenster,
managedObjectModel, managedObjectContext, autor;

- (void) applicationWillFinishLaunching:(NSNotification *)notification
{
    NSLog(@"applicationWillFinishLaunching");
    NSDictionary *defaultsDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Mathias Mustermann", @"autor", 
                                  @"mathias", @"benutzer", 
                                  @"passwort",@"passwort", nil];
    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict];
}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    [moc commitEditing];
    if ([moc hasChanges]) {
        NSLog(@"Save needed!");
        [moc save:nil];
    }
    return NSTerminateNow;
}

- (NSString *)autor{
    return [[NSUserDefaults standardUserDefaults] stringForKey:@"autor"];
}

- (void)windowDidBecomeKey:(NSNotification *)notification
{
    NSLog(@"windowDidBecomeKey");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [autorPrefFeld setStringValue:[defaults stringForKey:@"autor"]];
    [benutzerPrefFeld setStringValue:[defaults stringForKey:@"benutzer"]];
    [passwortPrefFeld setStringValue:[defaults stringForKey:@"passwort"]];
}

- (void)windowDidResignKey:(NSNotification *)notification
{
    NSLog(@"windowDidResignKey");
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[autorPrefFeld stringValue] forKey:@"autor"];
    [defaults setObject:[benutzerPrefFeld stringValue] forKey:@"benutzer"];
    [defaults setObject:[passwortPrefFeld stringValue] forKey:@"passwort"];
    [defaults synchronize];    
}

- (NSManagedObjectModel *)managedObjectModel
{
    if(objectModel){
        return objectModel;
    }
    objectModel= [NSManagedObjectModel mergedModelFromBundles:nil];
    return objectModel; 
}

- (NSString *)applicationSupportFolder
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
    return [basePath stringByAppendingPathComponent:@"WeblogClient"];
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (storeCoord) {
        return storeCoord;
    }

    NSFileManager *fileManager;
    NSString *applicationSupportFolder;
    NSURL *url;

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                             [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

    fileManager = [NSFileManager defaultManager];
    applicationSupportFolder = self.applicationSupportFolder;
    if (![fileManager fileExistsAtPath:applicationSupportFolder]) {
        [fileManager createDirectoryAtPath:applicationSupportFolder withIntermediateDirectories:NO attributes:nil error:nil];
    }
    url = [NSURL fileURLWithPath:[applicationSupportFolder stringByAppendingPathComponent:@"WeblogClient.xml"]];
    storeCoord = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
    [storeCoord addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:options error:nil];
    return storeCoord;                 
}



- (NSManagedObjectContext *)managedObjectContext
{
    if (moc) {
        return moc;
    }
    NSPersistentStoreCoordinator *coordinator = self.persistentStoreCoordinator;
    if (coordinator) {
        moc = [NSManagedObjectContext new];
        [moc setPersistentStoreCoordinator:coordinator];
    }
    return moc;
}

@end
like image 797
MJB Avatar asked Aug 04 '11 21:08

MJB


1 Answers

Not sure if you fixed your error, but check out: I keep on getting "save operation failure" after any change on my XCode Data Model

Also for me, I had the same storecordinator sqlite name as another project I was working on and had already ran...

like image 133
Tim Maxey Avatar answered Nov 05 '22 13:11

Tim Maxey