Awesome
(deprecated, please do not use)
PhiLipsStack - ϕ:lips: - embrace CoreData
<img align="left" src="logo-128x128.png" hspace="20"> 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
- Drag PhiLipsStack.xcodeproj to your project/workspace or open it to compile it
- 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
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