mirror of
https://github.com/Mueller-Patrick/DHBW-Service-App.git
synced 2024-12-22 13:25:13 +00:00
⚡ Now deleting invalid local RaPla events
This commit is contained in:
parent
7945e62d40
commit
ea0b759007
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user