Fetchable
A Swift protocol extension to make Core Data NSManagedObject subclasses more generic
Install / Use
/learn @stklieme/FetchableREADME
Fetchable
Fetchable is a Swift protocol extension to make Core Data NSManagedObject subclasses more generic (for macOS and iOS). It adds static methods for NSManagedObject subclasses to insert, fetch and delete objects without adding extra code to the subclasses except an enum to specify the sorting attribute keys.
Requirements:
- Swift 4.1
- Xcode 9+
- iOS 8.0+ / macOS 10.10+
Usage:
-
Add
Fetchable.swiftto your project and make allNSManagedObjectsubclasses adoptFetchable. -
Add an enum
AttributeNameto eachNSManagedObjectsubclass to specify the sorting attribute keys for exampleenum AttributeName : String { case name } -
Fetchablerequires a reference to theNSManagedObjectContextinstance.- If the project uses the default implementation of the Core Data stack in
AppDelegateyou are done. - If the Core Data stack is not located in
AppDelegateadd a computed read-only propertymanagedObjectContextinAppDelegateand return the reference to the managed object context. - If you are using Application Extensions which lacks an
AppDelegateclass change the reference directly in theFetchable.swiftfile.
- If the project uses the default implementation of the Core Data stack in
The default method to fetch data is (assuming there is a NSManagedObject subclass Foo)
do {
let objects = try Foo.objects()
} catch {
print(error)
}
It returns a non-optional array of Foo objects (no type cast needed).
All fetch methods can throw by passing through the Core Data error.
The objects can be sorted in three ways by
-
Passing one attribute key (default direction is
ascending)let objects = try Foo.objects(sortedBy : .name) let objects = try Foo.objects(sortedBy : .index, ascending: false) -
Passing an (variadic) array of tuples representing the
keyandascendingparameters of a sort descriptor respectivelylet objects = try Foo.objects(sortedBy : (.name, true), (.index, false)) -
Passing an array of
NSSortDescriptorinstances for more advanced sortinglet objects = try Foo.objects(sortedBy : [NSSortDescriptor(... , NSSortDescriptor(...])
Other (optional) parameters are
-
A predicate
let objects = try Foo.objects(for : NSPredicate(format...) -
A fetch limit
let objects = try Foo.objects(fetchLimit : 2)
A compound filter can be
let objects = try Foo.objects(for : NSPredicate(format...), sortedBy: .name, fetchLimit : 5)
This is much more convenient than creating the request, predicate and sort descriptor(s) manually.
There are also corresponding methods to return a single (optional) object
let object = try Foo.object()
and a method to get the number of found objects
let numberOfObjects = try Foo.objectCount()
The methods to insert a new object, get the entity description and delete all objects are very simple, too.
let newFoo = Foo.insertNewObject()let entityDescription = Foo.entityDescriptionFoo.deleteAll()
