From f79b2bd473ea0817c7eb3b0ecd7962498290f7e9 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Thu, 13 Apr 2023 14:12:27 +0200 Subject: [PATCH] Implemented the Service protocol in the Persistence library and conformed the PersistenceService service to it. --- .../Persistence/Protocols/Service.swift | 36 +++++++++++++++++++ .../Services/PersistenceService.swift | 24 +++++++------ .../Extensions/DependencyService+Keys.swift | 4 +-- .../LocationsListViewModel.swift | 2 +- .../LoadRemoteLocationsUseCase.swift | 4 +-- .../Use Cases/SaveLocalLocationUseCase.swift | 4 +-- 6 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 Apps/Locations/Libraries/Sources/Persistence/Protocols/Service.swift diff --git a/Apps/Locations/Libraries/Sources/Persistence/Protocols/Service.swift b/Apps/Locations/Libraries/Sources/Persistence/Protocols/Service.swift new file mode 100644 index 0000000..c37618d --- /dev/null +++ b/Apps/Locations/Libraries/Sources/Persistence/Protocols/Service.swift @@ -0,0 +1,36 @@ +// +// Service.swift +// Persistence +// +// Created by Javier Cicchelli on 13/04/2023. +// Copyright © 2023 Röck+Cöde. All rights reserved. +// + +import CoreData + +public protocol Service { + + // MARK: Properties + + /// The main managed object context. + var viewContext: NSManagedObjectContext { get } + + // MARK: Functions + + /// Create a private queue context. + /// - Returns: A concurrent `NSManagedObjectContext` context instance ready to use. + func makeTaskContext() -> NSManagedObjectContext + + /// Create a child context of the view context. + /// - Returns: A generated child `NSManagedObjectContext` context instance ready to use. + func makeChildContext() -> NSManagedObjectContext + + /// Save a given context, + /// - Parameter context: A `NSManagedObjectContext` context instance to save. + func save(context: NSManagedObjectContext) + + /// Save a given child context as well as its respective parent context. + /// - Parameter context: A child `NSManagedObjectContext` context instance to save. + func save(childContext context: NSManagedObjectContext) + +} diff --git a/Apps/Locations/Libraries/Sources/Persistence/Services/PersistenceService.swift b/Apps/Locations/Libraries/Sources/Persistence/Services/PersistenceService.swift index c81ab12..1fe8550 100644 --- a/Apps/Locations/Libraries/Sources/Persistence/Services/PersistenceService.swift +++ b/Apps/Locations/Libraries/Sources/Persistence/Services/PersistenceService.swift @@ -26,7 +26,7 @@ public struct PersistenceService { else { fatalError("Could not load the model from the library.") } - + container = NSPersistentContainer( name: .Model.name, managedObjectModel: managedObjectModel @@ -35,10 +35,18 @@ public struct PersistenceService { setContainer(inMemory) } - // MARK: Functions +} + +// MARK: - Service + +extension PersistenceService: Service { - /// Create a private queue context. - /// - Returns: A concurrent `NSManagedObjectContext` context instance ready to use. + // MARK: Properties + + public var viewContext: NSManagedObjectContext { container.viewContext } + + // MARK: Functions + public func makeTaskContext() -> NSManagedObjectContext { let taskContext = container.newBackgroundContext() @@ -48,8 +56,6 @@ public struct PersistenceService { return taskContext } - /// Create a child context of the view context. - /// - Returns: A generated child `NSManagedObjectContext` context instance ready to use. public func makeChildContext() -> NSManagedObjectContext { let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) @@ -60,8 +66,6 @@ public struct PersistenceService { return context } - /// Save a given context, - /// - Parameter context: A `NSManagedObjectContext` context instance to save. public func save(context: NSManagedObjectContext) { guard context.hasChanges else { return @@ -75,8 +79,6 @@ public struct PersistenceService { } } - /// Save a given child context as well as its respective parent context. - /// - Parameter context: A child `NSManagedObjectContext` context instance to save. public func save(childContext context: NSManagedObjectContext) { guard context.hasChanges else { return @@ -100,7 +102,7 @@ public struct PersistenceService { fatalError("Unresolved error \(nserror), \(nserror.userInfo)") } } - + } // MARK: - Helpers diff --git a/Apps/Locations/Sources/Extensions/DependencyService+Keys.swift b/Apps/Locations/Sources/Extensions/DependencyService+Keys.swift index aed48af..1826369 100644 --- a/Apps/Locations/Sources/Extensions/DependencyService+Keys.swift +++ b/Apps/Locations/Sources/Extensions/DependencyService+Keys.swift @@ -13,7 +13,7 @@ import Remote // MARK: - DependencyService+Keys extension DependencyService { - var persistence: PersistenceService { + var persistence: Persistence.Service { get { Self[PersistenceKey.self] } set { Self[PersistenceKey.self] = newValue } } @@ -27,7 +27,7 @@ extension DependencyService { // MARK: - Dependency keys struct PersistenceKey: DependencyKey { - static var currentValue: PersistenceService = .shared + static var currentValue: Persistence.Service = PersistenceService.shared } struct RemoteKey: DependencyKey { diff --git a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift index dedcbdc..90f1dd4 100644 --- a/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift +++ b/Apps/Locations/Sources/Screens/LocationsList/LocationsListViewModel.swift @@ -21,7 +21,7 @@ class LocationsListViewModel: ObservableObject { weak var coordinator: LocationsListCoordination? - private lazy var locationProvider = LocationProvider(managedContext: persistence.container.viewContext) + private lazy var locationProvider = LocationProvider(managedContext: persistence.viewContext) @Published private var viewStatus: LocationsListViewStatus = .initialised diff --git a/Apps/Locations/Sources/Use Cases/LoadRemoteLocationsUseCase.swift b/Apps/Locations/Sources/Use Cases/LoadRemoteLocationsUseCase.swift index fd436c3..fa6d45c 100644 --- a/Apps/Locations/Sources/Use Cases/LoadRemoteLocationsUseCase.swift +++ b/Apps/Locations/Sources/Use Cases/LoadRemoteLocationsUseCase.swift @@ -15,13 +15,13 @@ struct LoadRemoteLocationsUseCase { // MARK: Properties - private let persistence: PersistenceService + private let persistence: Persistence.Service private let remoteService: Remote.Service // MARK: Initialisers init( - persistence: PersistenceService, + persistence: Persistence.Service, remoteService: Remote.Service ) { self.persistence = persistence diff --git a/Apps/Locations/Sources/Use Cases/SaveLocalLocationUseCase.swift b/Apps/Locations/Sources/Use Cases/SaveLocalLocationUseCase.swift index 0d2d074..2d078d5 100644 --- a/Apps/Locations/Sources/Use Cases/SaveLocalLocationUseCase.swift +++ b/Apps/Locations/Sources/Use Cases/SaveLocalLocationUseCase.swift @@ -13,11 +13,11 @@ struct SaveLocalLocationUseCase { // MARK: Properties - private let persistence: PersistenceService + private let persistence: Persistence.Service // MARK: Initialisers - init(persistence: PersistenceService) { + init(persistence: Persistence.Service) { self.persistence = persistence }