Giter VIP home page Giter VIP logo

expressible's Introduction

Expressible

Expressible library helps you to fetch data from CoreData.

Requirements

  • iOS 10.0+
  • Swift 4.2

Usage

Sample 1

let result1 = try context.from(City.self).count()

Equivalent to:

let request = NSFetchRequest<NSNumber>(entityName: "City")
request.resultType = .countResultType
let result2 = try context.fetch(request).first?.intValue ?? 0

Sample 2

let result1 = try context
  .from(City.self)
  .filter(\City.population > 1_000_000)
  .sort(by: \City.population, ascending: false)
  .sort(by: \City.name, ascending: true)
  .all()

Equivalent to:

let request = NSFetchRequest<City>(entityName: "City")
request.predicate = NSPredicate(format: "population > 1000000")
request.sortDescriptors = [NSSortDescriptor(key: "population", ascending: false), NSSortDescriptor(key: "name", ascending: true)]
let result2 = try context.fetch(request)

Sample 3

let result1 = try context
  .from(City.self)
  .group(by: [(\City.province?.name).as(String.self, name: "province")])
  .having(\City.province?.country?.name == "Belarus")
  .select([
          (\City.province?.name).as(String.self, name: "province"),
          (\City.population).sum.as(Int.self, name: "population")
          ])
  .all()

Equivalent to:

let request = NSFetchRequest<NSDictionary>(entityName: "City")
request.havingPredicate = NSPredicate(format: "province.country.name == %@", "Belarus")
request.resultType = .dictionaryResultType
			
let province = NSExpressionDescription()
province.expression = NSExpression(format: "province.name")
province.expressionResultType = .stringAttributeType
province.name = "province"
			
let population = NSExpressionDescription()
population.expression = NSExpression(format: "sum:(population)")
population.expressionResultType = .integer32AttributeType
population.name = "population"
			
request.propertiesToFetch = [province, population]
request.propertiesToGroupBy = [province]
			
let result2 = try context.fetch(request)

Sample 4

let result1 = try context
  .from(Country.self)
  .filter((\Country.provinces).subquery((\Province.cities).any(\City.population) > 10_000_000).count != 0)
  .all()

Equivalent to:

let request = NSFetchRequest<Country>(entityName: "Country")
request.predicate = NSPredicate(format: "SUBQUERY(provinces, $x, ANY $x.cities.population > 10000000).@count != 0")
let result2 = try context.fetch(request)

expressible's People

Contributors

mrdepth avatar

Watchers

 avatar  avatar

Forkers

ladeiko

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.