Git Product home page Git Product logo

coreduck's Introduction

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 12.0+
  • macOS 10.10+
  • Swift 5.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.

coreduck's People

Contributors

yurii-voievodin avatar skliarov avatar gomer0513 avatar

Watchers

James Cloos 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.