iOS开发WDCB使用教程

WCDB

WCDB是一种在WeChat应用程序中使用的高效,完整,易于使用的移动数据库框架。它目前在iOS,MacOS和Android上可用。

前提条件

  • 使用WCDB的应用程序可以定位到:iOS 7或更高版本,macOS 10.9或更高版本。
  • 需要Xcode 8.0或更高版本。
  • 需要Objective-C ++。

通过CocoaPods安装

  • 在Podfile文件中添加 pod 'WCDB'
  • 运行 pod install
  • 添加#import <WCDB/WCDB.h>Objective-C ++源文件的顶部
  • WCDB是一个Objective-C ++框架,对于项目中包含WCDB的那些文件,您应该将其扩展名重命名 .m.mm

开发实战

  • 步骤一:定义一个常用宏
    #define TTDBPath [[UIApplication sharedApplication].documentsPath stringByAppendingPathComponent:@"ToDayDB"]

  • 步骤二:定义一个Model

CBFindModel.h

#import <Foundation/Foundation.h>
#import <WCDB/WCDB.h>
#import <WCDB/WCTMaster+WCTTableCoding.h>
@interface CBFindModel : NSObject <WCTTableCoding>

@property (nonatomic, copy) NSString *tag_id;
@property (nonatomic, copy) NSString *tag_name;
@property (nonatomic, copy) NSString *tag_icon;
@property (nonatomic, copy) NSString *tag_description;
@property (nonatomic, copy) NSString *tag_date;

WCDB_PROPERTY(tag_date);
WCDB_PROPERTY(tag_name);

@end

CBFindModel.m

#import "CBFindModel.h"
@implementation CBFindModel

WCDB_IMPLEMENTATION(CBFindModel)
WCDB_SYNTHESIZE(CBFindModel, tag_id)
WCDB_SYNTHESIZE(CBFindModel, tag_name)
WCDB_SYNTHESIZE(CBFindModel, tag_icon)
WCDB_SYNTHESIZE(CBFindModel, tag_description)
WCDB_SYNTHESIZE(CBFindModel, tag_date)
WCDB_PRIMARY(CBFindModel, tag_id)

/// 自定义主键的对应的属性 (需要是属性的全名)
+ (NSString *)jr_customPrimarykey {
    return @"tag_id";
}

@end
  • 步骤三:定义一个实例DBHelper存放Model

DBHelper.h

#import <Foundation/Foundation.h>

@class WCTDatabase;
@interface DBHelper : NSObject

+(instancetype)defaultDB;

@property(nonatomic,strong) WCTDatabase *feedNewsDB;    // 新闻流

@end

DBHelper.m

#import "DBHelper.h"
#import "CBFindModel.h"
#import <WCDB.h>

@implementation DBHelper

+(instancetype)defaultDB
{
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    
    static DBHelper *_instance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        _instance = [[self alloc] init];

        // 1.发现标签缓存
        NSString *path4 = [TTDBPath stringByAppendingPathComponent:TTFindCategory];
        _instance.feedNewsDB = [[WCTDatabase alloc] initWithPath:path];
        BOOL ret4 = [_instance.feedNewsDB createTableAndIndexesOfName:TTFindCategory withClass:CBFindModel.class];
    });
    return _instance;
}
  • 步骤四:插入一条数据
CBFindModel *model = [CBFindModel new];
model.tag_id = @"xxxxx";// 数据

BOOL result=[[DBHelper defaultDB].categoryDB insertOrReplaceObjects:@[model] into:TTFindCategory];
  • 步骤五:获取一条数据
NSArray<CBFindModel *> *datas=[[DBHelper defaultDB].categoryDB getObjectsOfClass:CBFindModel.class fromTable:TTFindCategory limit:20];
  • 步骤六:根据tag_date条件获取数据
NSArray<CBFindModel *> *datas =[[DBHelper defaultDB].feedCacheDB getObjectsOfClass:[CBFindModel class]
                                                                               fromTable:TTFindCategory
                                                                                 CBFindModel.tag_date.order()
                                                                                   limit:50];

参考资料

WCDB技术探讨QQ群:595235056

梦想让我与众不同,奋斗让我改变命运 !

☝☝☝阿里云活动一:点我领取阿里云1800元代金券大礼包 !!!
☝☝☝阿里云活动二:企业级高性能实例,限时2-5折 !!!
☝☝☝阿里云活动三:云·企业官网定制,买就送安全证书 !!!
☝☝☝阿里云活动四:【超高返现】购物车全产品返25% !!!
☝☝☝阿里云活动五:【商标注册服务】低至300元 !!!

《iOS开发WDCB使用教程》

    原文作者:dreamahui
    原文地址: https://www.jianshu.com/p/bf16ac930e4e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞