From ea0b759007c06d2eb580997f5706d278ca0b8e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Mu=CC=88ller?= Date: Sun, 31 Jan 2021 23:10:06 +0100 Subject: [PATCH] :zap: Now deleting invalid local RaPla events --- DHBW-Service/Utility/RaPlaFetcher.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/DHBW-Service/Utility/RaPlaFetcher.swift b/DHBW-Service/Utility/RaPlaFetcher.swift index 8536f7f..e8ab3c5 100644 --- a/DHBW-Service/Utility/RaPlaFetcher.swift +++ b/DHBW-Service/Utility/RaPlaFetcher.swift @@ -117,13 +117,14 @@ class RaPlaFetcher { } // Save the given iCalEvent objects to CoreData - // Updates the events if they already exist + // Updates the events if they already exist and deletes old (/invalid) ones private class func saveToCoreData(eventObjects: [iCalEvent]) -> Bool{ let existingEvents = UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: []) var existingEventsDict: [String:NSManagedObject] = [:] for event in existingEvents { existingEventsDict[event.value(forKey: "uid") as! String] = event } + let newEventUIDs = eventObjects.map{$0.uid} for event in eventObjects { // If the event already exists locally, update it. Otherwise, create a new record @@ -143,6 +144,16 @@ class RaPlaFetcher { evt.setValue(event.uid, forKey: "uid") } + // Now we also have to delete locally stored events that have been deleted from RaPla + for localUid in existingEventsDict.keys { + if(!newEventUIDs.contains(localUid)){ + // Locally stored event does not exist in RaPla anymore, delete it + let evt = existingEventsDict[localUid] + PersistenceController.shared.context.delete(evt!) + print("Deleted " + localUid) + } + } + PersistenceController.shared.save() return true