我正在使用一个核心数据库,它在 IOS 6 中运行,但是当我尝试在 IOS 5 上对其进行测试时。它什么也没做。让我解释一下我在做什么。
首先,我在 viewWillAppear 中执行此操作。
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"view appeared");
[super viewWillAppear:animated];
if (!self.genkDatabase) {
NSLog(@"comes to here");
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Default appGenk Database2"];
// url is now "<Documents Directory>/Default Photo Database"
self.genkDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; // setter will create this for us on disk
NSLog(@"database created on disk");
}
}
然后它出现在 UseDocument 方法中。
- (void)useDocument
{
NSLog(@"Comses in the use document");
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.genkDatabase.fileURL path]]) {
// does not exist on disk, so create it
[self.genkDatabase saveToURL:self.genkDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
NSLog(@"create");
[self setupFetchedResultsController];
[self fetchGenkDataIntoDocument:self.genkDatabase];
}];
} else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
NSLog(@"closed news");
// exists on disk, but we need to open it
[self.genkDatabase openWithCompletionHandler:^(BOOL success) {
[self setupFetchedResultsController];
}];
} else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
NSLog(@"normal");
// already open and ready to use
[self setupFetchedResultsController];
}
}
最后进入 setDatabase 方法。
- (void)setGenkDatabase:(UIManagedDocument *)genkDatabase
{
if (_genkDatabase != genkDatabase) {
_genkDatabase = genkDatabase;
[self useDocument];
}
NSLog(@"Comes in the setdatabase methode.");
}
执行所有这些操作会给出以下日志。
2012-10-22 10:42:47.444 RacingGenk[4786:c07] view appeared
2012-10-22 10:42:47.445 RacingGenk[4786:c07] comes to here
2012-10-22 10:42:47.459 RacingGenk[4786:c07] Comses in the use document
2012-10-22 10:42:47.460 RacingGenk[4786:c07] Comes in the setdatabase methode.
2012-10-22 10:42:47.461 RacingGenk[4786:c07] database created on disk
如您所见,它不会在我的使用文档中打印创建日志。因此它无法执行FetchDataIntoDocument方法。
任何人都可以帮我解决这个问题。我现在一直在寻找这个问题。
非常感谢。
史蒂夫