I created this wrapper mostly to ease my workflow in the projects, if you find it useful feel free to use it and modify it to your needs.
- Delete.
- DeleteBatchAsync
- UpdateBatchAsync
- InsertBatchAsync
- getAsync (fetch async)
- get (fetch)
- UndoManager
- Lightweight migrations.
- Multiple store configurations
- Aggregate functions
Check out the Documentation for more information.
First call NSPersist.setup(withName: "<#Model name#>")
or NSPersist.setup(withName: "<#Model name#>", configurations: ["<# Configuration name #>"])
to provide the name of the data model to be used and additional configurations.
This typically is called in AppDelegate didFinishLaunchingWithOptions
once.
Example add record:
let note = NSExampleNote(context: .main)
note.title = textFieldTitle.text
note.body = textFieldNote.text
note.createdAt = Date()
note.updatedAt = Date()
note.save()
As you can see the main context is accessible via .main
property, and note.save()
inserts in the main context by default, or you can specify another context in its parameter like this note.save(context: backgroundContext)
.
Example usage of get request or fetch
NSPersist.shared.request(NSExampleNote.self, completion: { (request) in
request.predicate = NSPredicate(format: "favorite = %d", true)
request.sortDescriptors = [.init(key: "createdAt", ascending: false)]
}).get()
As I said this is a lightweight wrapper, you are still working with the NSFetchRequest
that you get from the completion block.
Currently NSPersist is only available through Swift Package Manager.
dependencies: [
.package(url: "https://github.com/MartinStamenkovski/NSPersist.git", from: "1.1.0")
]
NSPersist is released under MIT License