DataManager is a lightweight Core Data utility. It is not a replacement/wrapper for Core Data. Here are some of the highlights:
- Encapsulates the boilerplate associated with setting up the Core Data stack.
- Sets up the stack with a private
NSPrivateQueueConcurrencyType
context as the root context with a publicNSMainQueueConcurrencyType
child context. This setup allows for asynchronous saves to disk. - Provides Swift-friendly convenience fetching methods that make use of generics to prevent you from having to handle downcasting from
NSManagedObject
to the entity's class every time you perform a fetch.
- iOS 12.0
- Swift 5.0
DataManager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'DataManager'
If you would like to test a beta version of DataManager, you can install the latest from develop:
pod 'DataManager', :git => 'https://github.com/metova/DataManager.git', :branch => 'develop'
When your app is launched, set up DataManager
with the data model name and a name for the persistent store file:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
DataManager.setUp(withDataModelName: "MyApp", bundle: .main, persistentStoreName: "MyApp")
/* ... */
return true
}
This won't set up the Core Data stack right away. The stack is lazy loaded when DataManager.mainContext
is first used.
DataManager uses generics so you don't have to worry about casting the NSManagedObject
results to the entity's class every time you perform a fetch. For example, the type of olderUsers
below is [User]
.
let predicate = NSPredicate(format: "\(#keyPath(Person.birthDate)) < %@", someDate)
let olderUsers = DataManager.fetchObjects(entity: User.self, predicate: predicate, context: DataManager.mainContext)
DataManager.delete([user1, user2], in: DataManager.mainContext)
DataManager.persist(synchronously: false)
let backgroundContext = DataManager.createChildContext(withParent: DataManager.mainContext)
backgroundContext.perform {
/* Do heavy lifting in the background */
}
DataManager is owned and maintained by Metova Inc.
DataManager is available under the MIT license. See the LICENSE file for more info.
- CoreDataMate by Todd Grooms is essentially the original Objective-C version that DataManager evolved from.