• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

YTBaseDBManager: IOS数据库升级数据数据迁移的解决方案

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

YTBaseDBManager

开源软件地址:

https://gitee.com/dhar/YTBaseDBManager

开源软件介绍:

YTBaseDBManager

IOS数据库升级数据数据迁移的解决方案

原理分析

IOS 数据库升级数据迁移解决方案

安装

使用 Pod 导入,因为是开发库,所以需要指定 :path 参数

pod 'YTBaseDBManager', :path => '../'

使用

客户端使用的DEMO代码如下

  1. 客户端使用方法 [self setDBFilePath:DBPath newDBVersion:DB_Version]; 设置数据库路径
  2. 客户端重写模板方法 initTables 执行创建表的逻辑
  3. 底层库会自动分析新表和旧表,自动进行数据迁移的操作
/** 数据库保存的缓存目录 */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

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Android-CustomRatingBar: 一个自定义的RatingBar发布时间:2022-03-24
下一篇:
DBExecutor: android 数据库框架,sqlite database发布时间:2022-03-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap