Skip to content

Commit 9a2c1df

Browse files
Merge pull request #1 from priteshrnandgaonkar/imporvements
Imporvements
2 parents 688d64e + 38bc259 commit 9a2c1df

File tree

3 files changed

+139
-161
lines changed

3 files changed

+139
-161
lines changed

Example/KVStoreExample/KVStoreExample/ViewController.swift

+9-18
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ViewController: UIViewController {
1616
@IBOutlet weak var fetchTextField: UITextField!
1717
@IBOutlet weak var deleteTextField: UITextField!
1818

19-
var storeManager: KVStoreManager!
19+
var storeManager: KVStoreManager<String>!
2020

2121
override func viewDidLoad() {
2222
super.viewDidLoad()
@@ -44,6 +44,7 @@ class ViewController: UIViewController {
4444
}
4545

4646
do {
47+
4748
try storeManager.insert(value: valueUnwrapped.data(using: String.Encoding.utf8)!, for: keyUnwrapped)
4849
showAlert(withTitle: "Success", buttonTitle: "OK", message: "Successfully added key value pair in database", okAction: nil)
4950

@@ -61,28 +62,18 @@ class ViewController: UIViewController {
6162
return
6263
}
6364

64-
do {
65-
let data = try storeManager.getValue(for: keyUnwrapped)
66-
guard let fetchedString = String(data: data, encoding: .utf8) else {
67-
showAlert(withTitle: "Error", buttonTitle: "OK", message: "Failed to convert blob to string", okAction: nil)
68-
return
69-
}
70-
71-
showAlert(withTitle: "Success", buttonTitle: "OK", message: "Successfully Fetched Value. Your Key: \(keyUnwrapped), Value: \(fetchedString)", okAction: nil)
72-
73-
} catch (let error) {
74-
var errorMessage = error.localizedDescription
75-
if let sqliteError = error as? SQLiteError {
76-
errorMessage = sqliteError.description
77-
}
78-
showAlert(withTitle: "Error", buttonTitle: "OK", message: errorMessage, okAction: nil)
79-
65+
let data = storeManager[keyUnwrapped] //storeManager.getValue(for: keyUnwrapped)
66+
guard let unwrappedData = data, let fetchedString = String(data: unwrappedData, encoding: .utf8) else {
67+
showAlert(withTitle: "Error", buttonTitle: "OK", message: "Failed to get value for \(keyUnwrapped)", okAction: nil)
68+
return
8069
}
70+
71+
showAlert(withTitle: "Success", buttonTitle: "OK", message: "Successfully Fetched Value. Your Key: \(keyUnwrapped), Value: \(fetchedString)", okAction: nil)
72+
8173
}
8274

8375
@IBAction func tappedDelete(_ sender: UIButton) {
8476
let key = deleteTextField.text
85-
8677
guard let keyUnwrapped = key, keyUnwrapped.characters.count > 0 else {
8778
showAlert(withTitle: "Error", buttonTitle: "OK", message: "Key is empty. Key should have atleast one character", okAction: nil)
8879
return

KVStore/KVStoreManager.swift

+22-9
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ protocol CRUDDelegate {
1212
func insert<T: Hashable>(value: Data, for key: T) throws
1313
func deleteValue<T: Hashable>(for key: T) throws
1414
func update<T: Hashable>(value: Data, for key: T) throws
15-
func getValue<T: Hashable>(for key: T) throws -> Data
15+
func getValue<T: Hashable>(for key: T) -> Data?
1616
}
1717

18-
public class KVStoreManager: CRUDDelegate {
18+
public final class KVStoreManager<F: Hashable>: CRUDDelegate {
1919

2020
let db: SQLiteDatabase
2121

22-
/// This init method would create the db file with name passed as parameter and would open the database connection. And if the file already exist it will just open the database connection.Also it would create a table in these file if its not already created.All this operations are done synchronously in background thread.
22+
/// This init method would create the db file with name passed as parameter and would open the database connection. And if the file already exist it will just open the database connection.Also it would create a table in these file if its not already created.Sqlite is configured in serialised mode
2323
///
2424
/// - parameter fileName: The sqlite filename.
2525
///
@@ -34,7 +34,7 @@ public class KVStoreManager: CRUDDelegate {
3434
print("Successfully opened connection to database.")
3535
}
3636

37-
/// Inserts or Updates the key value pair in the database in an synchronus thread safe way in background thread
37+
/// Inserts or Updates the key value pair in the database in a syrialized way
3838
///
3939
/// - parameter value: Its of type Data. This is stored as BLOB in sqlite for the unique key.
4040
/// - parameter key: Its a unique key which is of type `Hashable` and its also a primary key in a database
@@ -46,7 +46,7 @@ public class KVStoreManager: CRUDDelegate {
4646

4747
}
4848

49-
/// Deletes the key value pair in the database in synchronus thread safe way in background thread
49+
/// Deletes the key value pair in the database in a syrialized way
5050
///
5151
/// - parameter value: Its of type Data. This is stored as BLOB in sqlite for the unique key.
5252
/// - parameter key: Its a unique key which is of type `Hashable` and its also a primary key in a database
@@ -56,7 +56,7 @@ public class KVStoreManager: CRUDDelegate {
5656
try db.delete(key: key.hashValue)
5757
}
5858

59-
/// Updates the key value pair in the database in synchronus way in background thread
59+
/// Updates the key value pair in the database in a syrialized way
6060
///
6161
/// - parameter key: Its a unique key which is of type `Hashable` and its also a primary key in a database
6262
///
@@ -65,12 +65,25 @@ public class KVStoreManager: CRUDDelegate {
6565
try db.update(model: RowModel(id: (key.hashValue) , data: value))
6666
}
6767

68-
/// Fetches the value for a key in synchronus way in background thread
68+
/// Fetches the value for a key in a syrialized way
6969
///
7070
/// - parameter key: Its a unique key which is of type `Hashable` and its also a primary key in a database
7171
///
7272
/// - returns: Data
73-
public func getValue<T: Hashable>(for key: T) throws -> Data {
74-
return try db.getValue(for: key.hashValue, tableName: RowModel.tableName)
73+
public func getValue<T: Hashable>(for key: T) -> Data? {
74+
do {
75+
let data = try db.getValue(for: key.hashValue, tableName: RowModel.tableName)
76+
return data
77+
}
78+
catch {
79+
return nil;
80+
}
81+
}
82+
83+
/// returns Data? for get query. You cannot set values through subscript
84+
public subscript(index: F) -> Data? {
85+
get {
86+
return getValue(for: index)
87+
}
7588
}
7689
}

0 commit comments

Comments
 (0)