Skip to content

Instantly share code, notes, and snippets.

@dkw5877
Created March 18, 2019 18:51
Show Gist options
  • Select an option

  • Save dkw5877/25ef1939c2a6b319c607c9fee5894fc3 to your computer and use it in GitHub Desktop.

Select an option

Save dkw5877/25ef1939c2a6b319c607c9fee5894fc3 to your computer and use it in GitHub Desktop.
Example of singleton for SQLite using FMDB
class FMDBDatabase {
/*
* @description defines completion closure for database calls
*/
public typealias Completion = ((Bool, Error?)-> Void)
public typealias ResultCompletion = ((Bool, FMResultSet?, Error?)-> Void)
/*
* @description singleton to access the database
*/
static let sharedDatabase:FMDatabase = {
let fileURL = try! FileManager.default
.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
.appendingPathComponent("test.sqlite")
let database = FMDatabase(url: fileURL)
return database
}()
/*
* @description singleton to access a serial queue for updating the database
*/
static let sharedQueue: FMDatabaseQueue = {
let documents = try! FileManager.default
.url(for:.applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let fileURL = documents.appendingPathComponent("test.sqlite")
return FMDatabaseQueue(path: fileURL.path)!
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment