From 0264564a6433425e4f15f5dcdd8bef20bc1c08b5 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Fri, 15 Apr 2022 13:35:55 -0700 Subject: [PATCH] move coredata model into models package --- .../CoreDataModel.xcdatamodel/contents | 0 .../Sources/Models/CoreData/StorageProvider.swift | 12 ++++++++++++ apple/OmnivoreKit/Sources/Models/FeedItem.swift | 4 ++-- .../Sources/Services/DataService/DataService.swift | 10 +--------- 4 files changed, 15 insertions(+), 11 deletions(-) rename apple/OmnivoreKit/Sources/{Services/DataService => Models}/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents (100%) create mode 100644 apple/OmnivoreKit/Sources/Models/CoreData/StorageProvider.swift diff --git a/apple/OmnivoreKit/Sources/Services/DataService/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents b/apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents similarity index 100% rename from apple/OmnivoreKit/Sources/Services/DataService/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents rename to apple/OmnivoreKit/Sources/Models/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents diff --git a/apple/OmnivoreKit/Sources/Models/CoreData/StorageProvider.swift b/apple/OmnivoreKit/Sources/Models/CoreData/StorageProvider.swift new file mode 100644 index 000000000..1f42124dd --- /dev/null +++ b/apple/OmnivoreKit/Sources/Models/CoreData/StorageProvider.swift @@ -0,0 +1,12 @@ +import CoreData +import Foundation + +/// An `NSPersistentContainer` subclass that lives in the `Models` package so that +/// the data model is looked for in the same package bundle (rather than the main bundle) +public class PersistentContainer: NSPersistentContainer { + public static func make() -> PersistentContainer { + let modelURL = Bundle.module.url(forResource: "CoreDataModel", withExtension: "momd")! + let model = NSManagedObjectModel(contentsOf: modelURL)! + return PersistentContainer(name: "DataModel", managedObjectModel: model) + } +} diff --git a/apple/OmnivoreKit/Sources/Models/FeedItem.swift b/apple/OmnivoreKit/Sources/Models/FeedItem.swift index 806b66dc3..92d886716 100644 --- a/apple/OmnivoreKit/Sources/Models/FeedItem.swift +++ b/apple/OmnivoreKit/Sources/Models/FeedItem.swift @@ -118,13 +118,13 @@ public struct FeedItem: Identifiable, Hashable { object.slug = slug object.isArchived = isArchived object.contentReader = contentReader - + for label in labels { if let managedLabel = label.toManagedObject(inContext: context) { object.labels.insert(managedLabel) } } - + return object } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 0c6f7157a..f0648b797 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -3,10 +3,6 @@ import CoreData import Foundation import Models -/// An `NSPersistentContainer` subclass that lives in the `Services` package so that -/// the data model is looked for in the same package bundle (rather than the main bundle) -final class PersistentContainer: NSPersistentContainer {} - public final class DataService: ObservableObject { public static var registerIntercomUser: ((String) -> Void)? public static var showIntercomMessenger: (() -> Void)? @@ -27,11 +23,7 @@ public final class DataService: ObservableObject { public init(appEnvironment: AppEnvironment, networker: Networker) { self.appEnvironment = appEnvironment self.networker = networker - self.persistentContainer = { - let modelURL = Bundle.module.url(forResource: "CoreDataModel", withExtension: "momd")! - let model = NSManagedObjectModel(contentsOf: modelURL)! - return PersistentContainer(name: "DataModel", managedObjectModel: model) - }() + self.persistentContainer = PersistentContainer.make() persistentContainer.loadPersistentStores { _, error in if let error = error {