Latest 1.0.1
Homepage https://github.com/cszatma/QuickPersist
License MIT
Platforms ios 8.0, osx 10.9, watchos 2.0, tvos 9.0
Dependencies RealmSwift
Frameworks Foundation
Authors

Language Swift
Version
License
Platform

QuickPersist provides an easy way to save Swift structs to a Realm database.

Basic Use

Persistable

In order to persist a struct to a Realm, the struct must conform to the Persistable protocol.
Persistable requires conforming types to conform to Codable.

Persistable requires two properties to be implemented.

  • typeName: a String that represents the name of the struct.
  • primaryKey: a String value that is a unique identifier for the instance
struct Person {
    var name: String
    var age: Int
    let id: String
}

extension Person: Persistable {
    static var typeName: String {
        return "Person"
    }

    var primaryKey: String {
        return id
    }
}

That’s it! QuickPersist takes care of the rest. This struct is now ready to be persisted.

RealmOperator

QuickPersist provides a type called RealmOperator that lets you do basic operations on a Realm that involve Persistable types.

let op = try! RealmOperator() // Creates a new RealmOperator from the default Realm.

Or

let op = RealmOperator(realm: myCustomRealm) // Creates a new RealmOperator from the given Realm.

WriteTransaction

WriteTransaction provides an interface for interacting with a Realm during a write transaction. It should only every be used inside a RealmOperator write block.

Here’s an example of how to save a struct to a Realm:

let person = Person(name: "John Doe", age: 20, id: UUID().uuidString)

let op = try! RealmOperator()

try! op.write { (writeTransaction) in
    try! writeTransaction.add(person, update: true)
}

WriteTransaction takes Persistable instances directly, and performs the necessary actions to save it to the Realm. No extra work required.

QPResults

QPResults is a special wrapper over Realm’s Results that provides an easy way to get persisted values back in their original form.

Here’s an example of getting values from a Realm:

let op = try! RealmOperator()
let results = op.values(ofType: Person.self)
let person = results[0] // Returns an instance of Person.

Or if you can get a specific value with an id:

let person = op.value(ofType: Person.self, withPrimaryKey: id)

Installation

Requirements

  • iOS 8.0+
  • macOS 10.9+
  • tvOS 9.0+
  • watchOS 2.0+
  • Swift 4

CocoaPods

QuickPersist is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'QuickPersist', '~> 1.0'

Note: QuickPersist requires RealmSwift.

Contributing

Open an issue or submit a pull request.

Latest podspec

{
    "name": "QuickPersist",
    "version": "1.0.1",
    "summary": "Easily save Structs to Realm.",
    "description": "QuickPersist lets you easily save any data type to a Realm database.",
    "homepage": "https://github.com/cszatma/QuickPersist",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Christopher Szatmary": "[email protected]"
    },
    "source": {
        "git": "https://github.com/cszatma/QuickPersist.git",
        "tag": "1.0.1"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source_files": "Sources/**/*.{swift}",
    "frameworks": "Foundation",
    "dependencies": {
        "RealmSwift": []
    },
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This