Created
March 18, 2019 18:51
-
-
Save dkw5877/25ef1939c2a6b319c607c9fee5894fc3 to your computer and use it in GitHub Desktop.
Example of singleton for SQLite using FMDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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