pajai / JSQCoreDataKit

A swifter Core Data stack

Home Page:http://www.jessesquires.com/JSQCoreDataKit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSQCoreDataKit

Build Status Version Status license MIT Platform Carthage compatible

A swifter Core Data stack

About

This framework 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

For more information on Core Data:

Requirements

  • iOS 8
  • Swift 1.2

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

// Initialize a default stack
let stack = CoreDataStack(model: model)

// Initialize a private queue, in-memory stack
let privateStack = CoreDataStack(model: model, storeType: NSInMemoryStoreType, options: nil, concurrencyType: .PrivateQueueConcurrencyType)

Saving a managed object context

// Saving returns a tuple (Bool, NSError?)
let result: ContextSaveResult = saveContextAndWait(stack.managedObjectContext)
if !result.success {
    // save failed
    println("Save error: \(result.error)")
}

Deleting the store

let model = CoreDataModel(name: "MyModel", bundle: NSBundle(identifier: "com.MyApp.MyModelFramework")!)
model.removeExistingModelStore()

Checking migrations

let model = CoreDataModel(name: "MyModel", bundle: NSBundle(identifier: "com.MyApp.MyModelFramework")!)
let needsMigration: Bool = model.modelStoreNeedsMigration

Using child contexts

let model = CoreDataModel(name: "MyModel", bundle: NSBundle(identifier: "com.MyApp.MyModelFramework")!)
let stack = CoreDataStack(model: model)

// Create a default child context on main queue
let childContext = stack.childManagedObjectContext()

// Create a private queue child context with custom merge policy
let privateChildContext = stack.childManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType, mergePolicyType: .ErrorMergePolicyType)

Fetching

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

// Fetching returns a FetchResult<T>
let result = fetch(request: request, inContext: context)

if !result.success {
    println("Error = \(result.error)")
}

// use objects, [MyModel]
result.objects

Deleting

let objects: [MyModel] = /* array of MyModel objects */

deleteObjects(objects, inContext: context)

// Commit changes, remove objects from store
saveContextAndWait(context)

Unit tests

There's a suite of unit tests for JSQCoreDataKit.framework. To run them, open JSQCoreDataKit.xcworkspace, select the JSQCoreDataKit scheme, then ⌘-u. Additional tests are under the Example scheme.

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

This project borrows some ideas and concepts from my work on RSTCoreDataKit.

License

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

Copyright © 2015 Jesse Squires.

Please provide attribution, it is greatly appreciated.

About

A swifter Core Data stack

http://www.jessesquires.com/JSQCoreDataKit

License:MIT License


Languages

Language:Swift 96.4%Language:Objective-C 2.1%Language:Ruby 1.4%