Skip to content

Latest commit

History

History
198 lines (143 loc) · 5.98 KB

File metadata and controls

198 lines (143 loc) · 5.98 KB

JSQCoreDataKit

Build StatusVersion Statuslicense MITcodecovPlatformCarthage compatible

A swifter Core Data stack

About

This library aims to do the following:

  • Provide better interoperability with Swift
  • Harness Swift features and enforce Swift paradigms
  • Bring functional paradigms to Core Data
  • Simplify the processes of standing up the Core Data stack
  • Aid in testing your Core Data models
  • Reduce the boilerplate involved with Core Data

Further reading on Core Data:

Requirements

  • Xcode 7.3+
  • iOS 8.0+
  • OSX 10.10+
  • tvOS 9.0+
  • watchOS 2.0+
  • Swift 2.2+

Installation

CocoaPods (recommended)

use_frameworks!# For latest release in cocoapodspod'JSQCoreDataKit'# Feeling adventurous? Get the latest on developpod'JSQCoreDataKit',:git=>'https://github.com/jessesquires/JSQCoreDataKit.git',:branch=>'develop'
github "jessesquires/JSQCoreDataKit"

Documentation

Read the docs. Generated with jazzy. Hosted by GitHub Pages. More information on the gh-pages branch.

Getting Started

import JSQCoreDataKit

Standing up the stack

// Initialize the Core Data model, this class encapsulates the notion of a .xcdatamodeld file
// The name passed here should be the name of an .xcdatamodeld file
letbundle=NSBundle(identifier:"com.MyApp.MyModelFramework")!
letmodel=CoreDataModel(name:"MyModel", bundle: bundle)
// Initialize a stack with a factory
letfactory=CoreDataStackFactory(model: model)letstack:CoreDataStack?
factory.createStackInBackground{(result:CoreDataStackResult)inswitch result {case.Success(let s):
stack = s
case.Failure(let e):print("Error: \(e)")}}

In-memory stacks for testing

letinMemoryModel=CoreDataModel(name: myName, bundle: myBundle, storeType:.InMemory)letfactory=CoreDataStackFactory(model: inMemoryModel)letstack= factory.createStack()

Saving a managed object context

saveContext(stack.mainContext){ result inswitch result {case.Success:print("save succeeded")case.Failure(let error):print("save failed: \(error)")}}

Deleting the store

letbundle=NSBundle(identifier:"com.MyApp.MyModelFramework")!
letmodel=CoreDataModel(name:"MyModel", bundle: bundle)do{try model.removeExistingModelStore()}catch{print("Error: \(error)")}

Performing migrations

letbundle=NSBundle(identifier:"com.MyApp.MyModelFramework")!
letmodel=CoreDataModel(name:"MyModel", bundle: bundle)if model.needsMigration {do{trymigrate(model)}catch{print("Failed to migrate model: \(error)")}}

Using child contexts

// Create a background queue child context from the main queue context
letchildContext= stack.childContext()

Fetching

// Create a FetchRequest<T>, where T is a phantom type
letentity=entity(name:"MyModel", context: context)!
letrequest=FetchRequest<MyModel>(entity: entity)varresults=[MyModel]()do{
results =tryfetch(request: request, inContext: context)}catch{print("Fetch error: \(error)")}print("Results = \(results)")

Deleting

letobjects:[MyModel]= /* array of MyModel objects */
deleteObjects(objects, inContext: context)
// Commit changes to remove objects from store
saveContext(context)

Example app

There's an example app in the Example/ directory. Open the ExampleApp.xcodeproj to run it. The project exercises all basic functionality of the library.

Unit tests

There's a suite of unit tests for JSQCoreDataKit.framework. You can run them in the usual way from Xcode by opening JSQCoreDataKit.xcodeproj. These tests are well commented and serve as further documentation for how to use this library.

Contribute

Please follow these sweet contribution guidelines.

Credits

Created and maintained by @jesse_squires.

License

JSQCoreDataKit is released under an MIT License. See LICENSE for details.

Copyright © 2015-present Jesse Squires.

Please provide attribution, it is greatly appreciated.