Latest 2.0.0
Homepage https://github.com/phimage/PhiLipsStack
License MIT
Platforms ios 8.0, osx 10.9
Authors

PhiLipsStack – ϕ:lips: – embrace CoreData

License Platform Language Issues Cocoapod

PhiLipsStack PhiLipsStack aims to create a CoreData stack from model to context and provide some functions on your managed object which use by default the default stack context but not only

let context = NSManagedObjectContext.defaultContext
var object: MyManagedObject = MyManagedObject.create()
object.delete()

With PhiLipsStack you should use QueryKit to create query and the command line tool mogenerator to generate your NSManagedObject from model

Contents

Stack and optional configuration

A CoreDataStack is composed of three elements

and initialized with persistance store type and optionnal URL

var myStack = CoreDataStack(storeType: .SQLite, storeURL: anURL)

A default one is accessible with SQLite type and your application directory

CoreDataStack.defaultStack

The model

The stack use your application name as model name (ex: model file MyAppName.xcdatamodel)
If your model have another name, you can set your own model name by calling myStack.modelName = "MyModelName"

CoreDataStack.defaultStack.modelName = "MyModelName"

:warning: This must be done before requesting any of managedObjectContext, persistentStoreCoordinator, managedObjectModel or calling framework functions

The persistance store coordinator

By default the persistance store coordinatore is initialized with automigrate option
To change this behaviour set stack autoMigrate to false

If not able to load data from current persistance files, all data are removed (removeStore()) and new empty files are created.
To change this behaviour set removeIncompatibleStore to false

The managed object context

The default context can be accessed by class variable on NSManagedObjectContext

NSManagedObjectContext.defaultContext

This default context is the managedObjectContext attribute of default stack CoreDataStack.defaultStack. So you can change the defaultStack by your own if necessary

Play with managed objects: CRUD

Create

Your NSManagedObject must contains @objc(classname) or you must override entityName class var

@objc(MyEntity)
class MyEntity: NSManagedObject {
    @NSManaged var title: String
    @NSManaged var valid: NSNumber
}

You could use the command line tool mogenerator to generate your NSManagedObject from model

Then to create an object in default context

var entity = MyEntity.create()
var entity = MyEntity.createWithAttribute([key:value])

To create only if not exists, two useful functions

var entity: MyEntity = MyEntity.findFirstOrCreate()
var anotherEntity: MyEntity.findFirstOrCreateWithPredicate(aPredicate)

Read/Fetch

Get all object of one type

if let entities = MyEntity.all() ? [Entity] { .. }
let entityCount = MyEntity.count()

Some basic filtering using NSPredicate

if let entities = MyEntity.find(predicate) ? [Entity] { .. }
let entityCount = MyEntity.count(predicate)

For more advanced fetch with predicates, you should use QueryKit.

let myEntityQuerySet = QuerySet<MyEntity>(NSManagedObjectContext.defaultContext, MyEntity.entityName)

There is a mogerator templates for swift here: machine.swift.motemplate

Update and Save

Update your objects as usual by modifying attributes and relations, then you can save immediately your object

entity.save()

But it is recommanded to save the context when application will terminate or did enter background – see application delegate example

stack.save()

Delete

Delete object is as simple as following

entity.delete()

You can also delete all objects of specific type

Entity.deleteAll()

Error handling

Most of the functions provided by this framework allow to pass an error handler, a block of type (NSError) -> Void

entity.delete { (error) -> () in

}
Entity.find(NSPredicate(value: true)) { (error) -> () in

}

If no error handler is provided, you can access the last error handled by the stack

if let error = myStack.lastError {..}

At application start you can also check stack validity (context not nil)

if !myStack.valid() {
   // log and application shutdown
}
// or with error handler
myStack.valid((error: NSError)  in {
    // log and application shutdown
}

Setup

Using xcode project

  1. Drag PhiLipsStack.xcodeproj to your project/workspace or open it to compile it
  2. Add the PhiLipsStack framework to your project

Using cocoapods

Add pod 'PhiLipsStack' to your Podfile and run pod install.

Add use_frameworks! to the end of the Podfile.

Licence

The MIT License (MIT)

Copyright (c) 2015 Eric Marchand (phimage)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Misc

haroopad icon Readme done with Haroopad

Logo

Inspired by apple swift logo

Why a logo?

I like to see an image for each of my project when I browse them with SourceTree

Core Data Combo

PhiLipsStack
mogenerator, PhiLipsStack, QueryKit

Latest podspec

{
    "name": "PhiLipsStack",
    "version": "2.0.0",
    "summary": "A Swift library to init Core Data Stack",
    "description": "PhiLipsStack is a Swift library useful CoreDataStack object that providesn* managedObjectContextn* persistentStoreCoordinatorn* managedObjectModelnAnd functions on `NSManagedObject` which use by default a context from default stack",
    "homepage": "https://github.com/phimage/PhiLipsStack",
    "license": "MIT",
    "authors": {
        "phimage": "[email protected]"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source": {
        "git": "https://github.com/phimage/PhiLipsStack.git",
        "tag": "2.0.0"
    },
    "source_files": "PhiLipsStack/*.swift",
    "resources": "logo-128x128.png"
}

Pin It on Pinterest

Share This