Latest 1.2.1
Homepage https://github.com/appdev-academy/CoreDuck
License MIT
Platforms ios 8.0, osx 10.10, requires ARC
Frameworks Foundation, CoreData
Authors ,

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

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.

Latest podspec

{
    "name": "CoreDuck",
    "version": "1.2.1",
    "summary": "Small and fast CoreData stack written in Swift",
    "homepage": "https://github.com/appdev-academy/CoreDuck",
    "license": "MIT",
    "authors": {
        "Maksym Skliarov": "[email protected]",
        "Yura Voevodin": "[email protected]"
    },
    "source": {
        "git": "https://github.com/appdev-academy/CoreDuck.git",
        "tag": "1.2.1"
    },
    "social_media_url": "https://twitter.com/AppDev_Academy",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10"
    },
    "source_files": "CoreDuck/Classes/**/*",
    "frameworks": [
        "Foundation",
        "CoreData"
    ],
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This