Skip to content

Fetching Models

Jeff Hurray edited this page Apr 25, 2016 · 1 revision

Models conforming to SQLiteModel have static methods that allow them to fetch models from the database.

###Find

find allows you to query the database by primary key for a single row. It takes an SQLiteModelID (Int64) as a parameter. It returns an optional instance of the model type.

let firstRowInMovieDB: Movie? = try Movie.find(1)

###Fetch All

fetchAll will fetch all rows from the table associated with the model and convert them to the instances model type. It returns an array of the model type.

let allMovies = try Movie.fetchAll()

###Fetch

fetch takes a Query parameter will fetch all rows from the table associated with the model that satisfy the query, and convert them to the instances model type. It returns an array of the model type.

let query = Movie.query.order(Movie.Gross.desc).limit(5)
let top5GrossingMovies = try Movie.fetch(query)

Query is a type declared in SQLite.swift. More info can be found here

Clone this wiki locally