Skip to content

Commit

Permalink
Remove performSync method
Browse files Browse the repository at this point in the history
  • Loading branch information
caiozullo committed Mar 26, 2024
1 parent 26546ef commit 6030693
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,13 +38,6 @@ public final class CoreDataFeedStore {
}
}

func performSync<R>(_ action: (NSManagedObjectContext) -> Result<R, Error>) throws -> R {
let context = self.context
var result: Result<R, Error>!
context.performAndWait { result = action(context) }
return try result.get()
}

public func perform(_ action: @escaping () -> Void) {
context.perform(action)
}
Expand Down

0 comments on commit 6030693

Please sign in to comment.