CoreData,UIManagedDocument和空的持久存储

我正在使用UIManagedDocument读取和写入CoreData 。 我有Document类。 这是一些教程的文档:

。H

 #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> typedef void (^OnDocumentReady) (UIManagedDocument *document); @interface Document : NSObject @property (strong, nonatomic) UIManagedDocument *document; + (Document *)sharedDocument; - (void)performWithDocument:(OnDocumentReady)onDocumentReady; @end 

.M

 @interface Document () - (void)objectsDidChange:(NSNotification *)notification; - (void)contextDidSave:(NSNotification *)notification; @end; @implementation Document @synthesize document = _document; static Document*_sharedInstance; + (Document *)sharedDocument { static dispatch_once_t once; dispatch_once(&once, ^{ _sharedInstance = [[self alloc] init]; }); return _sharedInstance; } - (id)init { self = [super init]; if (self) { NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"Document"]; self.document = [[UIManagedDocument alloc] initWithFileURL:url]; NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; self.document.persistentStoreOptions = options; // Register for notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectsDidChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:self.document.managedObjectContext]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:self.document.managedObjectContext]; } return self; } - (void)performWithDocument:(OnDocumentReady)onDocumentReady { void (^OnDocumentDidLoad)(BOOL) = ^(BOOL success) { onDocumentReady(self.document); }; if (![[NSFileManager defaultManager] fileExistsAtPath:[self.document.fileURL path]]) { [self.document saveToURL:self.document.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:OnDocumentDidLoad]; } else if (self.document.documentState == UIDocumentStateClosed) { [self.document openWithCompletionHandler:OnDocumentDidLoad]; } else if (self.document.documentState == UIDocumentStateNormal) { OnDocumentDidLoad(YES); } } - (void)objectsDidChange:(NSNotification *)notification { #ifdef DEBUG NSLog(@"NSManagedObjects did change."); #endif } - (void)contextDidSave:(NSNotification *)notification { #ifdef DEBUG NSLog(@"NSManagedContext did save."); #endif } @end 

然后在ViewController我有NSURLConnection

 -(void)connectionDidFinishLoading:(NSURLConnection *)connection { DataParser *parser = [[DataParser alloc] init]; [parser startParsingData:self.myMutableData withContext:self.moc]; } 

我在这里打开文档:

 -(void)initDocument { if (!self.moc) { [[Document sharedDocument] performWithDocument:^(UIManagedDocument *document) { self.moc = document.managedObjectContext; [[NSNotificationCenter defaultCenter] postNotificationName:UIDocumentStateChangedNotification object:self]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"someURL"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; [NSURLConnection connectionWithRequest:request delegate:self]; }]; } } 

然后我尝试parsing数据:

 -(void)startParsingData:(NSData*)data withContext:(NSManagedObjectContext*)context { self.moc = context; self.startDate = [NSDate date]; NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; parser.delegate = self; [parser parse]; } 

ant尝试加载它在XMLparsing后的核心数据:

 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if([elementName isEqualToString:@"Item"]) { [self.moc performBlockAndWait:^{ TestEntity *te = [NSEntityDescription insertNewObjectForEntityForName:@"TestEntity" inManagedObjectContext:self.moc]; te.surname = @"5433fds"; te.name = @"5342fdsfsd"; }]; } } 

我看到日志:

 2014-01-24 16:21:21.692 Ce[85149:70b] NSManagedObjects did change. 2014-01-24 16:21:36.696 Ce[85149:70b] NSManagedContext did save. 

所以我认为这应该是在sqlite文件,但是当我尝试阅读它是完全空的。 awl文件有一些较大的大小,但persistentStore有0行。 为什么?

当你第二次启动应用程序会发生什么,你能看到你的应用程序中的数据?

在iOS 7中,SQLite现在打开了日志function。 我不知道是否导致混乱(数据在那里,但没有在sqlite文件呢)。

我也会质疑你为什么使用UIManagedDocument 。 看起来你正在使用Singleton模式的核心数据(这是坏的),然后使用UIManagedDocument在单身模式(更糟糕)。

UIManagedDocument用于基于文档的应用程序。 如果你build立了一个标准的核心数据栈,那么你很可能会很容易地清除该类中的一些“自动化”特性。

您可以考虑closures日志function,为您的NSPersistentStoreCoordinator添加一个真空选项或切换到标准的核心数据堆栈。

这很可能是因为iOS 7中使用了新的SQLite日记模式。如果将其设置为旧模式(如以下SO问题的答案中所示),则应该再次看到您的数据。

如何禁用WAL日志模式