kvdb

Warning: this package is not actively supported.

You may prefer to use something newer like Couchbase Lite, which is excellent and is widely supported. This package was made long before NoSQL was really available on iPhone.

kvdb

kvdb is a key-value store for iOS backed by SQLite3.

It can serialize and store any objects that implement the NSCoding protocol.

Therefore, lots of objects are supported right away:

  • NSDictionary
  • NSArray
  • NSNumber
  • NSString

etc.

Usage

#import "kvdb.h"

[[KVDB sharedDB] setValue:@"apple" forKey:@"fruit"];
[[KVDB sharedDB] setValue:@"Chicago" forKey:@"city"];

MyObject *object = [[MyObject alloc] initWithTitle:@"KVDB"];
[[KVDB sharedDB] setValue:object forKey:@"my_object"];

Installation

The recommended way is to install via Cocoapods:

Add into your Podfile:

pod 'kvdb', :git => 'https://github.com/colinyoung/kvdb'

And run

pod update

or you can just clone kvdb and add kvdb/ folder to your project.

Other stuff

[[KVDB sharedDB] dropDatabase];

[[KVDB sharedDB] createDatabase];

// Instantiate w/ a different file:
[KVDB sharedDBWithFile:@"blah.sqlite3"]; // the file will be created in your documents directory.

Coding nil-values: nil vs NSNull

To provide a compatibility with NSArray and NSDictionary classes KVDB denies the coding of nilvalues. KVDB follows the same policy as NSDictionary class does:

-[KVDB setValue:forKey:] - if value is nil then method attempts to remove key using removeValueForKey:.

-[KVDB setObject:forKey:] - raises an NSInvalidArgumentException if aKey is nil.

Like it is done when working with instances of NSArray or NSDictionary use NSNull class whereever you want -[KVDB setValue:forKey] to provide a given key with a null value:

Use

[[KVDB sharedDB] setValue:[NSNull null] forKey:@"fruit"];

id dbValue = [[KVDB sharedDB] valueForKey:testKey]; 

NSLog(@"%@", dbValue); // will print "<null>" i.e.  NSNull singleton

Instead of

[[KVDB sharedDB] setObject:nil forKey:@"fruit"]; // => Results in NSInvalidArgumentException

or

[[KVDB sharedDB] setValue:nil forKey:@"fruit"]; // => Results in removeValueForKey: behavior 

See the documentation of -[NSDictionary setObject:forKey:] and this nice NSHipster article about nil and NSNull.

Notes

  • kvdb is a simple key value store for iOS solution with codebase containing just a couple of files. Don't ask it what it is not intended for: it is not thread-safe, it does not support transactions and so on. For more serious solutions see "Similar tools".
  • kvdb plays very nice with all the solutions like Mantle, which provide auto-coding for all the properties you declare in your classes. For example, if using Mantle: create a MTLModel subclass, declare its properties, and... it is ready to be stored in a KVDB store, because Mantle has already autocoded these properties for you.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值