MartinStamenkovski / NSPersist

Lightweight Core Data wrapper in Swift

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A simple lightweight Core Data wrapper in Swift.

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.

Currently NSPersist supports:

  • Delete.
  • DeleteBatchAsync
  • UpdateBatchAsync
  • InsertBatchAsync
  • getAsync (fetch async)
  • get (fetch)
  • UndoManager
  • Lightweight migrations.
  • Multiple store configurations
  • Aggregate functions

Usage

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.

Instalation

Currently NSPersist is only available through Swift Package Manager.

dependencies: [
   .package(url: "https://github.com/MartinStamenkovski/NSPersist.git", from: "1.1.0")
]

License

NSPersist is released under MIT License

About

Lightweight Core Data wrapper in Swift

License:MIT License


Languages

Language:Swift 100.0%