mirror of
https://github.com/Mueller-Patrick/DHBW-Service-App.git
synced 2024-11-01 08:53:58 +00:00
43 lines
1.2 KiB
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
|
||
|
}
|
||
|
}
|
||
|
}
|