Skip to content

DataManager is a lightweight Core Data utility written in Swift.

License

Notifications You must be signed in to change notification settings

metova/DataManager

Repository files navigation

DataManager

Build Status CocoaPods Compatible Documentation Coverage Status Platform Twitter

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 public NSMainQueueConcurrencyType 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.

Requirements

  • iOS 12.0
  • Swift 5.0

Installation

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'

Usage

Setup

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.

Fetching

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)

Deleting

DataManager.delete([user1, user2], in: DataManager.mainContext)

Saving

DataManager.persist(synchronously: false)

Child Contexts

let backgroundContext = DataManager.createChildContext(withParent: DataManager.mainContext)

backgroundContext.perform {
    /* Do heavy lifting in the background */
}

Credits

DataManager is owned and maintained by Metova Inc.

Contributors

License

DataManager is available under the MIT license. See the LICENSE file for more info.

Alternatives

About

DataManager is a lightweight Core Data utility written in Swift.

Resources

License

Stars

Watchers

Forks

Packages

No packages published