mirror of
https://github.com/Mueller-Patrick/DHBW-Service-App.git
synced 2024-11-22 01:13:57 +00:00
🐛 Fixing bug where User data was not properly saved to CoreData
This commit is contained in:
parent
46551258e2
commit
63b1e56618
|
@ -18,6 +18,7 @@
|
|||
CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD721925912E1200FBF2F5 /* HomeView.swift */; };
|
||||
CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD72232591316500FBF2F5 /* LocalSettings.swift */; };
|
||||
CDCD7230259135C500FBF2F5 /* FirstOpeningSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD722F259135C500FBF2F5 /* FirstOpeningSettings.swift */; };
|
||||
CDDCF47B2591FE550027CDC5 /* UtilityFunctions.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDDCF47A2591FE550027CDC5 /* UtilityFunctions.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
|
@ -55,6 +56,7 @@
|
|||
CDCD721925912E1200FBF2F5 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; };
|
||||
CDCD72232591316500FBF2F5 /* LocalSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalSettings.swift; sourceTree = "<group>"; };
|
||||
CDCD722F259135C500FBF2F5 /* FirstOpeningSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstOpeningSettings.swift; sourceTree = "<group>"; };
|
||||
CDDCF47A2591FE550027CDC5 /* UtilityFunctions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UtilityFunctions.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
@ -107,6 +109,7 @@
|
|||
children = (
|
||||
CDCD720F25912D3C00FBF2F5 /* App */,
|
||||
CDCD721025912D4900FBF2F5 /* Views */,
|
||||
CDDCF4792591FE410027CDC5 /* Utility */,
|
||||
CDCD72222591314000FBF2F5 /* Observables */,
|
||||
CDCD721125912D5400FBF2F5 /* CoreData */,
|
||||
CDCD721225912D6300FBF2F5 /* Supporting Files */,
|
||||
|
@ -201,6 +204,14 @@
|
|||
path = Other;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
CDDCF4792591FE410027CDC5 /* Utility */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
CDDCF47A2591FE550027CDC5 /* UtilityFunctions.swift */,
|
||||
);
|
||||
path = Utility;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
/* End PBXGroup section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
|
@ -336,6 +347,7 @@
|
|||
CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */,
|
||||
CD9FAB8D258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld in Sources */,
|
||||
CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */,
|
||||
CDDCF47B2591FE550027CDC5 /* UtilityFunctions.swift in Sources */,
|
||||
CD9FAB81258EC60200D6D0C5 /* DHBW_ServiceApp.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
@ -495,7 +507,7 @@
|
|||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\"";
|
||||
DEVELOPMENT_TEAM = G3TRS8UHWN;
|
||||
DEVELOPMENT_TEAM = HS7KNT4MZ2;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
|
||||
|
@ -517,7 +529,7 @@
|
|||
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\"";
|
||||
DEVELOPMENT_TEAM = G3TRS8UHWN;
|
||||
DEVELOPMENT_TEAM = HS7KNT4MZ2;
|
||||
ENABLE_PREVIEWS = YES;
|
||||
INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
|
||||
|
|
|
@ -51,7 +51,7 @@ struct PersistenceController {
|
|||
if self.context.hasChanges {
|
||||
do {
|
||||
try self.context.save()
|
||||
print("In CoreData.stack.save()")
|
||||
print("In PersistenceController.shared.save()")
|
||||
} catch {
|
||||
let nserror = error as NSError
|
||||
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
|
||||
|
|
42
DHBW-Service/Utility/UtilityFunctions.swift
Normal file
42
DHBW-Service/Utility/UtilityFunctions.swift
Normal file
|
@ -0,0 +1,42 @@
|
|||
//
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -34,6 +34,7 @@ struct FirstOpeningSettings: View {
|
|||
.padding(.horizontal)
|
||||
Button(action: {
|
||||
self.settings.isFirstOpening = !self.settings.isFirstOpening
|
||||
self.saveToCoreData()
|
||||
}){
|
||||
Text("Confirm")
|
||||
.padding()
|
||||
|
@ -47,6 +48,11 @@ struct FirstOpeningSettings: View {
|
|||
|
||||
extension FirstOpeningSettings{
|
||||
func saveToCoreData(){
|
||||
// Delete old user data
|
||||
let status = UtilityFunctions.deleteAllCoreDataEntitiesOfType(type: "User")
|
||||
print("Deleting old user data status: \(status)")
|
||||
|
||||
// Insert new user data
|
||||
let entity = NSEntityDescription.entity(forEntityName: "User", in: PersistenceController.shared.context)!
|
||||
let user = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context)
|
||||
user.setValue(name, forKey: "name")
|
||||
|
|
|
@ -10,10 +10,7 @@ import CoreData
|
|||
|
||||
struct HomeView: View {
|
||||
@EnvironmentObject var settings: LocalSettings
|
||||
@State var user: NSManagedObject = NSManagedObject()
|
||||
|
||||
|
||||
|
||||
@State private var name: String = ""
|
||||
|
||||
var body: some View {
|
||||
VStack {
|
||||
|
@ -24,7 +21,7 @@ struct HomeView: View {
|
|||
}
|
||||
Text("Test")
|
||||
|
||||
// Text(user.value(forKey: "name") as! String)
|
||||
Text(self.name)
|
||||
|
||||
}.onAppear{
|
||||
self.readFromCoreData()
|
||||
|
@ -33,15 +30,12 @@ struct HomeView: View {
|
|||
}
|
||||
|
||||
extension HomeView{
|
||||
func readFromCoreData(){
|
||||
let managedContext = PersistenceController.shared.context
|
||||
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "User")
|
||||
func readFromCoreData() {
|
||||
let fetchedData = UtilityFunctions.getCoreDataObject(entity: "User")
|
||||
|
||||
do {
|
||||
try print(managedContext.fetch(fetchRequest))
|
||||
self.user = try managedContext.fetch(fetchRequest)[0]
|
||||
} catch let error as NSError {
|
||||
print(error)
|
||||
if(!fetchedData.isEmpty) {
|
||||
let user = fetchedData[0]
|
||||
self.name = user.value(forKey: "name") as! String
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user