Skip to content

Commit

Permalink
Replace occurrences of Error? for representing operation success/fail…
Browse files Browse the repository at this point in the history
…ure with `Result<Void, Error>`
  • Loading branch information
TrabelsiAchraf committed Nov 25, 2021
1 parent ef0a65a commit 72fb51d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 32 deletions.
4 changes: 2 additions & 2 deletions EssentialFeed/Feed Cache/FeedStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import Foundation
public typealias CachedFeed = (feed: [LocalFeedImage], timestamp: Date)

public protocol FeedStore {
typealias DeletionResult = Error?
typealias DeletionResult = Swift.Result<Void, Error>
typealias DeletionCompletion = (DeletionResult) -> Void

typealias InsertionResult = Error?
typealias InsertionResult = Swift.Result<Void, Error>
typealias InsertionCompletion = (InsertionResult) -> Void

typealias RetrievalResult = Result<CachedFeed?, Error>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public final class CodableFeedStore: FeedStore {
let cache = Cache(feed: feed.map(CodableFeedImage.init), timestamp: timestamp)
let encoded = try encoder.encode(cache)
try encoded.write(to: storeURL)
completion(nil)
completion(.success(()))
} catch {
completion(error)
completion(.failure(error))
}
}
}
Expand All @@ -79,14 +79,14 @@ public final class CodableFeedStore: FeedStore {
let storeURL = self.storeURL
queue.async(flags: .barrier) {
guard FileManager.default.fileExists(atPath: storeURL.path) else {
return completion(nil)
return completion(.success(()))
}

do {
try FileManager.default.removeItem(at: storeURL)
completion(nil)
completion(.success(()))
} catch {
completion(error)
completion(.failure(error))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public final class CoreDataFeedStore: FeedStore {
managedCache.feed = ManagedFeedImage.images(from: feed, in: context)

try context.save()
completion(nil)
completion(.success(()))
} catch {
completion(error)
completion(.failure(error))
}
}
}
Expand All @@ -49,9 +49,9 @@ public final class CoreDataFeedStore: FeedStore {
perform { context in
do {
try ManagedCache.find(in: context).map(context.delete).map(context.save)
completion(nil)
completion(.success(()))
} catch {
completion(error)
completion(.failure(error))
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions EssentialFeed/Feed Cache/LocalFeedLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,27 @@ public final class LocalFeedLoader {
}

extension LocalFeedLoader {
public typealias SaveResult = Error?
public typealias SaveResult = Result<Void, Error>

public func save(_ feed: [FeedImage], completion: @escaping (SaveResult) -> Void) {
store.deleteCachedFeed { [weak self] error in
store.deleteCachedFeed { [weak self] deletionResult in
guard let self = self else { return }

if let cachedDeletionError = error {
completion(cachedDeletionError)
} else {

switch deletionResult {
case .success:
self.cache(feed, with: completion)

case let .failure(error):
completion(.failure(error))
}
}
}

private func cache(_ feed: [FeedImage], with completion: @escaping (SaveResult) -> Void) {
store.insert(feed.toLocal(), timestamp: self.currentDate()) { [weak self] error in
store.insert(feed.toLocal(), timestamp: currentDate()) { [weak self] insertionResult in
guard self != nil else { return }
completion(error)

completion(insertionResult)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class EssentialFeedAPIEndToEndTests: XCTestCase {
exp.fulfill()
}

wait(for: [exp], timeout: 15.0)
wait(for: [exp], timeout: 30.0)

return receivedResult
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ class EssentialFeedCacheIntegrationTests: XCTestCase {

private func save(_ feed: [FeedImage], with loader: LocalFeedLoader, file: StaticString = #file, line: UInt = #line) {
let saveExp = expectation(description: "Wait for save completion")
loader.save(feed) { saveError in
XCTAssertNil(saveError, "Expected to save feed successfully", file: file, line: line)
loader.save(feed) { result in
if case let Result.failure(error) = result {
XCTAssertNil(error, "Expected to save feed successfully", file: file, line: line)
}
saveExp.fulfill()
}
wait(for: [saveExp], timeout: 1.0)
Expand Down
4 changes: 2 additions & 2 deletions EssentialFeedTests/Feed Cache/CacheFeedUseCaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ class CacheFeedUseCaseTests: XCTestCase {
let exp = expectation(description: "Wait for save completion")

var receivedError: Error?
sut.save(uniqueImageFeed().models) { error in
receivedError = error
sut.save(uniqueImageFeed().models) { result in
if case let Result.failure(error) = result { receivedError = error }
exp.fulfill()
}

Expand Down
8 changes: 4 additions & 4 deletions EssentialFeedTests/Feed Cache/Helpers/FeedStoreSpy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class FeedStoreSpy: FeedStore {
}

func completeDeletion(with error: Error, at index: Int = 0) {
deletionCompletions[index](error)
deletionCompletions[index](.failure(error))
}

func completeDeletionSuccessfully(at index: Int = 0) {
deletionCompletions[index](nil)
deletionCompletions[index](.success(()))
}

func insert(_ feed: [LocalFeedImage], timestamp: Date, completion: @escaping InsertionCompletion) {
Expand All @@ -41,11 +41,11 @@ class FeedStoreSpy: FeedStore {
}

func completeInsertion(with error: Error, at index: Int = 0) {
insertionCompletions[index](error)
insertionCompletions[index](.failure(error))
}

func completeInsertionSuccessfully(at index: Int = 0) {
insertionCompletions[index](nil)
insertionCompletions[index](.success(()))
}

func retrieve(completion: @escaping RetrievalCompletion) {
Expand Down
8 changes: 4 additions & 4 deletions EssentialFeedTests/Feed Cache/XCTestCase+FeedStoreSpecs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ extension FeedStoreSpecs where Self: XCTestCase {
func insert(_ cache: (feed: [LocalFeedImage], timestamp: Date), to sut: FeedStore) -> Error? {
let exp = expectation(description: "Wait for cache insertion")
var insertionError: Error?
sut.insert(cache.feed, timestamp: cache.timestamp) { receivedInsertionError in
insertionError = receivedInsertionError
sut.insert(cache.feed, timestamp: cache.timestamp) { result in
if case let Result.failure(error) = result { insertionError = error }
exp.fulfill()
}
wait(for: [exp], timeout: 1.0)
Expand All @@ -132,8 +132,8 @@ extension FeedStoreSpecs where Self: XCTestCase {
func deleteCache(from sut: FeedStore) -> Error? {
let exp = expectation(description: "Wait for cache deletion")
var deletionError: Error?
sut.deleteCachedFeed { receivedDeletionError in
deletionError = receivedDeletionError
sut.deleteCachedFeed { result in
if case let Result.failure(error) = result { deletionError = error }
exp.fulfill()
}
wait(for: [exp], timeout: 10.0)
Expand Down

0 comments on commit 72fb51d

Please sign in to comment.