Skip to content

RomualdPercereau/JSQCoreDataKit

 
 

Repository files navigation

JSQCoreDataKit

Build Status Version Status license MIT codecov.io Platform Carthage 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

  • iOS 8+
  • Swift 2.0

Installation

use_frameworks!

# For latest release in cocoapods
pod 'JSQCoreDataKit'

# Feeling adventurous? Get the latest on develop
pod 'JSQCoreDataKit', :git => 'https://github.com/jessesquires/JSQCoreDataKit.git', :branch => 'develop'
github "jessesquires/JSQCoreDataKit"

Manually

  1. Clone this repo and add the JSQCoreDataKit.xcodeproj to your project
  2. Select your project app target "Build Phases" tab
  3. Add the JSQCoreDataKit.framework to the "Link Binary With Libraries"
  4. Create a new build phase of type "Copy Files" and set the "Destination" to "Frameworks"
  5. Add the JSQCoreDataKit.framework and check "Code Sign On Copy"

For an example, see the demo project included in this repo.

For more information, see the Framework Programming Guide.

Documentation

Read the fucking 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
let bundle = NSBundle(identifier: "com.MyApp.MyModelFramework")!
let model = CoreDataModel(name: "MyModel", bundle: bundle)

// Initialize a stack with a factory
let factory = CoreDataStackFactory(model: model)

let stack: CoreDataStack?
factory.createStackInBackground { (result: CoreDataStackResult) in
    switch result {
        case .Success(let s):
            stack = s

        case .Failure(let e):
            print("Error: \(e)")
    }
}

In-memory stacks for testing

let inMemoryModel = CoreDataModel(name: myName, bundle: myBundle, storeType: .InMemory)
let factory = CoreDataStackFactory(model: inMemoryModel)
let stack = factory.createStack()

Saving a managed object context

saveContext(stack.mainContext) { result in
    switch result {
        case .Success:
            print("save succeeded")

        case .Failure(let error):
            print("save failed: \(error)")
    }
}

Deleting the store

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

Checking migrations

let bundle = NSBundle(identifier: "com.MyApp.MyModelFramework")!
let model = CoreDataModel(name: "MyModel", bundle: bundle)
if model.needsMigration {
    // do migration
}

Using child contexts

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

Fetching

// Create a FetchRequest<T>, where T is a phantom type
let entity = entity(name: "MyModel", context: context)!
let request = FetchRequest<MyModel>(entity: entity)

var results = [MyModel]()
do {
    results = try fetch(request: request, inContext: context)
}
catch {
    print("Fetch error: \(error)")
}

print("Results = \(results)")

Deleting

let objects: [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 ExampleApp/ 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. To run them, open JSQCoreDataKit.xcworkspace, select the JSQCoreDataKit scheme, then ⌘-u.

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 Jesse Squires.

Please provide attribution, it is greatly appreciated.

Packages

No packages published

Languages

  • Swift 98.2%
  • Objective-C 1.1%
  • Ruby 0.7%