Skip to content

It's a wrapper over UserDefaults which act like a LightStorage and work with Codable classes...Trust me it's really handy ;-)

License

Notifications You must be signed in to change notification settings

shndrs/HandyStorage

Repository files navigation

hs-shndrs

HandyStorage

Workflow Status codebeat badge Cocoapods Version swift-version swift package manager cocoapods GitHub License Min iOS Version Min macOS Version

In this project i wanna build an useful thread-safe storage with UserDefault which is super easy to deal with and on top of that you can save Codable Objects

HOW COOL IS THAT! πŸ™‚

Usage

It's pretty simple, just follow these instructions and you have it, First of all you need a Codable class/struct like below:

struct User: Codable {
    var username: String
    var password: String
}

Save/Insert

This will add a new object to "user-table"

Task {
    let user = User(username: "Walter", password: "123456")
    await HandyStorage.shared.save(object: user, key: "user-table")
}

Load

Get all users from "user-table"

Task {
    let users: [User] = await HandyStorage.shared.load(key: "user-table")
}     

Update

Update a specific object with new one

Task {
    let oldUser: User = await HandyStorage.shared.load(key: "user-table").first
    let newUser = HSUser(username: "Harvey", password: "654321")
    await HandyStorage.shared.update(oldObject: oldUser, newObject: newUser, key: "user-table")
}

Save/Insert an array of objects

Task {
    let user0 = User(username: "Jessica", password: "123456")
    let user1 = User(username: "Harvey", password: "654321")
    let user2 = User(username: "Mike", password: "321456")
    await HandyStorage.shared.saveArray(arrayObject: [user0, user1, user2], key: "user-table")
}

Delete Item

Task {
    let user: User = HandyStorage.shared.load(key: "user-table").first
    await HandyStorage.shared.delete(object: user, key: "user-table")
}

Delete Entire Table

This will delete whole "user-table"

Task {
    await HandyStorage.shared.delete(table: "user-table")
}

Delete Entire HandyStorage Data

Task {
    await HandyStorage.shared.removeAll()
}

Save/Load single String

Task {
    // Save
    await HandyStorage.shared.saveString(value: "Apple iMac M4", key: "system-model")
    // Load
    let singleString = await HandyStorage.shared.getString(key: "system-model")
}

Installation

HandyStorage is available through CocoaPods. To install the latest version, simply add the following line to your Podfile:

pod 'HandyStorage'

And then on your terminal run πŸ‘‡πŸ»:

pod install

spm-lg

  • File > Swift Packages > Add Package Dependency
  • Add https://github.com/shndrs/HandyStorage.git
  • Select "Up to Next Major" with "2.0.2"

Author

[email protected], [email protected], shndrs

License

HandyStorage is available under the MIT license. See the LICENSE file for more info.

About

It's a wrapper over UserDefaults which act like a LightStorage and work with Codable classes...Trust me it's really handy ;-)

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published