Add CoreData

This commit is contained in:
2023-01-13 19:12:23 +01:00
parent ee03888990
commit 94bb75bef8
8 changed files with 288 additions and 20 deletions
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="21513" systemVersion="22C65" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="User" representedClassName="User" syncable="YES">
<attribute name="firstName" optional="YES" attributeType="String"/>
<attribute name="jwt" optional="YES" attributeType="String"/>
<attribute name="username" optional="YES" attributeType="String"/>
</entity>
</model>
@@ -0,0 +1,43 @@
//
// User+CoreDataClass.swift
// Firebonk
//
// Created by Patrick Müller on 13.01.23.
//
import Foundation
import CoreData
@objc(User)
public class User: NSManagedObject {
@nonobjc public class func getAll() -> [User] {
let managedContext =
PersistenceController.shared.context
do {
return try managedContext.fetch(User.fetchRequest())
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
return []
}
}
@nonobjc public class func getSpecified(sortDescriptors: [NSSortDescriptor] = [], searchPredicate: NSPredicate? = nil) -> [User]{
let managedContext =
PersistenceController.shared.context
let fetchRequest: NSFetchRequest = User.fetchRequest()
fetchRequest.sortDescriptors = sortDescriptors
if(searchPredicate != nil) {
fetchRequest.predicate = searchPredicate
}
do {
return try managedContext.fetch(fetchRequest)
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
return []
}
}
}
@@ -0,0 +1,26 @@
//
// User+CoreDataProperties.swift
// Firebonk
//
// Created by Patrick Müller on 13.01.23.
//
import Foundation
import CoreData
extension User {
@nonobjc public class func fetchRequest() -> NSFetchRequest<User> {
return NSFetchRequest<User>(entityName: "User")
}
@NSManaged public var username: String?
@NSManaged public var jwt: String?
@NSManaged public var firstName: String?
}
extension User : Identifiable {
}
+78
View File
@@ -0,0 +1,78 @@
//
// Persistence.swift
// Firebonk
//
// Created by Patrick Müller on 13.01.23.
//
import Foundation
import CoreData
struct PersistenceController {
// Singleton
static let shared = PersistenceController()
// Cloud Kit container
let container: NSPersistentCloudKitContainer
// Managed object context
public var context: NSManagedObjectContext {
get {
return self.container.viewContext
}
}
// MARK: - Constructor
init(inMemory: Bool = false) {
container = NSPersistentCloudKitContainer(name: "Firebonk")
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
}
// MARK: - Core Data Saving support
public func save() {
if self.context.hasChanges {
do {
try self.context.save()
print("In PersistenceController.shared.save()")
} catch {
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}
// MARK: - Preview content
static var preview: PersistenceController = {
let result = PersistenceController(inMemory: true)
let viewContext = result.container.viewContext
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return result
}()
}