Latest 1.2.0
Homepage https://github.com/ephedra-software/RStorage
License MIT
Platforms ios 8.0
Authors

RStorage Build Status Version codebeat badge codecov

UserDefaults abstraction framework with caching

Installation

CocoaPods

For RStorage, use the following entry in your Podfile:

pod 'RStorage', '~> 1.2'

Then run pod install.

In any file you’d like to use RStorage in, don’t forget to
import the framework with import RStorage.

Usage

Using RStorage is really simple. You can access an API like this:

let storage: RStorage = RStorage<KeyManager>.instance

struct YourCodableStructure: Codable {
    let name: String
}

do {
    try storage.save(key: KeyManager.keys.keyOne, value: URL("https://www.google.com/")!)
    try storage.save(key: KeyManager.keys.keyTwo, value: YourCodableStructure(name: "Struct"))

    let data1 = try storage.load(key: KeyManager.keys.keyOne)
    let data2 = try storage.load(key: KeyManager.keys.keyTwo)

    print("Url: (data1 ?? "Not found url")")                   // Url: https://www.google.com/
    print("Data name: (data2?.name ?? "Not found data2")")     // Data name: Struct
} catch {
    print(error.localizedDescription)
}

storage.removeAll(without: KeyManager.keyOne)

do {
    let data1 = try storage.load(key: KeyManager.keys.keyOne)
    let data2 = try storage.load(key: KeyManager.keys.keyTwo)

    print("Url: (data1 ?? "Not found url")")                   // Url: https://www.google.com/
    print("Data name: (data2?.name ?? "Not found data2")")     // Data name: Not found data2
} catch {
    print(error.localizedDescription)
}

To do this, you must implement the following:

enum KeyManager: String, RStorageManagerProtocol {
    typealias SupportedKeys = (
        keyOne: Key<URL, KeyManager>,
        keyTwo: Key<YourCodableStructure, KeyManager>
    )

    case keyOne = "__DefaultType__"
    case keyTwo = "__YourCodableStructure__"

    static var keys: SupportedKeys {
        return (
            Key(.keyOne),
            Key(.keyTwo)
        )
    }

    var useCache: Bool {
        switch self {
        case .keyOne: return false
        case .keyTwo: return true
        }
    }

    var usePersistentStorage: Bool {
        switch self {
        case .keyOne: return true
        case .keyTwo: return true
        }
    }

    var name: String {
        return self.rawValue
    }
}

License

RStorage is released under an MIT license. See LICENSE for more information.

Latest podspec

{
    "name": "RStorage",
    "version": "1.2.0",
    "summary": "UserDefaults abstraction framework with caching",
    "homepage": "https://github.com/ephedra-software/RStorage",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Erik Basargin": "[email protected]"
    },
    "social_media_url": "https://twitter.com/Puasonych",
    "source": {
        "git": "https://github.com/ephedra-software/RStorage.git",
        "tag": "1.2.0"
    },
    "swift_version": "5.0",
    "platforms": {
        "ios": "8.0"
    },
    "default_subspecs": "Core",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "RStorage/*.swift",
            "frameworks": "Foundation"
        }
    ]
}

Pin It on Pinterest

Share This