Giter VIP home page Giter VIP logo

philipsstack's Introduction

PhiLipsStack - ϕ:lips: - embrace CoreData

License Platform Language Issues

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()

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 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"

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

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 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', :git => 'https://github.com/phimage/PhiLipsStack.git' 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

mogenerator, PhiLipsStack, QueryKit

philipsstack's People

Contributors

phimage avatar

Watchers

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