Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed AsyncStorage exists not throwing errors #325

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ let score = try? storage.object(forKey: "score")
let favoriteCharacter = try? storage.object(forKey: "my favorite city")

// Check if an object exists
let hasFavoriteCharacter = try? storage.objectExists(forKey: "my favorite city")
let hasFavoriteCharacter = storage.objectExists(forKey: "my favorite city")

// Remove an object in storage
try? storage.removeObject(forKey: "my favorite city")
Expand Down Expand Up @@ -263,8 +263,8 @@ storage.async.object(forKey: "my favorite city") { result in
}
}

storage.async.objectExists(forKey: "my favorite city") { result in
if case .success(let exists) = result, exists {
storage.async.objectExists(forKey: "my favorite city") { exists in
if exists {
print("I have a favorite city")
}
}
Expand Down Expand Up @@ -305,12 +305,10 @@ do {
print(error)
}

do {
let exists = try await storage.async.objectExists(forKey: "my favorite city")
if exists {
print("I have a favorite city")
}
} catch {}
let exists = await storage.async.objectExists(forKey: "my favorite city")
if exists {
print("I have a favorite city")
}

do {
try await storage.async.remoeAll()
Expand Down
28 changes: 17 additions & 11 deletions Source/Shared/Storage/AsyncStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,27 @@ extension AsyncStorage {
@available(*, deprecated, renamed: "objectExists(forKey:completion:)")
public func existsObject(
forKey key: Key,
completion: @escaping (Result<Bool, Error>) -> Void) {
completion: @escaping (Bool) -> Void) {
object(forKey: key, completion: { (result: Result<Value, Error>) in
completion(result.map({ _ in
return true
}))
switch result {
case .success:
completion(true)
case .failure:
completion(false)
}
})
}

public func objectExists(
forKey key: Key,
completion: @escaping (Result<Bool, Error>) -> Void) {
completion: @escaping (Bool) -> Void) {
object(forKey: key, completion: { (result: Result<Value, Error>) in
completion(result.map({ _ in
return true
}))
switch result {
case .success:
completion(true)
case .failure:
completion(false)
}
})
}
}
Expand Down Expand Up @@ -192,10 +198,10 @@ public extension AsyncStorage {
}
}

func objectExists(forKey key: Key) async throws -> Bool {
try await withCheckedThrowingContinuation { continuation in
func objectExists(forKey key: Key) async -> Bool {
await withCheckedContinuation { continuation in
objectExists(forKey: key) {
continuation.resume(with: $0)
continuation.resume(returning: $0)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Shared/Storage/StorageAware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public protocol StorageAware {
- Parameter key: Unique key to identify the object.
*/
@available(*, deprecated, renamed: "objectExists(forKey:)")
func existsObject(forKey key: Key) throws -> Bool
func existsObject(forKey key: Key) -> Bool

/**
Check if an object exist by the given key.
Expand Down Expand Up @@ -83,7 +83,7 @@ public extension StorageAware {
return try entry(forKey: key).object
}

func existsObject(forKey key: Key) throws -> Bool {
func existsObject(forKey key: Key) -> Bool {
do {
let _: Value = try object(forKey: key)
return true
Expand Down
13 changes: 5 additions & 8 deletions Tests/iOS/Tests/Storage/AsyncStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ final class AsyncStorageTests: XCTestCase {
}

then("all are removed") {
intStorage.objectExists(forKey: "key-99", completion: { result in
switch result {
case .success:
intStorage.objectExists(forKey: "key-99", completion: { exists in
if exists {
XCTFail()
default:
} else {
expectation.fulfill()
}
})
Expand All @@ -85,10 +84,8 @@ final class AsyncStorageTests: XCTestCase {
}

await then("all are removed") {
do {
_ = try await intStorage.objectExists(forKey: "key-99")
XCTFail()
} catch {}
let exists = await intStorage.objectExists(forKey: "key-99")
XCTAssertFalse(exists)
}
}
}