Implemented the Service protocol in the Persistence library and conformed the PersistenceService service to it.
This commit is contained in:
parent
2b38e03094
commit
f79b2bd473
@ -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)
|
||||||
|
|
||||||
|
}
|
@ -26,7 +26,7 @@ public struct PersistenceService {
|
|||||||
else {
|
else {
|
||||||
fatalError("Could not load the model from the library.")
|
fatalError("Could not load the model from the library.")
|
||||||
}
|
}
|
||||||
|
|
||||||
container = NSPersistentContainer(
|
container = NSPersistentContainer(
|
||||||
name: .Model.name,
|
name: .Model.name,
|
||||||
managedObjectModel: managedObjectModel
|
managedObjectModel: managedObjectModel
|
||||||
@ -35,10 +35,18 @@ public struct PersistenceService {
|
|||||||
setContainer(inMemory)
|
setContainer(inMemory)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Functions
|
}
|
||||||
|
|
||||||
|
// MARK: - Service
|
||||||
|
|
||||||
|
extension PersistenceService: Service {
|
||||||
|
|
||||||
/// Create a private queue context.
|
// MARK: Properties
|
||||||
/// - Returns: A concurrent `NSManagedObjectContext` context instance ready to use.
|
|
||||||
|
public var viewContext: NSManagedObjectContext { container.viewContext }
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
|
||||||
public func makeTaskContext() -> NSManagedObjectContext {
|
public func makeTaskContext() -> NSManagedObjectContext {
|
||||||
let taskContext = container.newBackgroundContext()
|
let taskContext = container.newBackgroundContext()
|
||||||
|
|
||||||
@ -48,8 +56,6 @@ public struct PersistenceService {
|
|||||||
return taskContext
|
return taskContext
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a child context of the view context.
|
|
||||||
/// - Returns: A generated child `NSManagedObjectContext` context instance ready to use.
|
|
||||||
public func makeChildContext() -> NSManagedObjectContext {
|
public func makeChildContext() -> NSManagedObjectContext {
|
||||||
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
|
||||||
|
|
||||||
@ -60,8 +66,6 @@ public struct PersistenceService {
|
|||||||
return context
|
return context
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Save a given context,
|
|
||||||
/// - Parameter context: A `NSManagedObjectContext` context instance to save.
|
|
||||||
public func save(context: NSManagedObjectContext) {
|
public func save(context: NSManagedObjectContext) {
|
||||||
guard context.hasChanges else {
|
guard context.hasChanges else {
|
||||||
return
|
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) {
|
public func save(childContext context: NSManagedObjectContext) {
|
||||||
guard context.hasChanges else {
|
guard context.hasChanges else {
|
||||||
return
|
return
|
||||||
@ -100,7 +102,7 @@ public struct PersistenceService {
|
|||||||
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
|
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Helpers
|
// MARK: - Helpers
|
||||||
|
@ -13,7 +13,7 @@ import Remote
|
|||||||
// MARK: - DependencyService+Keys
|
// MARK: - DependencyService+Keys
|
||||||
|
|
||||||
extension DependencyService {
|
extension DependencyService {
|
||||||
var persistence: PersistenceService {
|
var persistence: Persistence.Service {
|
||||||
get { Self[PersistenceKey.self] }
|
get { Self[PersistenceKey.self] }
|
||||||
set { Self[PersistenceKey.self] = newValue }
|
set { Self[PersistenceKey.self] = newValue }
|
||||||
}
|
}
|
||||||
@ -27,7 +27,7 @@ extension DependencyService {
|
|||||||
// MARK: - Dependency keys
|
// MARK: - Dependency keys
|
||||||
|
|
||||||
struct PersistenceKey: DependencyKey {
|
struct PersistenceKey: DependencyKey {
|
||||||
static var currentValue: PersistenceService = .shared
|
static var currentValue: Persistence.Service = PersistenceService.shared
|
||||||
}
|
}
|
||||||
|
|
||||||
struct RemoteKey: DependencyKey {
|
struct RemoteKey: DependencyKey {
|
||||||
|
@ -21,7 +21,7 @@ class LocationsListViewModel: ObservableObject {
|
|||||||
|
|
||||||
weak var coordinator: LocationsListCoordination?
|
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
|
@Published private var viewStatus: LocationsListViewStatus = .initialised
|
||||||
|
|
||||||
|
@ -15,13 +15,13 @@ struct LoadRemoteLocationsUseCase {
|
|||||||
|
|
||||||
// MARK: Properties
|
// MARK: Properties
|
||||||
|
|
||||||
private let persistence: PersistenceService
|
private let persistence: Persistence.Service
|
||||||
private let remoteService: Remote.Service
|
private let remoteService: Remote.Service
|
||||||
|
|
||||||
// MARK: Initialisers
|
// MARK: Initialisers
|
||||||
|
|
||||||
init(
|
init(
|
||||||
persistence: PersistenceService,
|
persistence: Persistence.Service,
|
||||||
remoteService: Remote.Service
|
remoteService: Remote.Service
|
||||||
) {
|
) {
|
||||||
self.persistence = persistence
|
self.persistence = persistence
|
||||||
|
@ -13,11 +13,11 @@ struct SaveLocalLocationUseCase {
|
|||||||
|
|
||||||
// MARK: Properties
|
// MARK: Properties
|
||||||
|
|
||||||
private let persistence: PersistenceService
|
private let persistence: Persistence.Service
|
||||||
|
|
||||||
// MARK: Initialisers
|
// MARK: Initialisers
|
||||||
|
|
||||||
init(persistence: PersistenceService) {
|
init(persistence: Persistence.Service) {
|
||||||
self.persistence = persistence
|
self.persistence = persistence
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user