Skip to content
Small and fast CoreData stack written in Swift
Swift Ruby Objective-C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CoreDuck
Example
.gitignore
.travis.yml
CoreDuck.podspec
LICENSE
README.md
_Pods.xcodeproj

README.md

CoreDuck

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 8.0+
  • macOS 10.10+
  • Swift 3.0+

Installation

CoreDuck is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'CoreDuck'

Initialization

import CoreDuck
// Init CoreDuck
let _ = CoreDuck.quack

If you want to see errors in console during development:

CoreDuck.printErrors = true

If you want to specifi name of CoreData *.xcdatamodeld file ("CoreData" by default):

/// Name of your *.xcdatamodel file
CoreDuck.coreDataModelName = "YourModelName"

Contexts

/// Main NSManagedObjectContext of the app.
/// Primary usage is UIKit, works on the main thread of the app.
/// It's a singleton - always returns the same instance.
let context = NSManagedObjectContext.main

/// Background NSManagedObjectContext.
/// Returns new instance of NSManagedObjectContext each time you access this variable.
/// Use it for persisting changes to CoreData.
let backgroundContext = NSManagedObjectContext.background

Saving data

Asynchronously:

NSManagedObjectContext.saveWithBlock({ context in
// your code goes here
}, completion: { success in
// completion block
})

Synchronously:

NSManagedObjectContext.saveWithBlockAndWait({ context in
// your code goes here
}, completion: { success in
// completion block
})

Creating objects

To create a new Core Data object in specified context:

if let newEntity = context.new(Entity.self) {
  // your code goes here
}

Getting object in context

Get reference to NSManagedObject instance in context:

if let entityInContext = context.get(entity) {
// your code goes here
}

Deleting objects

Entity.deleteAllObjects()

Fetching Entities

Basic search

As an example, let's assume that you have an entity named Person. You can retrieve all Person entities from your persistent store using the following function:

let people = context.findAll(entity: Person.self)

To return the same entities sorted by a specific attribute:

let people = context.findAll(entity: Person.self, sortedBy: "name", ascending: true)

If you want to find object in Core Data by attribute, you can use following functions:

let people = context.findFirst(entity: Person.self, by: "name", with: "John")
let people = context.findFirst(entity: Person.self, by: "officeID", with: 7)

Advanced search

If you want to execute more accurate search request, you can use predicates:

let people = context.findAll(entity: Person.self, with: NSPredicate(format: "entityID IN %@", peopleIDs))

NSFetchedResultsController

let people = context.fetchAll(entity: Person.self, sortedBy: "entityID", ascending: true, delegate: self)
let people = context.fetchAll(entity: Person.self, with: predicate, sortedBy: "entityID", ascending: true, delegate: self)
let people = context.fetchAll(entity: Person.self, by: "officeID", with: 7, sortedBy: "entityID", ascending: true, delegate: self)

Authors

License

CoreDuck is available under the MIT license. See the LICENSE file for more info.

You can’t perform that action at this time.