Skip to content

Commit ab8b6ea

Browse files
author
zhongbaojian
committed
更新 Realm 库版本为 3.5.0,同时优化了一些方法
1 parent e41b3f8 commit ab8b6ea

15 files changed

+290
-47
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ fastlane/screenshots
5858
# https://github.com/johnno1962/injectionforxcode
5959

6060
iOSInjectionProject/
61+
AJRealmDB/Pods

AJRealmDB/AJRealmDB.xcodeproj/project.pbxproj

+105-14
Large diffs are not rendered by default.

AJRealmDB/AJRealmDB.xcworkspace/contents.xcworkspacedata

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// AJDBConfig.h
3+
// RealmPro
4+
//
5+
// Created by zbj on 2018/5/18.
6+
// Copyright © 2018年 qlchat. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import "AJDBObject.h"
11+
12+
@interface AJDBConfig : NSObject
13+
14+
/// 数据库加密KEY
15+
@property (nonatomic, strong, nullable) NSData *encryptKey;
16+
/// 数据库版本
17+
@property (nonatomic, assign) uint64_t dbVer;
18+
/// 数据库版本合并处理回调
19+
@property (nonatomic, copy, nullable) RLMMigrationBlock migrationBlock;
20+
21+
@end
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// AJDBConfig.m
3+
// RealmPro
4+
//
5+
// Created by zbj on 2018/5/18.
6+
// Copyright © 2018年 qlchat. All rights reserved.
7+
//
8+
9+
#import "AJDBConfig.h"
10+
11+
@implementation AJDBConfig
12+
13+
@end

AJRealmDB/AJRealmDB/AJRealmDB/AJDBManager.h

+17-10
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@
99
#import <Foundation/Foundation.h>
1010
#import "AJDBObject.h"
1111
#import "AJSortFilter.h"
12+
#import "AJDBConfig.h"
1213

1314
@interface AJDBManager : NSObject
1415

15-
1616
/**
17-
* 设置数据库加密Key,全局只需设置一次。如果不设置,默认不加密
18-
* 一个数据库只对应一个Key
19-
@param secKey 加密Key
17+
* 数据库初始化配置, 全局只会执行一次
18+
* @param cfg 配置信息
2019
*/
21-
+ (void)configSecurityKey:(NSData *)secKey;
20+
+ (void)setupConfigInfo:(AJDBConfig *)cfg;
2221

2322
/**
2423
* 写入一条数据
@@ -32,14 +31,14 @@
3231
*
3332
* @param objs 数组
3433
*/
35-
+ (void)writeObjArray:(NSArray<__kindof AJDBObject *> *)objs;
34+
+ (void)writeObjs:(NSArray<__kindof AJDBObject *> *)objs;
3635

3736
/**
3837
* 更新一条数据,更新数据必须在block中执行
3938
*
4039
* @param updateBlock 在block中更新数据
4140
*/
42-
+ (void)updateObj:(void (^)())updateBlock;
41+
+ (void)updateObj:(void (^)(void))updateBlock;
4342

4443
/**
4544
* 在数据库中删除目标数据
@@ -48,6 +47,14 @@
4847
*/
4948
+ (void)deleteObj:(__kindof AJDBObject *)obj;
5049

50+
/**
51+
* 在数据库中目标主键数据
52+
*
53+
* @param primaryKey 主键
54+
* @param clazz 目标类
55+
*/
56+
+ (void)deleteObjWithPrimaryKey:(id)primaryKey targetClass:(Class)clazz;
57+
5158
/**
5259
* 在数据库中删除目标数组数据
5360
*
@@ -72,7 +79,7 @@
7279
*
7380
* @return 查询结果
7481
*/
75-
+ (NSArray<__kindof AJDBObject *> *)queryObjWithPredicate:(NSPredicate *)predicate targetClass:(Class)clazz;
82+
+ (NSArray<__kindof AJDBObject *> *)queryObjsWithPredicate:(NSPredicate *)predicate targetClass:(Class)clazz;
7683

7784
/**
7885
* 根据断言条件查询数据,并进行排序
@@ -83,7 +90,7 @@
8390
*
8491
* @return 查询结果
8592
*/
86-
+ (NSArray<__kindof AJDBObject *> *)queryObjWithPredicate:(NSPredicate *)predicate sortFilter:(AJSortFilter *)sortFilter targetClass:(Class)clazz;
93+
+ (NSArray<__kindof AJDBObject *> *)queryObjsWithPredicate:(NSPredicate *)predicate sortFilter:(AJSortFilter *)sortFilter targetClass:(Class)clazz;
8794

8895
/**
8996
* 根据主键查询目标数据
@@ -93,7 +100,7 @@
93100
*
94101
* @return 查询结果
95102
*/
96-
+ (__kindof AJDBObject *)queryObjWithPrimaryKeyValue:(id)primaryKey targetClass:(Class)clazz;
103+
+ (__kindof AJDBObject *)queryObjWithPrimaryKey:(id)primaryKey targetClass:(Class)clazz;
97104

98105
/**
99106
* 清空数据库

AJRealmDB/AJRealmDB/AJRealmDB/AJDBManager.m

+39-7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ + (AJDBManager *)sharedInstance
2727
return instance;
2828
}
2929

30+
31+
+ (void)setupConfigInfo:(AJDBConfig *)cfg
32+
{
33+
if (cfg == nil) {
34+
return;
35+
}
36+
37+
static dispatch_once_t onceToken;
38+
dispatch_once(&onceToken, ^{
39+
40+
// 数据库配置
41+
RLMRealmConfiguration *rlmCfg = [RLMRealmConfiguration defaultConfiguration];
42+
rlmCfg.schemaVersion = cfg.dbVer;
43+
rlmCfg.migrationBlock = cfg.migrationBlock;
44+
45+
if (cfg.encryptKey.length > 0) {
46+
rlmCfg.encryptionKey = cfg.encryptKey;
47+
}
48+
49+
[self sharedInstance].realmConfig = rlmCfg;
50+
});
51+
}
52+
3053
+ (void)configSecurityKey:(NSData *)secKey
3154
{
3255
if (secKey == nil || [secKey length] == 0) {
@@ -77,17 +100,17 @@ + (void)writeObj:(__kindof AJDBObject *)obj
77100
[realm commitWriteTransaction];
78101
}
79102

80-
+ (void)writeObjArray:(NSArray<__kindof AJDBObject *> *)objs
103+
+ (void)writeObjs:(NSArray<__kindof AJDBObject *> *)objs
81104
{
82105
RLMRealm *realm = [[self sharedInstance] realm];
83106
[realm beginWriteTransaction];
84-
[realm addOrUpdateObjectsFromArray:objs];
107+
[realm addOrUpdateObjects:objs];
85108
[realm commitWriteTransaction];
86109
}
87110

88111
#pragma mark - 更新
89112

90-
+ (void)updateObj:(void (^)())updateBlock
113+
+ (void)updateObj:(void (^)(void))updateBlock
91114
{
92115
RLMRealm *realm = [[self sharedInstance] realm];
93116

@@ -106,6 +129,14 @@ + (void)deleteObj:(__kindof AJDBObject *)obj
106129
[realm commitWriteTransaction];
107130
}
108131

132+
+ (void)deleteObjWithPrimaryKey:(id)primaryKey targetClass:(Class)clazz
133+
{
134+
AJDBObject *obj = [self queryObjWithPrimaryKey:primaryKey targetClass:clazz];
135+
if (obj) {
136+
[self deleteObj:obj];
137+
}
138+
}
139+
109140
+ (void)deleteObjs:(NSArray<__kindof AJDBObject *> *)objs
110141
{
111142
RLMRealm *realm = [[self sharedInstance] realm];
@@ -133,7 +164,7 @@ + (void)deleteObjs:(NSArray<__kindof AJDBObject *> *)objs
133164
return resultArray;
134165
}
135166

136-
+ (NSArray<__kindof AJDBObject *> *)queryObjWithPredicate:(NSPredicate *)predicate targetClass:(Class)clazz
167+
+ (NSArray<__kindof AJDBObject *> *)queryObjsWithPredicate:(NSPredicate *)predicate targetClass:(Class)clazz
137168
{
138169
[AJDBManager checkClazz:clazz];
139170

@@ -150,13 +181,14 @@ + (void)deleteObjs:(NSArray<__kindof AJDBObject *> *)objs
150181
return resultArray;
151182
}
152183

153-
+ (NSArray<__kindof AJDBObject *> *)queryObjWithPredicate:(NSPredicate *)predicate sortFilter:(AJSortFilter *)sortFilter targetClass:(Class)clazz;
184+
+ (NSArray<__kindof AJDBObject *> *)queryObjsWithPredicate:(NSPredicate *)predicate sortFilter:(AJSortFilter *)sortFilter targetClass:(Class)clazz;
154185
{
155186
[AJDBManager checkClazz:clazz];
156187

157188
RLMRealm *realm = [[self sharedInstance] realm];
189+
158190
RLMResults<AJDBObject *> *queryResult = [[clazz objectsInRealm:realm withPredicate:predicate]
159-
sortedResultsUsingProperty:sortFilter.sortPropertyName
191+
sortedResultsUsingKeyPath:sortFilter.sortPropertyName
160192
ascending:sortFilter.ascending];
161193

162194
NSMutableArray *resultArray = [NSMutableArray array];
@@ -170,7 +202,7 @@ + (void)deleteObjs:(NSArray<__kindof AJDBObject *> *)objs
170202
return resultArray;
171203
}
172204

173-
+ (__kindof AJDBObject *)queryObjWithPrimaryKeyValue:(id)primaryKey targetClass:(Class)clazz;
205+
+ (__kindof AJDBObject *)queryObjWithPrimaryKey:(id)primaryKey targetClass:(Class)clazz;
174206
{
175207
[AJDBManager checkClazz:clazz];
176208

AJRealmDB/AJRealmDB/AppDelegate.m

+21-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,27 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
2727

2828
NSLog(@"#secKey-data#: %@", seckey); // 在电脑上读取加密后的数据库时可以使用这串16进制Key
2929

30-
// [AJDBManager configSecurityKey:seckey];
30+
31+
// 数据库配置
32+
AJDBConfig *cfg = [AJDBConfig new];
33+
cfg.encryptKey = seckey;
34+
cfg.dbVer = 1000;
35+
cfg.migrationBlock = ^(RLMMigration * _Nonnull migration, uint64_t oldSchemaVersion) {
36+
// 数据库版本合并
37+
if (oldSchemaVersion < 1000) {
38+
// The enumerateObjects:block: method iterates
39+
// over every 'Person' object stored in the Realm file
40+
// [migration enumerateObjects:Person.className
41+
// block:^(RLMObject *oldObject, RLMObject *newObject) {
42+
//
43+
// // combine name fields into a single field
44+
// newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@",
45+
// oldObject[@"firstName"],
46+
// oldObject[@"lastName"]];
47+
// }];
48+
}
49+
};
50+
[AJDBManager setupConfigInfo:cfg];
3151

3252
return YES;
3353
}

AJRealmDB/AJRealmDB/DatabaseTestViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ - (IBAction)manyAddBtnClick:(id)sender
197197
[studentArray addObject:student];
198198
}
199199

200-
[AJDBManager writeObjArray:studentArray];
200+
[AJDBManager writeObjs:studentArray];
201201

202202
[self refreshData];
203203
}

AJRealmDB/AJRealmDB/QueryDataViewController.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ - (IBAction)primaryKeyBtnClick:(id)sender
7575
NSString *primaryKey = self.primaryKeyTF.text;
7676
if (primaryKey != nil && primaryKey.length > 0) {
7777

78-
StudentBean *student = [AJDBManager queryObjWithPrimaryKeyValue:@([primaryKey integerValue]) targetClass:[StudentBean class]];
78+
StudentBean *student = [AJDBManager queryObjWithPrimaryKey:@([primaryKey integerValue]) targetClass:[StudentBean class]];
7979

8080
if (student) {
8181

@@ -99,7 +99,7 @@ - (IBAction)prdicateBtnClick:(id)sender
9999

100100
// 不排序的查询
101101
// NSPredicate *query1 = [NSPredicate predicateWithFormat:@"age > %d", 25];
102-
// NSArray *queryResult = [AJDBManager queryObjWithPredicate:query1 targetClass:[StudentBean class]];
102+
// NSArray *queryResult = [AJDBManager queryObjsWithPredicate:query1 targetClass:[StudentBean class]];
103103
//
104104
// if (queryResult.count > 0) {
105105
// [self.studentArray removeAllObjects];
@@ -112,7 +112,7 @@ - (IBAction)prdicateBtnClick:(id)sender
112112
// 排序查询
113113
NSPredicate *query2 = [NSPredicate predicateWithFormat:@"height >= %f", 166.0f];
114114
AJSortFilter *sortFilter = [AJSortFilter sortFilterWithPropertyName:@"age" ascending:NO];
115-
NSArray *queryResult = [AJDBManager queryObjWithPredicate:query2 sortFilter:sortFilter targetClass:[StudentBean class]];
115+
NSArray *queryResult = [AJDBManager queryObjsWithPredicate:query2 sortFilter:sortFilter targetClass:[StudentBean class]];
116116

117117
if (queryResult.count > 0) {
118118
[self.studentArray removeAllObjects];

AJRealmDB/AJRealmDB/UpdateViewController.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ - (IBAction)writeArrayBtnClick:(UIButton *)sender
6060
[dogArray addObject:dog1];
6161
}
6262

63-
[AJDBManager writeObjArray:dogArray];
63+
[AJDBManager writeObjs:dogArray];
6464
}
6565

6666
- (IBAction)deleteArrayBtnClick:(id)sender

AJRealmDB/Podfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Uncomment the next line to define a global platform for your project
2+
source 'https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git'
3+
platform :ios, '9.0'
4+
5+
target 'AJRealmDB' do
6+
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
7+
# use_frameworks!
8+
9+
pod 'Realm', '3.5.0'
10+
11+
target 'AJRealmDBTests' do
12+
inherit! :search_paths
13+
end
14+
15+
end

AJRealmDB/Podfile.lock

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
PODS:
2+
- Realm (3.5.0):
3+
- Realm/Headers (= 3.5.0)
4+
- Realm/Headers (3.5.0)
5+
6+
DEPENDENCIES:
7+
- Realm (= 3.5.0)
8+
9+
SPEC REPOS:
10+
https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git:
11+
- Realm
12+
13+
SPEC CHECKSUMS:
14+
Realm: 0378021c424e4e2bd6a0e63b27881e929d9b9ad1
15+
16+
PODFILE CHECKSUM: d2f673f26b9c753d3eb4ed4d39b596c16c3b3936
17+
18+
COCOAPODS: 1.5.0

0 commit comments

Comments
 (0)