r-dent / ObjectStore

A simple class that stores lists of `Codable` objects to JSON files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Poor Persons Object Store

A simple class that stores a list of Codable objects to a JSON file. Providing an easy to use persistence solution for small amounts of data.

The ObjectStore will save the data to JSON files in the ApplicationSupport directory.

Installation

Just copy the contents of the Sources folder to your project.

Usage

Make your datatypes conform to Codable and ObjectStoreElement by providing an id property.

struct TestData: Codable, ObjectStoreElement {
    let id: Int // Required by ObjectStoreElement protocol.
    let created: Date = Date()
    let name: String
    let uuid: String
    let float: Float
}

Create an ObjectStore for your types.

db = JsonObjectStore<TestData>(fileName: "testdata.json") { elements in
    print("Found \(elements.count) objects.")
}

Add objects to the store.

db.add(TestData(
    id: 3,
    name: "A Name",
    uuid: UUID().uuidString,
    float: 2.345
))

About

A simple class that stores lists of `Codable` objects to JSON files.


Languages

Language:Swift 100.0%