GithubHelp home page GithubHelp logo

taketin / barrel Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tarunon/barrel

1.0 2.0 0.0 162 KB

Type-safe CoreData library for Swift.

License: MIT License

Ruby 1.01% Swift 98.11% C++ 0.88%

barrel's Introduction

Barrel

A simple type-safe CoreData library for Swift.

##Installation

platform :ios, "8.0"
use_frameworks!

pod 'Barrel', :git => 'https://github.com/tarunon/Barrel.git'

##Summary

Swift is a type-safe programing language and supported type-inference.

Before

func findPerson(age: Int) -> [Person] {
  let fetchRequest = NSFetchRequest(entityName: "Person")
  fetchRequest.predicate = NSPredicate(format: "age == %i", age)
  fetchRequest.sortDescriptor = [NSSortDescriptor(key: "name", ascending: true)]
  return context.executeFetchRequest(fetchRequest, error: nil) as? [Person] ?? []
}

func createPerson(name: String, age: Int) -> Person {
  let person = NSEntityDescription.insertNewObjectForEntityForName("Person", inManagedObjectContext: context) as! T
  person.name = name
  person.age = age
  return person
}

After

func findPerson(age: Int) -> [Person] {
  return context.fetch()
    .filter{ $0.age == age }
    .orderBy{ $0.name < $1.name }
    .execute().all()
}

func createPerson(name: String, age: Int) -> Person {
  return context.insert().setValues{
    $0.name = name
    $0.age = age
    }.insert()
}

##Fetch

Barrel provides fetch methods.

###Type-safe Barrel's fetch method is type-safe.

let persons = context.fetch(Person).execute().all()

If the type is defined, fetch's argument can be omitted.

let persons: [Person] = context.fetch().execute().all()

###Enum value A result of Barrel's fetch is Enum value Array or NSError.

let personsResult = context.fetch(Person).execute()
switch personsResult {
case .Succeed(let persons):
    // case of Array of Person
case .Failed(let error):
    // case of NSError
}

###Method chaining Barrel is defined condition of fetch using method chaining.

let persons = context.fetch(Person)
  .filter(NSPredicate(format: "name == %@", "John"))
  .orderBy(NSSortDescriptor(key: "age", ascending: true))
  .execute().all()

###Closure Barrel can be defined condition of fetch using closure.

let persons = context.fetch(Person)
  .filter{ $0.name == "John" }
  .orderBy{ $0.age < $1.age }
  .execute().all()

##Insert

Barrel provides insert methods. Type-safe and Closure support.

let person = context.insert(Person).setValues{
  $0.name = "John"
  $0.age = 24
  }.insert()

And support getOrInsert method.

let person = context.insert(Person).setValues{
  $0.name = "John"
  $0.age = 24
  }.getOrInsert()

##Others

Barrel has more functions.

###Aggregate and Grouping

Support aggregate method,

let maxAge = context.fetch(Person)
  .aggregate{ $0.max($1.age) }
  .execute().get()!
// maxAge => ["maxAge": XX]

and grouping.

let maxAgePerName = context.fetch(Person)
  .aggregate{ $0.max($1.age) }
  .aggregate{ $1.name }
  .groupBy{ $1.name }
  .execute().all()
// maxAgePerName => [["maxAge" : XX, "name": "YY"], ...]

###ResultsController NSFetchResultsController is not also type-safe. Barrel supports Type-safe ResultsController object.

let resultsController: ResultsController<Person> = context.fetch(Person)
  .orderBy{ $0.age < $1.age }
  .resultsController(sectionKeyPath: nil, cacheName: nil)
let person = resultsController.objectAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))

barrel's People

Contributors

tarunon avatar

Stargazers

 avatar

Watchers

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