GithubHelp home page GithubHelp logo

gjnilsen / jsqcoredatakit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jessesquires/jsqcoredatakit

0.0 1.0 0.0 4.09 MB

A swifter Core Data stack

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

License: Other

Swift 98.70% Objective-C 0.75% Ruby 0.55%

jsqcoredatakit's Introduction

JSQCoreDataKit

Build Status Version Status license MIT codecov 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
  • Make Core Data more Swifty
  • 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 cocoapods
pod 'JSQCoreDataKit'

# Feeling adventurous? Get the latest on develop
pod '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
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.createStack { (result: StackResult) 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: CoreDataStack?
factory.createStack { (result: StackResult) in
    switch result {
        case .Success(let s):
            stack = s

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

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.removeExistingStore()
} catch {
    print("Error: \(error)")
}

Performing migrations

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

Using child contexts

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

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

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 stack.mainContext.fetch(request: request)
}
catch {
    print("Fetch error: \(error)")
}

print("Results = \(results)")

Deleting

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

stack.mainContext.delete(objects: objects)

// 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.

jsqcoredatakit's People

Contributors

jessesquires avatar liquidsoul avatar tonyarnold avatar marius-serban avatar romualdpercereau avatar cgoldsby avatar

Watchers

GJ Zwart avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.