Latest 1.2
Homepage https://github.com/YuAo/WAKeyValuePersistenceStore
License MIT
Platforms ios 7.0, osx 10.7, requires ARC

Build Status
CocoaPods Platform
CocoaPods Version
CocoaPods License

WAKeyValuePersistenceStore is a simple, file based Key-Value persistence store.

Usage

Create a WAKeyValuePersistenceStore is simple:

self.store = WAKeyValuePersistenceStore(
        directory: NSSearchPathDirectory.CachesDirectory,
        name: "Store",
        objectSerializer: WAPersistenceObjectSerializer.keyedArchiveSerializer())

directory is where you want to store the data files. (cache directory, document directory, application support directory… etc.)

name is the store’s name. Stores with the same directory/name combination share the same data storage.

objectSerializer controls how object will be serialized.

For instance, WAPersistenceObjectSerializer.keyedArchiveSerializer() uses NSKeyedArchiver and NSKeyedUnarchiver to serialize/deserialize objects.

WAPersistenceObjectSerializer.passthroughSerializer() is used to directly write the object to the disk. You may want to use this object serializer to store NSData objects.

You can also created custom object serializers by conform to the WAPersistenceObjectSerialization protocol.

Use subscript to store or retrive objects:

var testData = NSProcessInfo.processInfo().globallyUniqueString
self.store["data"] = testData
//.....
var string = self.store["data"]

Manage the store or get the store’s information

//WAKeyValuePersistenceStore

//Get the store's path on disk.
var path: String { get }

//Get the preferred file URL for a key.
func fileURLForKey(key: String) -> NSURL

//Remove objects
func removeAllObjects()

func removeObjectsByLastAccessDate(date: NSDate, progressChangedBlock: ((UInt, UInt, UnsafeMutablePointer<ObjCBool>) -> Void)?)

//Store information
var currentDiskUsage: UInt { get }

var objectCount: UInt { get }

Generics with Swift

There’s a swift extension for WAKeyValuePersistenceStore. You may specify the object type when retriving object from the store.

extension WAKeyValuePersistenceStore {
    public func objectForKey<ValueType>(aKey: String, valueType: ValueType.Type) -> ValueType? {
        if let value = self.objectForKey(aKey) as? ValueType {
            return value
        } else {
            return nil
        }
    }
}

Also there’s a WAKeyValuePersistenceStoreTypedAccessor<ValueType> class. You may use it to access a store using generics.

For example:

self.typedStore = WAKeyValuePersistenceStoreTypedAccessor<[Int]>(store: self.store)
var testData = [0,1,2,3,4,5,6]
self.typedStore["data"] = testData
//.....
var array = self.typedStore["data"] //array is [Int]

Install

Either clone the repo and manually add the files in WAKeyValuePersistenceStore directory

or if you use CocoaPods, add the following to your Podfile

pod 'WAKeyValuePersistenceStore'

If you use Swift, you can enable generics support by adding

use_frameworks!

pod 'WAKeyValuePersistenceStore/Generics'

Requirements

  • Automatic Reference Counting (ARC)
  • iOS 7.0+
  • Xcode 6.3+

Contributing

If you find a bug and know exactly how to fix it, please open a pull request. If you can’t make the change yourself, please open an issue after making sure that one isn’t already logged.

Latest podspec

{
    "name": "WAKeyValuePersistenceStore",
    "version": "1.2",
    "authors": "YuAo",
    "summary": "A file based Key-Value persistence store for iOS.",
    "homepage": "https://github.com/YuAo/WAKeyValuePersistenceStore",
    "license": {
        "type": "MIT",
        "file": "LICENSE.md"
    },
    "source": {
        "git": "https://github.com/YuAo/WAKeyValuePersistenceStore.git",
        "tag": "1.2"
    },
    "requires_arc": true,
    "platforms": {
        "ios": "7.0",
        "osx": "10.7"
    },
    "default_subspecs": "Core",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "WAKeyValuePersistenceStore/**/*.{h,m}"
        },
        {
            "name": "Generics",
            "source_files": "WAKeyValuePersistenceStore/WAKeyValuePersistenceStore+Generics.swift",
            "platforms": {
                "ios": "8.0",
                "osx": "10.10"
            },
            "dependencies": {
                "WAKeyValuePersistenceStore/Core": []
            }
        }
    ]
}

Pin It on Pinterest

Share This