Giter Site home page Giter Site logo

pantry's Introduction

Storage

Platform: iOS 8+ Language: Swift 2 Carthage compatible CocoaPods compatible Docs License: MIT

InstallationIssuesLicense

Pantry is new! Please join us in issues if you'd like to help us get to 1.0. And read about more use cases for Pantry.

Pantry is a lightweight way to persist structs containing user data, cached content or other relevant objects for later retrieval.

let someCustomStruct = SomeCustomStruct(...)
Pantry.pack(someCustomStruct, "user_data")

... later ...

if let unpackedCustomStruct: SomeCustomStruct = Pantry.unpack("user_data") {
  print("got my data out",unpackedCustomStruct)
} else {
  print("there was no struct data to get")
}

You can store:

  • Structs
  • Strings, Ints and Floats (our default types)
  • Arrays of structs and default types
  • Nested structs
  • Nested Arrays
  • Classes
  • Arrays of classes and default types
  • Nested classes
  • Enums with raw types

Check out the tests for a detailed look at the varied types you can easily store.

Compatibility

Pantry requires iOS 8+ and is compatible with Swift 2 projects. Objective-C support is unlikely.

Installation

Installation for Carthage is simple enough:

github "nickoneill/Pantry" ~> 0.2.1

As for CocoaPods, use this to get the latest release:

use_frameworks!

pod 'Pantry'

And import Pantry in the files you'd like to use it.

Usage

Basic types

Pantry provides serialization of some basic types (String, Int, Float, Bool) with no setup. You can use it as a simple expiring cache like this:

if let available: Bool = Pantry.unpack("promptAvailable") {
    completion(available: available)
} else {
    anExpensiveOperationToDetermineAvailability({ (available) -> () in
      Pantry.pack(available, key: "promptAvailable", expires: .Seconds(60 * 10))
      completion(available: available)
    })
}

Automagic Persistent Variables

Use Swift's get/set to automatically persist the value of a variable on write and get the latest value on read.

var autopersist: String? {
    set {
        if let newValue = newValue {
            Pantry.pack(newValue, key: "autopersist")
        }
    }
    get {
        return Pantry.unpack("autopersist")
    }
}

...later...

autopersist = "Hello!"
// restart app, reboot phone, etc
print(autopersist) // Hello!

Structs

Add the Storable protocol to any struct you want stored and then ensure they comply by implementing an init method that gets each property from the warehouse:

struct Basic: Storable {
    let name: String
    let age: Float
    let number: Int

    init(warehouse: JSONWarehouse) {
        self.name = warehouse.get("name") ?? "default"
        self.age = warehouse.get("age") ?? 20.5
        self.number = warehouse.get("number") ?? 10
    }
}

Getters always provide an optional value, leaving you the opportunity to fill in a default if a value isn't available. This makes for hassle-free property additions to your structs.

Classes

Classes are also supported and can be setup the same way Structs are however the init method must be marked required in this case. Class inheritance and nested Storable properties are also possible:

class ModelBase: Storable {
    let id: String
    
    required init(warehouse: Warehouseable) {
        self.id = warehouse.get("id") ?? "default_id"
    }
}

class BasicClassModel: ModelBase {
    let name: String
    let age: Float
    let number: Int
    
    required init(warehouse: Warehouseable) {
        self.name = warehouse.get("name") ?? "default"
        self.age = warehouse.get("age") ?? 20.5
        self.number = warehouse.get("number") ?? 10
        
        super.init(warehouse: warehouse)
    }
}

Also

Pantry works great with network data when paired with a JSON struct decoder such as Unbox. Download JSON, decode it with Unbox, save it with Pantry and have it available for as long as you need. The architecture of Pantry is heavily influenced by Unbox, it's worth a look in any case.

License

Pantry uses the MIT license. Please file an issue if you have any questions or if you'd like to share how you're using this tool.

ack

Pantry "can icon" by CDH from the Noun Project

pantry's People

Contributors

nickoneill avatar andreamazz avatar wanewang avatar litso avatar ankitspd avatar iankeen avatar nuudles avatar readmecritic avatar arthur-here avatar

Watchers

James Cloos avatar Boon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.