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

Allow to clear the cache synchronously #1186

Merged
merged 2 commits into from
May 5, 2020
Merged
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
10 changes: 7 additions & 3 deletions Sources/Apollo/InMemoryNormalizedCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public final class InMemoryNormalizedCache: NormalizedCache {

public func clear(callbackQueue: DispatchQueue?,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should make handler non optional here, otherwise its easy to miss that its asynchronous operation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I can still see a case for people wanting it to fire off in a non-blocking way but not wanting to pass in a handler, I don't think it's worth it.

completion: ((Result<Void, Error>) -> Void)?) {
self.recordsLock.lock()
self.records.clear()
self.recordsLock.unlock()
clearImmediately()

guard let completion = completion else {
return
Expand All @@ -44,4 +42,10 @@ public final class InMemoryNormalizedCache: NormalizedCache {
action: completion,
result: .success(()))
}

public func clearImmediately() {
self.recordsLock.lock()
self.records.clear()
self.recordsLock.unlock()
}
}
3 changes: 3 additions & 0 deletions Sources/Apollo/NormalizedCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ public protocol NormalizedCache {
/// - completion: [optional] A completion closure to fire when the clear function has completed.
func clear(callbackQueue: DispatchQueue?,
completion: ((Result<Void, Error>) -> Void)?)

// Clears all records synchronously
func clearImmediately() throws
}
6 changes: 5 additions & 1 deletion Sources/ApolloSQLite/SQLiteNormalizedCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ extension SQLiteNormalizedCache: NormalizedCache {
public func clear(callbackQueue: DispatchQueue?, completion: ((Swift.Result<Void, Error>) -> Void)?) {
let result: Swift.Result<Void, Error>
do {
try self.clearRecords()
try clearImmediately()
result = .success(())
} catch {
result = .failure(error)
Expand All @@ -158,4 +158,8 @@ extension SQLiteNormalizedCache: NormalizedCache {
action: completion,
result: result)
}

public func clearImmediately() throws {
try clearRecords()
}
}
4 changes: 4 additions & 0 deletions Tests/ApolloTests/BatchedLoadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ private final class MockBatchedNormalizedCache: NormalizedCache {
result: .success(()))
}
}

func clearImmediately() {
records.clear()
}
}

class BatchedLoadTests: XCTestCase {
Expand Down