DHBW-Service-App/DHBW-Service/Utility/UtilityFunctions.swift

43 lines
1.2 KiB
Swift

//
// UtilityFunctions.swift
// DHBW-Service
//
// Created by Patrick Müller on 22.12.20.
//
import Foundation
import CoreData
class UtilityFunctions {
public class func getCoreDataObject(entity: String) -> [NSManagedObject]{
let managedContext =
PersistenceController.shared.context
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: entity)
do {
return try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
return []
}
}
public class func deleteAllCoreDataEntitiesOfType(type: String) -> Bool{
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: type)
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest)
do {
try PersistenceController.shared.context.execute(deleteRequest)
PersistenceController.shared.save()
return true
} catch let error as NSError {
print(error)
return false
}
}
}