简体   繁体   English

无法在IOS5中使用核心数据库

[英]Can't use core database in IOS5

I am working with a core database, it is working in IOS 6 but when I trying to test it on IOS 5. It does not do anything. 我正在使用核心数据库,它在IOS 6中正常工作,但是当我尝试在IOS 5上对其进行测试时,它没有任何作用。 Let me explain what I'm doing. 让我解释一下我在做什么。

First I do this in my viewWillAppear. 首先,我在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");
    }

}

Then it comes in the UseDocument method. 然后它进入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];
    }

}

And finally it goes into the setDatabase Method. 最后,它进入setDatabase方法。

- (void)setGenkDatabase:(UIManagedDocument *)genkDatabase
{
    if (_genkDatabase != genkDatabase) {
        _genkDatabase = genkDatabase;
        [self useDocument];
    }
    NSLog(@"Comes in the setdatabase methode.");
}

Doing all this gives the following log. 完成所有操作后,将显示以下日志。

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

Like you can see it does not print the create log in my use document. 就像您看到的那样,它不会在我的使用文档中打印创建日志。 So it isn't able to execute the method FetchDataIntoDocument . 因此,它无法执行FetchDataIntoDocument方法。

Can anybody help me with this problem. 任何人都可以帮助我解决这个问题。 I am searching at this problem for ages for now. 我正在寻找这个问题很久了。

Many thanks in advace. 非常感谢。

Stef 燕姿

Are you testing with the simulator or on the device? 您正在使用模拟器进行测试还是在设备上进行测试? The simulator is case-insensitive - the device is case-sensitive on file access, remember that! 模拟器不区分大小写-设备在文件访问时区分大小写,请记住!

But beside from that the documentation for NSFileManager recommends not checking to see if files exist, and instead just trying to read the file and handle any errors gracefully (eg file not found error). 但是除此之外NSFileManager的文档建议不要检查文件是否存在,而只是尝试读取文件并优雅地处理所有错误(例如,找不到文件错误)。 So just try loading the file instead of checking to see if it exists. 因此,只需尝试加载文件,而不要检查文件是否存在。

And i don't see any file extension for your database file! 而且我没有看到您的数据库文件的任何文件扩展名! It just states ""Default appGenk Database2". Is this already the file name or jut another subdirectory so far? 它只是说“默认的appGenk Database2”。到目前为止,这已经是文件名还是突出了另一个子目录?

EDIT: 编辑:

You can try the following code: 您可以尝试以下代码:

- (void) setupStore {
    NSString* storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"mydatabase.sqlite"];

    // Set up the store.
    NSFileManager* fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, create one.
    if (![fileManager fileExistsAtPath: storePath]) {
        // create your store here!
    }
}


- (NSString*) applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

"mydatabase.sqlite" is the name of the database-file you expect to be present in your documents directory. “ mydatabase.sqlite”是您希望出现在文档目录中的数据库文件的名称。

If you would like to see a full-fledged example on how to set-up a core data persistent store you could check out apples own iPhoneCoreDataRecipes example. 如果您想查看有关如何设置核心数据持久性存储的完整示例,可以查看苹果自己的iPhoneCoreDataRecipes示例。 Just take a look at the 只是看看

RecipesAppDelegate.m RecipesAppDelegate.m

implementation. 实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM