Persist is a simple, reusable Core Data stack written in Swift. It also provides some lightweight helpers for common actions such as creating entities and querying.
It supports iOS 8+, watchOS 2.0, and Mac OS 10.9+.
Persist uses a simple parent/child Core Data stack, with a private writer context attached directly to the NSPersistentStoreCoordinator, with a main queue context that is exposed to the user as its child.
┌─────────────────────────────────────────────────────────────────────────┐
│ ┌─────────────────────────────────┐ │
│ │ Persistent Store Coordinator │ │
│ └────────────────┬────────────────┘ │
│ │ │
│ │ │
│ ┌────────────────▼────────────────┐ │
│ │ Private Queue Context │ │
│ └────────────────┬────────────────┘ │
│ │ │
│ │ │
│ ┌────────────────▼────────────────┐ ┌─────────────────┐ │
│ │ Main Queue Context ├─────────▶ User Interface │ │
│ └────────────────┬────────────────┘ └─────────────────┘ │
│ │ │
│ │ │
│ ┌────────────────▼────────────────┐ ┌────────────────────────┐ │
│ │ Temporary Private Queue Context ◀─────────┤ Writes/Updates │ │
│ └─────────────────────────────────┘ └────────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
## Usage
Persist gives you full access to the configuration of its underlying stores
via the StoreType
enum, providing safe construction of InMemory, SQLite, and
Binary store types, and allows you to also provide a custom store type.
Creating an in-memory store would look like this:
import Persist
let bundle = NSBundle.mainBundle()
let modelPath = bundle.pathForResource("MyModel", ofType: "momd")!
let modelURL = NSURL(fileURLWithPath: modelPath)
let persist = try! Persist(storeType: .InMemory, modelURL: modelURL)
You can see more examples in PersistTests/PersistStoreBasedTests.swift
To opt your NSManagedObject subclasses into the helpers, annotate them with the ManagedObjectType protocol like so:
@objc(Person)
class Person: ManagedObjectType {
}
If you don't provide an objective-c name for your class, or update your model
configuration to support module namespaces, you will need to provide your own
implementation of static func entityName() -> String
.
- Add
github "endocrimes/Persist"
to your Cartfile - Run
$ carthage update
- Add the frameworks to your project.
- Add
pod "Persist"
to your podfile - Run
$ pod install
Issues and feature requests are welcome, although the intention is to keep Persist lightweight.
- Fork it ( https://github.com/endocrimes/Persist/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request