A very light-weight CoreData wrapper that dynamically re-writes itself, as you develop your project.
Fetch
do {
let performances: [Performance] = try Performance.Query(group: .solo).all(in: context)
performances.forEach { (performance) in
context.delete(performance)
}
try context.save()
} catch {
// Errors from the CoreData layer such as 'model not found' etc
}
Create
struct Information {
let name: String
let phone: String
let email: String
let type: String
// implement protocol named `Recordable` here ~ 2 minutes
}
let info = Information(name: "DanceSchool", phone: "01234567891", email: "[email protected]", type: "School")
do {
let record: Party = try info.record(in: context)
} catch {
// Errors from the CoreData layer such as 'model not found' etc
}
When using the protocol named Recordable
we ensure the following is performed when we call record(in: )
.
- If a record does not exist that matches this data, it is created.
- If a record does exist that matches this data, it is retrieved.
Observe
The following tableView is connected to an NSFetchedResultsController and UI changes are delegated to a default protocol implementation.
class PerformancesViewController: UIViewController {
private let fetchedResultsController: PerformancesFetchedResultsController!
@IBOutlet private weak var tableView: PerformancesTableView! {
didSet {
fetchedResultsController.delegate = tableView
fetchedResultsController.dataSource = tableView
tableView.dataSource = fetchedResultsController
tableView.delegate = fetchedResultsController
}
}
override func viewDidLoad() {
super.viewDidLoad()
try! fetchedResultsController.reload()
}
}
Installation
- Run the Carthage command
carthage update
. - Embed Binary.
- Install Sourcery.
- Adjust NSManagedObject subclasses
~2 minute job
.
Demo
- Run the Carthage command
carthage bootstrap
. - Install Sourcery.
- Run the Xcode scheme named
Performances
.
Footnotes
See footnotes for some handy tips.