Skip to content

Commit eeaf771

Browse files
authored
Merge pull request #307 from joeljfischer/bugfix/issue-306-existsObject-changes
Add objectExists to replace existsObject
2 parents b621482 + 33a693c commit eeaf771

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

Source/Shared/Storage/AsyncStorage.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ extension AsyncStorage {
106106
})
107107
}
108108

109+
@available(*, deprecated, renamed: "objectExists(forKey:completion:)")
109110
public func existsObject(
110111
forKey key: Key,
111112
completion: @escaping (Result<Bool, Error>) -> Void) {
@@ -115,6 +116,16 @@ extension AsyncStorage {
115116
}))
116117
})
117118
}
119+
120+
public func objectExists(
121+
forKey key: Key,
122+
completion: @escaping (Result<Bool, Error>) -> Void) {
123+
object(forKey: key, completion: { (result: Result<Value, Error>) in
124+
completion(result.map({ _ in
125+
return true
126+
}))
127+
})
128+
}
118129
}
119130

120131
public extension AsyncStorage {

Source/Shared/Storage/StorageAware.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,15 @@ public protocol StorageAware {
4747
Check if an object exist by the given key.
4848
- Parameter key: Unique key to identify the object.
4949
*/
50+
@available(*, deprecated, renamed: "objectExists(forKey:)")
5051
func existsObject(forKey key: Key) throws -> Bool
5152

53+
/**
54+
Check if an object exist by the given key.
55+
- Parameter key: Unique key to identify the object.
56+
*/
57+
func objectExists(forKey key: Key) -> Bool
58+
5259
/**
5360
Removes all objects from the cache storage.
5461
*/
@@ -80,6 +87,15 @@ public extension StorageAware {
8087
}
8188
}
8289

90+
func objectExists(forKey key: Key) -> Bool {
91+
do {
92+
let _: Value = try object(forKey: key)
93+
return true
94+
} catch {
95+
return false
96+
}
97+
}
98+
8399
func isExpiredObject(forKey key: Key) throws -> Bool {
84100
do {
85101
let entry = try self.entry(forKey: key)

Tests/iOS/Tests/Storage/AsyncStorageTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ final class AsyncStorageTests: XCTestCase {
5050
}
5151

5252
then("all are removed") {
53-
intStorage.existsObject(forKey: "key-99", completion: { result in
53+
intStorage.objectExists(forKey: "key-99", completion: { result in
5454
switch result {
5555
case .success:
5656
XCTFail()

Tests/iOS/Tests/Storage/SyncStorageTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ final class SyncStorageTests: XCTestCase {
4040
try intStorage.removeAll()
4141
}
4242

43-
try then("all are removed") {
44-
XCTAssertFalse(try intStorage.existsObject(forKey: "key-99"))
43+
then("all are removed") {
44+
XCTAssertFalse(intStorage.objectExists(forKey: "key-99"))
4545
}
4646
}
4747
}

0 commit comments

Comments
 (0)