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