diff --git a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift index ed61a4341..0460a1578 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Home/HomeFeedViewModel.swift @@ -43,7 +43,7 @@ import Views // Check if user has scrolled to the last five items in the list if let itemIndex = itemIndex, itemIndex > thresholdIndex, items.count < thresholdIndex + 10 { - Task { await loadItems(dataService: dataService, isRefresh: false) } + loadItems(dataService: dataService, isRefresh: false) } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents b/apple/OmnivoreKit/Sources/Services/DataService/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents new file mode 100644 index 000000000..12986cc36 --- /dev/null +++ b/apple/OmnivoreKit/Sources/Services/DataService/CoreData/CoreDataModel.xcdatamodeld/CoreDataModel.xcdatamodel/contents @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift index 12e72e406..74ee001a8 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/DataService.swift @@ -1,15 +1,9 @@ import Combine +import CoreData import Foundation import Models -public class CacheManager: NSObject, NSCacheDelegate { - public func cache(_: NSCache, willEvictObject obj: Any) { - // This is just used for debugging - if let content = obj as? CachedPageContent { - print("evicting page from cache", content.slug) - } - } -} +final class PersistentContainer: NSPersistentContainer {} public final class DataService: ObservableObject { public static var registerIntercomUser: ((String) -> Void)? @@ -25,14 +19,23 @@ public final class DataService: ObservableObject { let highlightsCache = NSCache() let highlightsCacheQueue = DispatchQueue(label: "app.omnivore.highlights.cache.queue", attributes: .concurrent) - let cacheManager: CacheManager + let persistentContainer: PersistentContainer var subscriptions = Set() public init(appEnvironment: AppEnvironment, networker: Networker) { self.appEnvironment = appEnvironment self.networker = networker - self.cacheManager = CacheManager() - pageCache.delegate = cacheManager + self.persistentContainer = { + let modelURL = Bundle.module.url(forResource: "CoreDataModel", withExtension: "momd")! + let model = NSManagedObjectModel(contentsOf: modelURL)! + return PersistentContainer(name: "DataModel", managedObjectModel: model) + }() + + persistentContainer.loadPersistentStores { _, error in + if let error = error { + fatalError("Core Data store failed to load with error: \(error)") + } + } } public func clearHighlights() { @@ -51,7 +54,6 @@ public final class DataService: ObservableObject { public extension DataService { func prefetchPages(items: [FeedItem]) { - print("prefetching pages") guard let viewer = currentViewer else { return } for item in items { @@ -67,13 +69,7 @@ public extension DataService { } func pageFromCache(slug: String) -> ArticleContent? { - if let content = pageCache.object(forKey: NSString(string: slug)) { - print("cache hit", slug) - return content.value - } else { - print("cache miss", slug) - } - return nil + pageCache.object(forKey: NSString(string: slug))?.value } func invalidateCachedPage(slug: String?) {