数据库存储
数据库存储基于WCDB进行了封装,可以简洁地使用各种数据库基础功能。
首先定义一个TestDBModel测试类:
//
// TestDBModel.h
// TMFStorage
//
// Created by xxx on 2022/2/10.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface TestDBModel : NSObject
@property (nonatomic, copy) NSString *key;//主键
@property (nonatomic, copy, nullable) NSString *test;
@end
NS_ASSUME_NONNULL_END
//
// TestDBModel.mm
// TMFStorage
//
// Created by xxx on 2022/2/10.
//
#import "TestDBModel.h"
#import <WCDB/WCDB.h>
#import "TMFStorage.h"
@interface TestDBModel()<TMFStorageDBModelProtocol>
@end
@implementation TestDBModel
WCDB_IMPLEMENTATION(TestDBModel)
WCDB_SYNTHESIZE(TestDBModel, key)
WCDB_SYNTHESIZE(TestDBModel, test)
WCDB_PRIMARY(TestDBModel, key)
//如果需要使用- (BOOL)insertOrReplaceObjects:(NSArray<WCTObject *> *)objects withProperties:(NSArray *)properties into:(NSString *)tableName 方法, 则必须实现 TMFStorageDBModelProtocol
+ (NSArray *)primaryKeys {
return @[@"key"];
}
- (NSString *)description {
return [NSString stringWithFormat:@"TestDBModel: key = %@, test = %@",self.key,self.test];
}
@end
//
// TestDBModel+WCTTableCoding.h
// TMFStorage
//
// Created by xxx on 2022/2/10.
//
#import "TestDBModel.h"
#import <WCDB/WCDB.h>
NS_ASSUME_NONNULL_BEGIN
@interface TestDBModel (WCTTableCoding) <WCTTableCoding>
WCDB_PROPERTY(key)
WCDB_PROPERTY(test)
@end
NS_ASSUME_NONNULL_END
//
// TMFStorageDBContext+Test.h
// TMFDemo
//
// Created by xxx on 2022/2/15.
// Copyright © 2022 Tencent. All rights reserved.
//
#import "TMFStorageDBContext.h"
NS_ASSUME_NONNULL_BEGIN
@class TestDBModel;
@interface TMFStorageDBContext (Test)
- (BOOL)deleteModelWithKey:(NSString *)key;
- (TestDBModel *)selectTestModelWithTest:(NSString *)test;
@end
NS_ASSUME_NONNULL_END
//
// TMFStorageDBContext+Test.m
// TMFDemo
//
// Created by xxx on 2022/2/15.
// Copyright © 2022 Tencent. All rights reserved.
//
#import "TMFStorageDBContext+Test.h"
#import "TMFStorageDBContext+Private.h" //导入此文件才能访问到 .database
#import "WCTDatabase+TMFDatabase.h" //导入此文件才能访问 deleteObjectsFromClass
#import "TestDBModel+WCTTableCoding.h" //导入此文件才能访问 TestDBModel.key
@implementation TMFStorageDBContext (Test)
- (BOOL)deleteModelWithKey:(NSString *)key {
BOOL result = [self.database deleteObjectsFromClass:TestDBModel.class where:TestDBModel.key.in(@[key])];
return result;
}
- (TestDBModel *)selectTestModelWithTest:(NSString *)test {
TestDBModel *result = [self.database getOneObjectOfClass:TestDBModel.class where:TestDBModel.test == test];
return result;
}
@end
基本的功能使用如下:
self.manager = [TMFStorageManager sharedManager];
//增
TestDBModel *testModel = [[TestDBModel alloc] init];
testModel.key = @"1";
testModel.test = @"this is a test";
BOOL result = [self.manager.commonDBContext insertObject:testModel];
//删
//删除的方法一般需要自己实现,可以查看TMFStorageDBContext+Test 中的实现方式
BOOL result = [self.manager.commonDBContext deleteModelWithKey:@"1"];
//改
BOOL result = [self.manager.commonDBContext insertOrReplaceObject:testModel];
//查
TestDBModel *testModel = [self.manager.commonDBContext getOneObjectOfClass:TestDBModel.class];
//查(自定义)
TestDBModel *testModel = [self.manager.commonDBContext selectTestModelWithTest:@"this is a test for update data"];
//清表
BOOL result = [self.manager.commonDBContext clearTable:NSStringFromClass(TestDBModel.class)];
批量操作可以使用以下写法(这并不是“事务”,只是把数据库操作放到一个block里,写法上更简单,如果要使用事务,应该在自定义的方法中使用self.database runtransaction):
//批量操作写法(异步)
TestDBModel *testModel = [[TestDBModel alloc] init];
testModel.key = @"2";
testModel.test = @"this is a test";
[self.manager.commonDBContext performBlock:^(TMFStorageDBContext *db) {
[db insertObject:testModel];
[db deleteModelWithKey:@"2"];
}];
//批量操作写法(同步)
TestDBModel *testModel = [[TestDBModel alloc] init];
testModel.key = @"2";
testModel.test = @"this is a test";
[self.manager.commonDBContext performBlockAndWait:^(TMFStorageDBContext *db) {
[db insertObject:testModel];
[db deleteModelWithKey:@"2"];
}];
WCDB使用教程,请参见 官方文档。