Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

强化KeyValueItem #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions YTKKeyValueStore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
0C92970E1A090B7F0098DD16 /* YTKKeyValueManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 0C92970D1A090B7F0098DD16 /* YTKKeyValueManager.m */; };
4597AE5A19DEDFB30028ECAC /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4597AE5919DEDFB30028ECAC /* main.m */; };
4597AE5D19DEDFB30028ECAC /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4597AE5C19DEDFB30028ECAC /* AppDelegate.m */; };
4597AE6019DEDFB30028ECAC /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4597AE5F19DEDFB30028ECAC /* ViewController.m */; };
Expand Down Expand Up @@ -34,6 +35,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
0C92970C1A090B7F0098DD16 /* YTKKeyValueManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YTKKeyValueManager.h; sourceTree = "<group>"; };
0C92970D1A090B7F0098DD16 /* YTKKeyValueManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YTKKeyValueManager.m; sourceTree = "<group>"; };
4597AE5419DEDFB30028ECAC /* YTKKeyValueStore.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = YTKKeyValueStore.app; sourceTree = BUILT_PRODUCTS_DIR; };
4597AE5819DEDFB30028ECAC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4597AE5919DEDFB30028ECAC /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -105,6 +108,8 @@
isa = PBXGroup;
children = (
4597AE9019DEE0D90028ECAC /* FMDB */,
0C92970C1A090B7F0098DD16 /* YTKKeyValueManager.h */,
0C92970D1A090B7F0098DD16 /* YTKKeyValueManager.m */,
4597AE7D19DEDFC80028ECAC /* YTKKeyValueStore.h */,
4597AE7E19DEDFC80028ECAC /* YTKKeyValueStore.m */,
4597AE5B19DEDFB30028ECAC /* AppDelegate.h */,
Expand Down Expand Up @@ -265,6 +270,7 @@
files = (
4597AE6019DEDFB30028ECAC /* ViewController.m in Sources */,
4597AE9F19DEE0D90028ECAC /* FMDatabaseQueue.m in Sources */,
0C92970E1A090B7F0098DD16 /* YTKKeyValueManager.m in Sources */,
4597AE5D19DEDFB30028ECAC /* AppDelegate.m in Sources */,
4597AE7F19DEDFC80028ECAC /* YTKKeyValueStore.m in Sources */,
4597AE9C19DEE0D90028ECAC /* FMDatabase.m in Sources */,
Expand Down Expand Up @@ -464,6 +470,7 @@
4597AE7919DEDFB30028ECAC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
4597AE7A19DEDFB30028ECAC /* Build configuration list for PBXNativeTarget "YTKKeyValueStoreTests" */ = {
isa = XCConfigurationList;
Expand All @@ -472,6 +479,7 @@
4597AE7C19DEDFB30028ECAC /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
14 changes: 11 additions & 3 deletions YTKKeyValueStore/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import "AppDelegate.h"
#import "YTKKeyValueStore.h"
#import "YTKKeyValueManager.h"

@interface AppDelegate ()

Expand All @@ -19,15 +19,23 @@ @implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Demo
NSString *tableName = @"user_table";
YTKKeyValueStore *store = [[YTKKeyValueStore alloc] initDBWithName:@"test.db"];
NSString *msgTableName = @"msgTableName";
YTKKeyValueStore *store = [YTKKeyValueManager getKeyValueStoreWithName:@"test.db"];
[store createTableWithName:tableName];
[store createTableWithName:msgTableName];

NSString *key = @"1";
NSDictionary *user = @{@"id": @1, @"name": @"tangqiao", @"age": @30};
[store putObject:user withId:key intoTable:tableName];
YTKKeyValueItem *item = [store putObject:user withId:key intoTable:tableName];
[store putObject:item withId:key intoTable:msgTableName];

NSDictionary *queryUser = [store getObjectById:key fromTable:tableName];
NSLog(@"query data result: %@", queryUser);

YTKKeyValueItem *newItem = [store getObjectById:key fromTable:msgTableName];
NSLog(@"query data result: %@", newItem);


return YES;
}

Expand Down
18 changes: 18 additions & 0 deletions YTKKeyValueStore/YTKKeyValueManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// YTKKeyValueManager.h
// YTKKeyValueStore
//
// Created by Arthur on 14/11/4.
// Copyright (c) 2014年 TangQiao. All rights reserved.
//

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

@interface YTKKeyValueManager : NSObject

+ (YTKKeyValueStore *)getKeyValueStoreWithName:(NSString *)name;
+ (YTKKeyValueStore *)getKeyValueStoreWithPath:(NSString *)path;
+ (void)closeKeyValueWith:(YTKKeyValueStore *)store;

@end
88 changes: 88 additions & 0 deletions YTKKeyValueStore/YTKKeyValueManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// YTKKeyValueManager.m
// YTKKeyValueStore
//
// Created by Arthur on 14/11/4.
// Copyright (c) 2014年 TangQiao. All rights reserved.
//

#import "YTKKeyValueManager.h"

@interface YTKKeyValueManager ()

@property (nonatomic, strong) NSMutableDictionary* stores;

@end

@implementation YTKKeyValueManager

+ (YTKKeyValueManager*)defaultKeyValueManager {
static YTKKeyValueManager* manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ manager = [[YTKKeyValueManager alloc] init]; });

return manager;
}

- (instancetype)init {
self = [super init];
if (self) {
self.stores = [[NSMutableDictionary alloc] init];
}

return self;
}

+ (YTKKeyValueStore*)getKeyValueStoreWithName:(NSString*)name {
if (name.length == 0) {
return nil;
}

NSString* path = [[NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:name];

return [YTKKeyValueManager getKeyValueStoreWithPath:path];
}

+ (YTKKeyValueStore*)getKeyValueStoreWithPath:(NSString*)path {
if (path.length == 0) {
return nil;
}

YTKKeyValueManager* manager = [YTKKeyValueManager defaultKeyValueManager];

if ([manager.stores objectForKey:path]) {
return [manager.stores objectForKey:path];
} else {
YTKKeyValueStore* store =
[[YTKKeyValueStore alloc] initWithDBWithPath:path];
if (store) {
[manager.stores setObject:store forKey:path];
return store;
}
}

return nil;
}

+ (void)closeKeyValueWith:(YTKKeyValueStore*)store {
YTKKeyValueManager* manager = [YTKKeyValueManager defaultKeyValueManager];
NSString* deleteKey = nil;

NSArray* keys = [manager.stores allKeys];
for (NSString* key in keys) {
id object = [manager.stores objectForKey:key];
if (object == store) {
deleteKey = key;
break;
}
}

if (deleteKey) {
[store close];
[manager.stores removeObjectForKey:deleteKey];
}
}

@end
17 changes: 13 additions & 4 deletions YTKKeyValueStore/YTKKeyValueStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@

#import <Foundation/Foundation.h>

@interface YTKKeyValueItem : NSObject
#define YTKKeyValueItem NSMutableDictionary

@interface NSMutableDictionary(YTKKeyValueStore)

- (YTKKeyValueItem*)initKeyValueItemWithPath:(NSString *)path;
- (void)cleanObject;
- (void)loadObject;

@property (strong, nonatomic) NSString *tableName;
@property (strong, nonatomic) NSString *itemId;
@property (strong, nonatomic) id itemObject;
@property (strong, nonatomic) NSDate *createdTime;
Expand All @@ -19,6 +26,8 @@

@interface YTKKeyValueStore : NSObject

@property (nonatomic, strong) NSString* dbPath;

- (id)initDBWithName:(NSString *)dbName;

- (id)initWithDBWithPath:(NSString *)dbPath;
Expand All @@ -31,17 +40,17 @@

///************************ Put&Get methods *****************************************

- (void)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)tableName;
- (YTKKeyValueItem *)putObject:(id)object withId:(NSString *)objectId intoTable:(NSString *)tableName;

- (id)getObjectById:(NSString *)objectId fromTable:(NSString *)tableName;

- (YTKKeyValueItem *)getYTKKeyValueItemById:(NSString *)objectId fromTable:(NSString *)tableName;

- (void)putString:(NSString *)string withId:(NSString *)stringId intoTable:(NSString *)tableName;
- (YTKKeyValueItem *)putString:(NSString *)string withId:(NSString *)stringId intoTable:(NSString *)tableName;

- (NSString *)getStringById:(NSString *)stringId fromTable:(NSString *)tableName;

- (void)putNumber:(NSNumber *)number withId:(NSString *)numberId intoTable:(NSString *)tableName;
- (YTKKeyValueItem *)putNumber:(NSNumber *)number withId:(NSString *)numberId intoTable:(NSString *)tableName;

- (NSNumber *)getNumberById:(NSString *)numberId fromTable:(NSString *)tableName;

Expand Down
Loading