macos 不断收到“此 NSPersistentStoreCoordinator 没有持久存储。它无法执行保存操作。” 关闭我的应用程序 (OSX)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6948509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Keep getting "This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation." upon closing my app (OSX)
提问by MJB
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..
我已经检查了互联网和 stackOverflow 上的几篇文章,但到目前为止还没有找到答案。这是我的 AppDelegate,据我所知,这些实现是非常标准的..我只是添加了以下行并传递了参数,但它没有帮助..
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...
我清理了我的项目,这也没有帮助。也不会创建 ApplicationSupport 文件夹。这可能是问题的原因吗?我没有使用“使用核心数据”选项创建应用程序,但我提供了必要的方法......
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator{...} is at the bottom!
-(NSPersistentStoreCoordinator *)persistentStoreCoordinator{...} 在底部!
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
采纳答案by Tim Maxey
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
不确定您是否修复了错误,但请查看:在我的 XCode 数据模型发生任何更改后,我不断收到“保存操作失败”
Also for me, I had the same storecordinator sqlite name as another project I was working on and had already ran...
同样对我来说,我的 storecordinator sqlite 名称与我正在从事的另一个项目相同,并且已经运行......