Skip to content

Commit

Permalink
加入KeyValue存储功能
Browse files Browse the repository at this point in the history
  • Loading branch information
bing6 committed Feb 22, 2017
1 parent 476ac3c commit ebe93ac
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 6 deletions.
83 changes: 83 additions & 0 deletions Classes/FMDTKeyValueStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// FMDTKeyValueStorage.h
// FMDataTable
//
// Created by bing.hao on 2017/2/22.
// Copyright © 2017年 bing.hao. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "FMDTManager.h"

#define STORE [FMDTKeyValueStorage shared]

@interface FMDTKeyValueStorage : FMDTManager

/**
添加键值存储
@param key 关键字
@param value 具体的值,可以是NSString, NSNumber, Dictionary, Array等类型
*/
- (void)set:(NSString *)key value:(id)value;


/**
获取值
@param key 关键字
@return 返回值
*/
- (id)objectForKey:(NSString *)key;


/**
获取值
@param key 关键字
@return 返回值
*/
- (NSString *)stringForKey:(NSString *)key;


/**
获取值
@param key 关键字
@return 返回值
*/
- (NSNumber *)numberForKey:(NSString *)key;


/**
获取值
@param key 关键字
@return 返回值
*/
- (NSDictionary *)dictionaryForKey:(NSString *)key;


/**
获取值
@param key 关键字
@return 返回值
*/
- (NSArray *)arrayForKey:(NSString *)key;


/**
删除
@param key 关键字
*/
- (void)removeForKey:(NSString *)key;


/**
删除所有
*/
- (void)removeAll;

@end
128 changes: 128 additions & 0 deletions Classes/FMDTKeyValueStorage.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//
// FMDTKeyValueStorage.m
// FMDataTable
//
// Created by bing.hao on 2017/2/22.
// Copyright © 2017年 bing.hao. All rights reserved.
//

#import "FMDTKeyValueStorage.h"
#import "FMDTObject.h"

@interface FMDTKeyValueObject : FMDTObject

@property (nonatomic, strong) NSString *m_key;
@property (nonatomic, strong) NSDictionary *m_value;
//@property (nonatomic, strong) NSString *m_type;

@end

@implementation FMDTKeyValueObject

+ (NSString *)primaryKeyFieldName {
return @"m_key";
}

@end

@interface FMDTKeyValueStorage ()

@property (nonatomic, strong) FMDTContext *kv;

@end

@implementation FMDTKeyValueStorage

+ (instancetype)shared {
static id _staticObject = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_staticObject = [FMDTKeyValueStorage new];
});
return _staticObject;
}

- (FMDTContext *)kv {
// NSMutableDictionary *s;
// [s set]
NSString *path = [NSString stringWithFormat:@"%@/kv.db", [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
return [self cacheWithClass:[FMDTKeyValueObject class] dbPath:path];
}

//- (void)set:(NSString *)key valueForString:(NSString *)value {
//
//}
//
//- (void)set:(NSString *)key valueForNumber:(NSNumber *)value {
//
//}
//
//- (void)set:(NSString *)key valueForDictionary:(NSDictionary *)value {
//
//}
//
//- (void)set:(NSString *)key valueForArray:(NSArray *)value {
//
//}
//

- (void)set:(NSString *)key value:(id)value {
if (key == nil || value == nil) {
return;
}
FMDTKeyValueObject *obj = [[FMDTKeyValueObject alloc] init];
obj.m_key = key;
obj.m_value = @{ @"value": value };
FMDTInsertCommand *cmd = [self.kv createInsertCommand];
[cmd add:obj];
[cmd setRelpace:YES];
[cmd saveChanges];
}

- (id)objectForKey:(NSString *)key {

if (key == nil) {
return nil;
}

FMDTSelectCommand *cmd = [self.kv createSelectCommand];
[cmd where:@"m_key" equalTo:key];
FMDTKeyValueObject *obj = [cmd fetchObject];
if (obj) {
return [obj.m_value objectForKey:@"value"];
}
return nil;
}

- (NSString *)stringForKey:(NSString *)key {
return [self objectForKey:key];
}

- (NSNumber *)numberForKey:(NSString *)key {
return [self objectForKey:key];
}

- (NSDictionary *)dictionaryForKey:(NSString *)key {
return [self objectForKey:key];
};

- (NSArray *)arrayForKey:(NSString *)key {
return [self objectForKey:key];
}

- (void)removeForKey:(NSString *)key {

if (key == nil) {
return;
}
FMDTDeleteCommand *cmd = [self.kv createDeleteCommand];
[cmd where:@"m_key" equalTo:key];
[cmd saveChanges];
}

- (void)removeAll {
FMDTDeleteCommand *cmd = [self.kv createDeleteCommand];
[cmd saveChanges];
}

@end
4 changes: 2 additions & 2 deletions FMDBDataTable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Pod::Spec.new do |s|
#

s.name = "FMDBDataTable"
s.version = "2.1.6"
s.version = "2.2"
s.summary = "基于FMDB的一个ORM解决方案"

s.description = <<-DESC
Expand Down Expand Up @@ -78,7 +78,7 @@ Pod::Spec.new do |s|
# Supports git, hg, bzr, svn and HTTP.
#

s.source = { :git => "https://github.com/bing6/FMDBDataTable.git", :tag => "2.1.6" }
s.source = { :git => "https://github.com/bing6/FMDBDataTable.git", :tag => "2.2" }


# ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
Expand Down
10 changes: 10 additions & 0 deletions FMDataTable.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
37477D521C8FC1E500924741 /* FMDTUpdateObjectCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = 37477D511C8FC1E500924741 /* FMDTUpdateObjectCommand.m */; };
37477D551C8FE14C00924741 /* Users.m in Sources */ = {isa = PBXBuildFile; fileRef = 37477D541C8FE14C00924741 /* Users.m */; };
37477D581C8FE1E600924741 /* DBSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 37477D571C8FE1E600924741 /* DBSet.m */; };
374AEB451E5D4DAA00828FDA /* FMDTKeyValueStorage.m in Sources */ = {isa = PBXBuildFile; fileRef = 374AEB441E5D4DAA00828FDA /* FMDTKeyValueStorage.m */; };
3765CCA81C8E755100CB32B2 /* FMDTContext.m in Sources */ = {isa = PBXBuildFile; fileRef = 3765CCA71C8E755100CB32B2 /* FMDTContext.m */; };
3765CCAB1C8E755D00CB32B2 /* FMDTSchema.m in Sources */ = {isa = PBXBuildFile; fileRef = 3765CCAA1C8E755D00CB32B2 /* FMDTSchema.m */; };
3765CCAE1C8E756800CB32B2 /* FMDTObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 3765CCAD1C8E756800CB32B2 /* FMDTObject.m */; };
Expand Down Expand Up @@ -64,6 +65,9 @@
37477D541C8FE14C00924741 /* Users.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Users.m; sourceTree = "<group>"; };
37477D561C8FE1E600924741 /* DBSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DBSet.h; sourceTree = "<group>"; };
37477D571C8FE1E600924741 /* DBSet.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DBSet.m; sourceTree = "<group>"; };
374AEB431E5D4DAA00828FDA /* FMDTKeyValueStorage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FMDTKeyValueStorage.h; sourceTree = "<group>"; };
374AEB441E5D4DAA00828FDA /* FMDTKeyValueStorage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FMDTKeyValueStorage.m; sourceTree = "<group>"; };
374AEB461E5D5FD200828FDA /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
3765CCA61C8E755100CB32B2 /* FMDTContext.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FMDTContext.h; sourceTree = "<group>"; };
3765CCA71C8E755100CB32B2 /* FMDTContext.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FMDTContext.m; sourceTree = "<group>"; };
3765CCA91C8E755D00CB32B2 /* FMDTSchema.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FMDTSchema.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -135,6 +139,7 @@
371FF0041B93E0DD001D59BB = {
isa = PBXGroup;
children = (
374AEB461E5D5FD200828FDA /* README.md */,
37FC779E1CFFD23F009F7FA1 /* FMDBDataTable.podspec */,
371FF0491B93F3C4001D59BB /* Classes */,
371FF00F1B93E0DD001D59BB /* FMDataTable */,
Expand Down Expand Up @@ -219,6 +224,8 @@
3765CCB91C8E77A300CB32B2 /* FMDTDeleteCommand.m */,
3765CCBE1C8ED00300CB32B2 /* FMDTManager.h */,
3765CCBF1C8ED00300CB32B2 /* FMDTManager.m */,
374AEB431E5D4DAA00828FDA /* FMDTKeyValueStorage.h */,
374AEB441E5D4DAA00828FDA /* FMDTKeyValueStorage.m */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -396,6 +403,7 @@
3765CCB71C8E777300CB32B2 /* FMDTUpdateCommand.m in Sources */,
3765CCB41C8E774400CB32B2 /* FMDTInsertCommand.m in Sources */,
3765CCBA1C8E77A300CB32B2 /* FMDTDeleteCommand.m in Sources */,
374AEB451E5D4DAA00828FDA /* FMDTKeyValueStorage.m in Sources */,
37B305EE1C910B510079B2B1 /* Message.m in Sources */,
3765CCB11C8E757600CB32B2 /* FMDTSelectCommand.m in Sources */,
3765CCC01C8ED00300CB32B2 /* FMDTManager.m in Sources */,
Expand Down Expand Up @@ -529,6 +537,7 @@
baseConfigurationReference = DAAD600BEDF629331279BE95 /* Pods.debug.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = FMDataTable/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -541,6 +550,7 @@
baseConfigurationReference = B248441247E193317B6F9625 /* Pods.release.xcconfig */;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = FMDataTable/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand Down
4 changes: 2 additions & 2 deletions FMDataTable/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.1.6</string>
<string>2.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2.1.6</string>
<string>2.2</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UILaunchStoryboardName</key>
Expand Down
30 changes: 29 additions & 1 deletion FMDataTable/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import "ViewController.h"
#import "DBSet.h"
#import "FMDTKeyValueStorage.h"

@interface ViewController ()

Expand Down Expand Up @@ -190,18 +191,45 @@ - (void)dyInsertData {

}

- (void)testKeyValue {

//字符串存储
[STORE set:@"a1" value:@"我是字符串"];
//数值存储
[STORE set:@"a2" value:@(1)];
//字典存储
[STORE set:@"a3" value:@{@"test1": @"abc", @"test2": @"abc222"}];
//数组存储
[STORE set:@"a4" value:@[@(1),@(2),@(3),@(4)]];

//读取字符串
NSLog(@"%@", [STORE stringForKey:@"a1"]);
//读取数值
NSLog(@"%@", [STORE stringForKey:@"a2"]);
//读取字典
NSLog(@"%@", [STORE stringForKey:@"a3"]);
//读取数组
NSLog(@"%@", [STORE stringForKey:@"a4"]);

//删除键值
[STORE removeForKey:@"a1"];
//清除所有的键值
[STORE removeAll];
}

- (void)viewDidLoad {
[super viewDidLoad];

NSLog(@"%@", [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]);

[self insertData]; //插入数据
// [self insertData]; //插入数据
// [self updateData]; //更新数据
// [self deleteData]; //删除数据
// [self selectData]; //查询数据
//
// [self dyInsertData]; //向动态表里添加数据

[self testKeyValue];
}

- (void)didReceiveMemoryWarning {
Expand Down
Loading

0 comments on commit ebe93ac

Please # to comment.