在主包中更新只读Core Data sqlite [英] Update read-only Core Data sqlite in main bundle

查看:130
本文介绍了在主包中更新只读Core Data sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的只是Core Data sqlite从主包,工作得很好。当我添加一个新版本的数据库(更多的只读数据)到主包,它仍然读取数据库的旧版本。

I am using a read-only Core Data sqlite from the Main Bundle, works well. When i add a new version of the database (more read-only data) to the main bundle it still reads the "Old" version of the database.

任何人帮助我了解为什么,当当前用户使用新版本的数据库下载更新时,如何获取新的数据库版本?

Anyone that can help me understand why and what to do to get the new database version the current one when a current user download an update with the new version of the database?

这是部分尝试解决此帖中的问题:从文档目录访问更新的数据库时出现同样的问题

This is part of trying to solve the problem in this post: Same problem when accessing updated database from documents directory

===解决方案====

===SOLUTION====

我通过在新主包中更改新数据库的名称来解决这个问题,它的工作原理就像一个梦想。此外,如果这是一个更新,我删除文件目录中的旧数据库进行清理。

I solved this by changing the name of the new database in the "new" main bundle and it works like a dream. Also, if this is an update i delete the old database in the documents directory to clean up.

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}


//===READ DATABASE FROM MAIN BUNDLE===//
NSFileManager *fileManager = [NSFileManager defaultManager];

NSURL *storeUrl = [[NSBundle mainBundle] URLForResource:kNewDB withExtension:@"sqlite"];

//=== IF THE OLD DATABASE STILL EXIST DELETE IT FROM DOCUMENT DIRECTORY ===//
NSURL *oldDatabasePathURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];
NSString *oldDatabasePath = [oldDatabasePathURL path];
if ([fileManager fileExistsAtPath:oldDatabasePath]) {
    //Remove old database from Documents Directory
    [fileManager removeItemAtURL:oldDatabasePathURL error:nil];

}

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

NSError *error;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
    // Update to handle the error appropriately.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);  // Fail
}    

return persistentStoreCoordinator;
}


推荐答案

您的代码,您在其中检查数据库文件的副本是否存在于某个可写目录(可能是您的文档目录)中,如果没有,则将其复制到那里。当你需要更改数据库时,这是一个非常常见的方法。问题是,当您更新应用程序时,文件已存在,因此不会重复。

You must have a place in your code where you check to see if a copy of the database file exists in some writable directory (possibly your Documents directory) and if not, then you copy it there. This is a very common approach to take when you need to make changes to your database. The problem is, when you update your app, the file already exists, so it is never copied over again.

有两种方法可以解决您的问题: / p>

There are two approaches to take to fix your problem:


  1. (首选):不要首先复制数据库。由于它是只读的,你不需要,它只是占用了额外的空间在设备上。

  1. (Preferable): Don't copy the database in the first place. Since it is read only, you don't need to, and it just takes up extra space on the device. Simply open the database using the path of the file that is in the main bundle.

只需打开数据库,而不是检查文件是否存在在可写目录中,检查它是否比主包中的更新。 (不是使用日期,因为他们可以安装程序,并在更新提交到应用商店后批准,这将导致新的没有被复制的文件创建),你需要检查的版本数据库,可能通过在您的应用程序包中存储另一个文件,存储版本信息,或使用版本特定代码确定它)。如果没有,请重新复制。

Instead of checking to see if a file exists in the writable directory, check to see if it is newer than the one in the main bundle. (not by using the date, since they could have installed the program and created the file after your update was submitted to the app store for approval, which would result in the new one not being copied over. You need to check the version of the database, possibly by storing another file in your app bundle which stores the version info, or determining it with version specific code). If not, then copy it over again.

这篇关于在主包中更新只读Core Data sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆