Coder Social home page Coder Social logo

querykit's Introduction

QueryKit

Build Status

QueryKit, a simple CoreData query language for Swift.

Usage

Person.queryset(context).filter(Person.name == "Kyle").delete()

Querying

To retrieve objects from CoreData with QueryKit, you can construct a QuerySet for your model in a managed object context.

A queryset is an immutable object, any operation will return a new queryset instead of modifying the queryset.

var queryset = Person.queryset(context)

Filtering

You can filter a queryset using the filter and exclude methods, which accept a predicate and return a new queryset.

queryset.filter(NSPredicate(format:"name == %@", "Kyle"))
queryset.filter(Person.name == "Kyle")
queryset.exclude(Person.age < 21)

Ordering

You can order a queryset's results by using the orderBy method which accepts a collection of sort descriptors:

queryset.orderBy(NSSortDescriptor(key: "name", ascending: true))
queryset.orderBy(Person.name.ascending)
queryset.orderBy([Person.name.ascending, Person.age.descending])

Slicing

You can use slicing to limit a queryset to a range. For example, to get the first 5 items:

queryset[0..5]

Fetching

Single object

var kyle = queryset.filter(Person.name == "Kyle").get()

Object at index

var orta = queryset[3]

Count

println("There are \(queryset.count() - 1) more Kyle's.")

Iteration

for person in queryset {
    println(person.name)
}

Conversion to an array

queryset.array()

Deleting

This method immediately deletes the objects in your queryset and returns a count and an error if the operation failed.

queryset.delete()

Predicate extensions

We've extended NSPredicate to add support for the !, && and || operators for joining predicates together.

NSPredicate(format:"name == Kyle") || NSPredicate(format:"name == Katie")
NSPredicate(format:"age >= 21") && !NSPredicate(format:"name == Kyle")
Person.name == "Kyle" || Person.name == "Katie"
Person.age >= 21 || Person.name != "Kyle"

License

QueryKit is released under the BSD license. See LICENSE.

querykit's People

Contributors

kylef avatar

Watchers

James Cloos avatar Janak Nirmal 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.