-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
281 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.