Fetch the repository succeeded.
IOS数据库升级数据数据迁移的解决方案
使用 Pod 导入,因为是开发库,所以需要指定 :path 参数
pod 'YTBaseDBManager', :path => '../'
客户端使用的DEMO代码如下
[self setDBFilePath:DBPath newDBVersion:DB_Version];
设置数据库路径initTables
执行创建表的逻辑/** 数据库保存的缓存目录 */
static NSString* kDBCache = @"DBCache";
/** 数据库文件名称 */
static NSString* DB_NAME = @"YTDB.sqlite";
/** 当前使用的数据库版本,程序会根据版本号的改变升级数据库以及迁移旧的数据 */
static NSString* DB_Version = @"1.0.1";
@implementation YTBusinessDBManager
// 子类的单例
+ (instancetype)sharedInstance{
static id instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
// 创建数据库文件
NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *DBDir = [cachePath stringByAppendingPathComponent:kDBCache];
BOOL isDir = NO;
if (!([[NSFileManager defaultManager] fileExistsAtPath:DBDir isDirectory:&isDir] && isDir)) {
[[NSFileManager defaultManager] createDirectoryAtPath:DBDir withIntermediateDirectories :YES attributes :nil error :nil];
}
NSString* DBPath = [DBDir stringByAppendingPathComponent:DB_NAME];
// 设置数据库路径,包含了数据库升级的逻辑
__weak __typeof(self)weakSelf = self;
[[YTBaseDBManager sharedInstance] startUpgradeDBWithPath:DBPath newDBVersion:DB_Version createTableBlock:^{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf initTables];
}];
}
return self;
}
// 初始化数据表
- (void)initTables {
// TODO: 在这里做初始化表的操作
[VideoUploadModel createTableIfNotExists];
}
@end
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
Activity
Community
Health
Trend
Influence
:Code submit frequency
:React/respond to issue & PR etc.
:Well-balanced team members and collaboration
:Recent popularity of project
:Star counts, download counts etc.