- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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];
}];
}
}
然后我尝试解析数据:
-(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 尝试在 XML
解析后将其加载到核心数据中:
-(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 现在启用了日志功能。我想知道日志记录是否引起了困惑(数据存在但还没有在 sqlite 文件中)。
我还会质疑您为什么使用 UIManagedDocument
。看来您在单例模式中使用核心数据(这很糟糕),然后在该单例模式中使用 UIManagedDocument
(更糟)。
UIManagedDocument
适用于基于文档的应用程序。您可能会违反该类中的一些“自动化”功能,如果您改为构建标准的 Core Data 堆栈,这些功能很容易清除。
您可能会考虑关闭 Journaling,为您的 NSPersistentStoreCoordinator
添加 vacuum 选项或切换到标准的 Core Data 堆栈。
关于ios - CoreData、UIManagedDocument 和空持久存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21336404/
我一直在阅读苹果文档,但仍然有一个问题,我找不到答案。我有一个 UIManagedDocument 对象,它有两个嵌套上下文 - 主线程上的子上下文和私有(private)线程上的父上下文。接下来,我
我已经看过 How do I create a global UIManagedDocument instance per document-on-disk shared by my whole ap
在UIManagedDocument的文档中简要提到了以下内容: 为了支持异步数据写入,Core Data实际上使用一对嵌套的托管对象上下文。 是特定于UIManagedDocument还是Core
所以我将核心数据迁移到UIManagedDocument。 在iPhone模拟器上调用openWithCompletionHandler可以正常工作,但是当我尝试在真实设备上运行项目时,它始终返回NO
我有一个连接到 NSFetchedResultsController 的 TableView ,相应的 managedObjectContext 是 self.myDatabase.managedOb
好吧,假设我正在制作一个观鸟应用。 有一个“官方”鸟类数据库。它存储在一个 UIManagedDocument 中。它用于将所有鸟类填充到 UITableView 中,并使用图片和数据为每只鸟类提供详
我正在尝试使用 Core Data 制作一个 iPhone 应用程序。我必须使用 NSManagedObjectContext 来访问数据,为此我使用了 UIManagedDocument。但是,如果
我的应用使用核心数据 SQLite 数据库。我想让我的用户使用 iCloud 在设备之间同步它 - 我想我可以使用 UIManagedDocument。 我按照 Apple 的文档对其进行了子类化,并
我正在使用这个博客:http://www.adevelopingstory.com/blog/2012/03/core-data-with-a-single-shared-uimanageddocum
我找到了这些斯坦福教程 https://itunes.apple.com/us/course/ipad-iphone-app-development/id495052415 ,一直在听有关核心数据的讲
我正在开发一个使用 UIManagedDocument 子类的 iPhone 应用程序并将其文档存储在 iCloud 上。 一切正常,直到我更改了我的核心数据模型/方案(添加了一个新的模型版本 - 就
首先,我应该提到这是我在这个网站上的第一篇文章。我正在尝试自学 iOS 编程,在谷歌搜索答案时,我发现我经常被引导到这里。所以感谢所有在这里做出贡献的人。你已经帮了我很多了。 我一直在参加斯坦福大学
在斯坦福大学 CS193P 类(class)中,我开始在我的应用程序中使用 CoreData,该类(class)涉及 iOS 5 新类 UIManagedDocument 的使用。该方法本身非常简单,
我的应用程序中有一项功能,用户可以通过单击按钮重置应用程序上的所有内容。此时,我没有尝试删除所有核心数据关系(级联删除)和其他并发症,而是决定使用这段代码实际删除整个 UIManagedDocumen
我设置了异常断点,当我调用时- (void)saveToURL:(NSURL *)url forSaveOperation:(UIDocumentSaveOperation)saveOperation
我正在使用 UIManagedDocument 读取和写入 CoreData。我有 Document 类。这是一些教程中的文档: .h #import #import typedef void (
最近,我在 iOS 中使用一个时间表应用程序,我在使用 Core Data 时遇到了问题。 我的应用程序有一个主用户界面,有点像 Apple 创建的原始日历应用程序,我将所有事件数据保存在 Core
我在使用 UIManagedDocument 保存实体时遇到问题。我有一个 NSFetchedResultsController,其上下文设置为 UIManagedDocuments 上下文。我在 C
如标题中所述,我想同步打开 UIManagedDocument,即,我希望我的执行等到打开完成。我仅在 mainThread 上打开文档。 当前要打开的 API 使用 block [UIManaged
我一直在关注 CS193p 关于核心数据的讲座,但在插入新的托管对象时遇到了问题。 错误是: Terminating app due to uncaught exception 'NSInternal
我是一名优秀的程序员,十分优秀!