From 61c7403d24ccdebffabf5a0fbeaa254d9b3de3a0 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Sun, 24 Apr 2022 17:10:07 -0700 Subject: [PATCH] update highlights on linkedItem when persisting --- .../DataService/Queries/ArticleContentQuery.swift | 3 ++- .../Services/InternalModels/InternalHighlight.swift | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift index 458f3be42..69ecf3686 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Queries/ArticleContentQuery.swift @@ -85,9 +85,10 @@ extension DataService { let linkedItem = try? self.backgroundContext.fetch(fetchRequest).first if let linkedItem = linkedItem, let linkedItemID = linkedItem.id { - _ = highlights.map { + let highlightObjects = highlights.map { $0.asManagedObject(context: self.backgroundContext, associatedItemID: linkedItemID) } + linkedItem.addToHighlights(NSSet(array: highlightObjects)) linkedItem.htmlContent = htmlContent } diff --git a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift index 7cb9594bb..16994232b 100644 --- a/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift +++ b/apple/OmnivoreKit/Sources/Services/InternalModels/InternalHighlight.swift @@ -15,7 +15,13 @@ struct InternalHighlight: Encodable { let createdByMe: Bool func asManagedObject(context: NSManagedObjectContext, associatedItemID: String) -> Highlight { - let highlight = Highlight(entity: Highlight.entity(), insertInto: context) + let fetchRequest: NSFetchRequest = Highlight.fetchRequest() + fetchRequest.predicate = NSPredicate( + format: "id == %@", id + ) + let existingHighlight = (try? context.fetch(fetchRequest))?.first + let highlight = existingHighlight ?? Highlight(entity: Highlight.entity(), insertInto: context) + highlight.linkedItemId = associatedItemID highlight.markedForDeletion = false highlight.id = id