A swifter Core Data stack
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:
- iOS 8
- Swift 1.2
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"
- Clone this repo and add the
JSQCoreDataKit.xcodeproj
to your project - Select your project app target "Build Phases" tab
- Add the
JSQCoreDataKit.framework
to the "Link Binary With Libraries" - Create a new build phase of type "Copy Files" and set the "Destination" to "Frameworks"
- 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.
Read the fucking docs. Generated with jazzy. Hosted by GitHub Pages.
More information on the gh-pages branch.
import JSQCoreDataKit
// 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 returns a tuple (Bool, NSError?)
let result: ContextSaveResult = saveContextAndWait(stack.managedObjectContext)
if !result.success {
// save failed
println("Save error: \(result.error)")
}
let model = CoreDataModel(name: "MyModel", bundle: NSBundle(identifier: "com.MyApp.MyModelFramework")!)
model.removeExistingModelStore()
let model = CoreDataModel(name: "MyModel", bundle: NSBundle(identifier: "com.MyApp.MyModelFramework")!)
let needsMigration: Bool = model.modelStoreNeedsMigration
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)
// 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
let objects: [MyModel] = /* array of MyModel objects */
deleteObjects(objects, inContext: context)
// Commit changes, remove objects from store
saveContextAndWait(context)
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.
Please follow these sweet contribution guidelines.
Created and maintained by @jesse_squires
This project borrows some ideas and concepts from my work on RSTCoreDataKit.
JSQCoreDataKit
is released under an MIT License. See LICENSE
for details.
Copyright © 2015 Jesse Squires.
Please provide attribution, it is greatly appreciated.