From 6030693d5f559e77befd5942eeffe21b63414e0f Mon Sep 17 00:00:00 2001 From: Caio Zullo Date: Fri, 22 Mar 2024 15:48:49 +0200 Subject: [PATCH] Remove performSync method --- ...CoreDataFeedStore+FeedImageDataStore.swift | 16 +++--------- .../CoreDataFeedStore+FeedStore.swift | 26 +++++-------------- .../CoreData/CoreDataFeedStore.swift | 9 +------ 3 files changed, 12 insertions(+), 39 deletions(-) diff --git a/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore+FeedImageDataStore.swift b/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore+FeedImageDataStore.swift index d95be9f8..9e687eca 100644 --- a/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore+FeedImageDataStore.swift +++ b/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore+FeedImageDataStore.swift @@ -7,21 +7,13 @@ import Foundation extension CoreDataFeedStore: FeedImageDataStore { public func insert(_ data: Data, for url: URL) throws { - try performSync { context in - Result { - try ManagedFeedImage.first(with: url, in: context) - .map { $0.data = data } - .map(context.save) - } - } + try ManagedFeedImage.first(with: url, in: context) + .map { $0.data = data } + .map(context.save) } public func retrieve(dataForURL url: URL) throws -> Data? { - try performSync { context in - Result { - try ManagedFeedImage.data(with: url, in: context) - } - } + try ManagedFeedImage.data(with: url, in: context) } } diff --git a/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore+FeedStore.swift b/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore+FeedStore.swift index 88b272c7..46db2f89 100644 --- a/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore+FeedStore.swift +++ b/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore+FeedStore.swift @@ -7,32 +7,20 @@ import CoreData extension CoreDataFeedStore: FeedStore { public func retrieve() throws -> CachedFeed? { - try performSync { context in - Result { - try ManagedCache.find(in: context).map { - CachedFeed(feed: $0.localFeed, timestamp: $0.timestamp) - } - } + try ManagedCache.find(in: context).map { + CachedFeed(feed: $0.localFeed, timestamp: $0.timestamp) } } public func insert(_ feed: [LocalFeedImage], timestamp: Date) throws { - try performSync { context in - Result { - let managedCache = try ManagedCache.newUniqueInstance(in: context) - managedCache.timestamp = timestamp - managedCache.feed = ManagedFeedImage.images(from: feed, in: context) - try context.save() - } - } + let managedCache = try ManagedCache.newUniqueInstance(in: context) + managedCache.timestamp = timestamp + managedCache.feed = ManagedFeedImage.images(from: feed, in: context) + try context.save() } public func deleteCachedFeed() throws { - try performSync { context in - Result { - try ManagedCache.deleteCache(in: context) - } - } + try ManagedCache.deleteCache(in: context) } } diff --git a/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore.swift b/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore.swift index 52650607..8b935c4b 100644 --- a/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore.swift +++ b/EssentialFeed/EssentialFeed/Feed Cache/Infrastructure/CoreData/CoreDataFeedStore.swift @@ -9,7 +9,7 @@ public final class CoreDataFeedStore { private static let model = NSManagedObjectModel.with(name: modelName, in: Bundle(for: CoreDataFeedStore.self)) private let container: NSPersistentContainer - private let context: NSManagedObjectContext + let context: NSManagedObjectContext enum StoreError: Error { case modelNotFound @@ -38,13 +38,6 @@ public final class CoreDataFeedStore { } } - func performSync(_ action: (NSManagedObjectContext) -> Result) throws -> R { - let context = self.context - var result: Result! - context.performAndWait { result = action(context) } - return try result.get() - } - public func perform(_ action: @escaping () -> Void) { context.perform(action) }