Skip to content

Rannie/RHKeyValueStore

Repository files navigation

RHKeyValueStore

Key-Value storage tool,based on WCDB (WeChat DataBase).

About WCDB : Tencent/WCDB , 微信WCDB进化之路 - 开源与开始

Easy KV database, can use it instead of NSUserDefaults.

Installation


Add pod 'RHKeyValueStore' to your Podfile and run pod install.
Import <RHKeyValueStore.h>.

Usage


Already #define KeyValueStore [RHKeyValueStore store] ,
so you can use KeyValuStore to get the store instance.

  • Integer
[KeyValueStore setInteger:10 forKey:@"someInteger"];
[KeyValueStore integerForKey:@"someInteger"];
  • Double
[KeyValueStore setDouble:12.4 forKey:@"someDouble"];
[KeyValueStore doubleForKey:@"someDouble"];
  • Bool
[KeyValueStore setBool:YES forKey:@"boolean"];
[KeyValueStore boolForKey:@"boolean"]; 
  • String
[KeyValueStore setString:@"lalal" forKey:@"onestring"];
[KeyValueStore stringForKey:@"onestring"];
  • Object

JSON encoding/decoding

TestObject *obj = [TestObject new];
obj.host = @"github";
obj.ipAddress = @"127.0.0.1";
obj.count = 10;
[KeyValueStore setJsonObject:[obj yy_modelToJSONObject] forKey:@"jsonModel"];
    
NSDictionary *ano = [KeyValueStore jsonObjectForKey:@"jsonModel"];
TestObject *obj2 = [TestObject yy_modelWithDictionary:ano];

Archive/Unarchive (confirm NSCoding Protocol)

[KeyValueStore setArchiveObject:obj forKey:@"archiveObj"];
TestObject *obj3 = [KeyValueStore unarchiveObjectForKey:@"archiveObj"];
  • Isolation Store

Use method + (instancetype)storeWithLabel:(NSString *)label to create a new table.

Like:

NSString *userId = @"my_user_id";
RHKeyValueStore *userStore = [RHKeyValueStore storeWithLabel:userId];
[userStore setInteger:30 forKey:@"someInteger"];
    
NSLog(@"global -- %zd, user -- %zd", [KeyValueStore integerForKey:@"someInteger"], [userStore integerForKey:@"someInteger"]);

Console output global -- 10, user -- 30 .

  • Other Methods
// get all keys
NSArray *keys = [KeyValueStore allKeys];
// get pair count
NSUInteger count = [KeyValueStore getKVCount];
// clear current table
[userStore clear];

Documentation


Full API documentation is available on CocoaDocs.

License


The MIT License (MIT)

Copyright (c) 2017 Hanran Liu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.