Latest 0.3.1
Homepage https://github.com/mattcomi/PersistentObject
License MIT
Platforms ios 8.0, osx 10.9, requires ARC
Authors


Carthage compatible

Platform

Simple object persistence in Swift.

Includes support for NSUserDefaults, NSUbiquitousKeyValueStore and the file system.

API Documentation

Getting Started

To persist an object, initialize a PersistentObject with the desired repository. For example, to persist a Vehicle to a file:

let persistentVehicle = PersistentObject<Vehicle>(filename: "file.txt")

Or, to persist a Person to the NSUserDefaults database:

let persistentPerson = PersistentObject<Person>(userDefaultsKey: personKey)

If a PersistentObject exists in the repository, that is, if it has been persisted previously, it will be unarchived and initialized. To access the underlying object:

if let person = persistentPerson.object {
  print("Hi, my name is (person.name)")
}

Initialization

If a PersistentObject does not yet exist in the repository, you will need to initialize it yourself:

if persistentPerson.object == nil {
    persistentPerson.reset(Person(name: "Brian Doyle-Murray"))
}

Saving and Synchronization

The underlying object is automatically archived to its repository when the app enters the background and when the PersistentObject is deinitialized. You may also trigger it manually:

persistentPerson.save()

You may also synchronize the repository:

persistentPerson.synchronize()

Manual synchronization is typically only necessary when:

  1. The underlying repository is a UbiquituousKeyValueStoreRepository
  2. You require fast-as-possible upload to iCloud after changing the object

Repository

The follow repositories are supported currently:

  • FileRepository: Persists to a file
  • UbiquituousKeyValueStoreRepository: Persists to the NSUbiquituousKeyValueStore
  • UserDefaultsRepository: Persists to the NSUserDefaults database

These repositories require that the object being persisted is NSCoding compliant.

External Changes

A Repository may support external changes. For example, when using the UbiquituousKeyValueStoreRepository, it is possible for the value to change in iCloud. If an external change occurs, the PersistentObject‘s underlying object is replaced, invalidating any references. To be notified when this occurs, provide a delegate when initializing the PersistentObject:

let delegate = PersistentObjectDelegate<Person>()

delegate.objectChangedExternally = { (persistentObject) in
  // handle the external change
}

let p = PersistentObject<Person>(
  ubiquituousKeyValueStoreKey: "personKey",
  delegate: delegate)

Custom Repository

To provide a custom repository, you may implement the Repository protocol:

public protocol Repository {
  associatedtype ObjectType
  var delegate: RepositoryDelegate<ObjectType> { get }
  func archiveObject(object: ObjectType?)
  func unarchiveObject() -> ObjectType
  func synchronize()
}

Then, to initialize a PersistentObject with that Repository:

let p = PersistentObject<Person>(repository: MyCustomRepository())

Latest podspec

{
    "name": "PersistentObject",
    "version": "0.3.1",
    "summary": "Simple object persistence in Swift.",
    "homepage": "https://github.com/mattcomi/PersistentObject",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Matt Comi": "[email protected]"
    },
    "source": {
        "git": "https://github.com/mattcomi/PersistentObject.git",
        "tag": "0.3.1"
    },
    "source_files": "PersistentObject/*.{swift}",
    "requires_arc": true,
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    }
}

Pin It on Pinterest

Share This