frenzy-ios/Reader/CoreData/ManagedObjectExtensions.swift

39 lines
1.2 KiB
Swift

//
// ManagedObjectExtensions.swift
// Reader
//
// Created by Shadowfacts on 12/24/21.
//
import Foundation
import Fervor
extension Group {
func updateFromServer(_ serverGroup: Fervor.Group) {
guard self.id == nil || self.id == serverGroup.id else { return }
self.id = serverGroup.id
self.title = serverGroup.title
// feeds relationships will be updated after feeds are created in PersistentContainer.sync
}
}
extension Feed {
func updateFromServer(_ serverFeed: Fervor.Feed) {
guard self.id == nil || self.id == serverFeed.id else { return }
self.id = serverFeed.id
self.title = serverFeed.title
self.url = serverFeed.url
self.lastUpdated = serverFeed.lastUpdated
// todo: check this
self.removeFromGroups(self.groups!.filtered(using: NSPredicate(format: "NOT id IN %@", serverFeed.groupIDs)) as NSSet)
let groupsToAddReq = Group.fetchRequest()
groupsToAddReq.predicate = NSPredicate(format: "id IN %@", serverFeed.groupIDs.filter { g in !self.groups!.contains { ($0 as! Group).id == g } })
let groupsToAdd = try! self.managedObjectContext!.fetch(groupsToAddReq)
self.addToGroups(NSSet(array: groupsToAdd))
}
}