17

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
MJB
  • 3,934
  • 10
  • 48
  • 72

1 Answers1

17

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...

Community
  • 1
  • 1
Tim Maxey
  • 749
  • 6
  • 13
  • 10
    Had a similar problem. Just had to delete and reinstall. It was conflicting with earlier versions of the database. – Will Larche Feb 08 '12 at 02:22
  • Just had to delete and reinstall. It was conflicting with earlier versions of the database. - Its not a proper solution. - Needs to be make Migration for that. - Follow this http://www.raywenderlich.com/27657/how-to-perform-a-lightweight-core-data-migration link for migration. – Tarang Jul 15 '15 at 13:39