WCDB使用

Git地址:https://github.com/Tencent/wcdb/wiki/ORM%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B#wctcolumncoding%E6%96%87%E4%BB%B6%E6%A8%A1%E7%89%88

WCTColumnCoding文件模版

为了简化定义,WCDB提供了Xcode文件模版来创建类字段绑定。

  1. 首先需要安装文件模版。

    • 未获取 WCDB 的 Github 仓库的开发者,可以在命令执行 curl https://raw.githubusercontent.com/Tencent/wcdb/master/tools/templates/install.sh -s | sh
    • 已获取 WCDB 的 Github 仓库的开发者,可以手动运行 cd path-to-your-wcdb-dir/tools/templates; sh install.sh; 手动安装 文件模版。
  2. 安装完成后重启Xcode,选择新建文件,滚到窗口底部,即可看到对应的文件模版。


    截屏2020-12-24 下午9.22.01.png

注意:引入WCDB需要以.mm结尾

DBManage文件



#import 
#import "MessageHH.h"
#import "MessageHH+WCTTableCoding.h"
#import "MessageListHH.h"
#import "MessageListHH+WCTTableCoding.h"
NS_ASSUME_NONNULL_BEGIN

@interface DBManager : NSObject

SingletonH(DBManager)
- (WCTDatabase *)createDataBase ;
- (void)insertMessageListToDB:(MessageListHH *)messagelist;

- (void)insertMessageToDB:(MessageHH *)message;
- (NSArray *)getMessageByChatId:(NSString *)chatId;

- (NSArray *)getAllMessageListData;
- (NSArray *)getMessageListData:(NSString *) uid;

@end
#import "DBManager.h"
#import 
@interface DBManager ()

@property (nonatomic,strong) WCTDatabase *database;

@end

@implementation DBManager
SingletonM(DBManager)
- (WCTDatabase *)createDataBase {
    if (_database) {
        return _database;
    }
    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *path = [NSString stringWithFormat:@"%@/chatDB",docDir];//注意,此处数据库名字不要带sqlite的后缀名,恢复数据库的时候如果有这个后缀名,会造成无法恢复的情况。这是一个坑啊,大家千万要注意
    NSLog(@"DB Path %@",path);
    _database = [[WCTDatabase alloc] initWithPath:path];
    /*
     CREATE TABLE messsage (localID INTEGER PRIMARY KEY,
     content TEXT,
     createTime BLOB,
     modifiedTime BLOB)
     */
    BOOL result = [_database createTableAndIndexesOfName:NSStringFromClass(MessageHH.class)
                                              withClass:MessageHH.class];
    BOOL resultList = [_database createTableAndIndexesOfName:NSStringFromClass(MessageListHH.class)
                                 withClass:MessageListHH.class];
    if (result && resultList) {
        return _database;
    }
    return nil;
}

- (void)insertMessageToDB:(MessageHH *)message {
    
    BOOL result = [self.database insertObject:message into:NSStringFromClass(MessageHH.class)];
    NSLog(@"insertMessageToDB:%@",@(result));
    
}

- (NSArray *)getMessageByChatId:(NSString *)chatId {

    NSArray *ary = [self.database getObjectsOfClass:MessageHH.class fromTable:NSStringFromClass(MessageHH.class) where:MessageHH.chatId == chatId];
    return ary;
}


- (void)insertMessageListToDB:(MessageListHH *)messagelist {
    BOOL result = [self.database insertOrReplaceObject:messagelist into:NSStringFromClass(MessageListHH.class)];
    NSLog(@"insertMessageListToDB:%@",@(result));
}

- (NSArray *)getAllMessageListData {
    NSArray *ary = [self.database getAllObjectsOfClass:MessageListHH.class fromTable:NSStringFromClass(MessageListHH.class)];

    return ary;
}

- (NSArray *)getMessageListData:(NSString *) uid {
    NSArray *ary = [self.database getObjectsOfClass:MessageListHH.class fromTable:NSStringFromClass(MessageListHH.class) where:MessageListHH.fromId == uid || MessageListHH.toId == uid];
    
    return ary;
}

messageList文件


@interface MessageListHH : NSObject

@property(nonatomic, copy) NSString *listIdentifier;//列表标识
@property(nonatomic, copy) NSString *fromId;//发送方ID
@property(nonatomic, copy) NSString *message;//聊天内容
@property(nonatomic, copy) NSString *room;//聊天房间号
@property(nonatomic, copy) NSString *type;//消息类型
@property(nonatomic, copy) NSString *toId;//接受方ID
@property(nonatomic, copy) NSString *name;
@property(nonatomic, copy) NSString *talker;
@property(nonatomic, copy) NSString *avatar;
@property(nonatomic, copy) NSString *nameRemark;
@property(nonatomic, copy) NSString *timeStr;
@property(nonatomic, strong) NSDate *createTime;
@property(nonatomic, assign) NSInteger is_send;
@property(nonatomic, assign) NSInteger unReadCount;//未读条数
@end

#import "MessageListHH.h"
#import 

@implementation MessageListHH

WCDB_IMPLEMENTATION(MessageListHH)
WCDB_SYNTHESIZE(MessageListHH, listIdentifier)
WCDB_SYNTHESIZE(MessageListHH, fromId)
WCDB_SYNTHESIZE(MessageListHH, message)
WCDB_SYNTHESIZE(MessageListHH, room)
WCDB_SYNTHESIZE(MessageListHH, type)
WCDB_SYNTHESIZE(MessageListHH, toId)
WCDB_SYNTHESIZE(MessageListHH, name)
WCDB_SYNTHESIZE(MessageListHH, talker)
WCDB_SYNTHESIZE(MessageListHH, avatar)
WCDB_SYNTHESIZE(MessageListHH, nameRemark)
WCDB_SYNTHESIZE(MessageListHH, createTime)
WCDB_SYNTHESIZE(MessageListHH, is_send)
WCDB_SYNTHESIZE(MessageListHH, unReadCount)

WCDB_PRIMARY(MessageListHH, listIdentifier) //主键

@end
#import "MessageListHH.h"
#import 

@interface MessageListHH (WCTTableCoding) 

WCDB_PROPERTY(listIdentifier)
WCDB_PROPERTY(fromId)
WCDB_PROPERTY(message)
WCDB_PROPERTY(room)
WCDB_PROPERTY(type)
WCDB_PROPERTY(toId)
WCDB_PROPERTY(name)
WCDB_PROPERTY(talker)
WCDB_PROPERTY(avatar)
WCDB_PROPERTY(nameRemark)
WCDB_PROPERTY(createTime)
WCDB_PROPERTY(is_send)
WCDB_PROPERTY(unReadCount)

@end

你可能感兴趣的:(WCDB使用)