🐛 Fixing bug where User data was not properly saved to CoreData

This commit is contained in:
Patrick Müller 2020-12-22 11:30:41 +01:00 committed by Patrick Müller
parent 46551258e2
commit 63b1e56618
5 changed files with 70 additions and 16 deletions

View File

@ -18,6 +18,7 @@
CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD721925912E1200FBF2F5 /* HomeView.swift */; }; CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD721925912E1200FBF2F5 /* HomeView.swift */; };
CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD72232591316500FBF2F5 /* LocalSettings.swift */; }; CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD72232591316500FBF2F5 /* LocalSettings.swift */; };
CDCD7230259135C500FBF2F5 /* FirstOpeningSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = CDCD722F259135C500FBF2F5 /* FirstOpeningSettings.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 */ /* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */ /* Begin PBXContainerItemProxy section */
@ -55,6 +56,7 @@
CDCD721925912E1200FBF2F5 /* HomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeView.swift; sourceTree = "<group>"; }; 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>"; }; 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>"; }; 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 */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
@ -107,6 +109,7 @@
children = ( children = (
CDCD720F25912D3C00FBF2F5 /* App */, CDCD720F25912D3C00FBF2F5 /* App */,
CDCD721025912D4900FBF2F5 /* Views */, CDCD721025912D4900FBF2F5 /* Views */,
CDDCF4792591FE410027CDC5 /* Utility */,
CDCD72222591314000FBF2F5 /* Observables */, CDCD72222591314000FBF2F5 /* Observables */,
CDCD721125912D5400FBF2F5 /* CoreData */, CDCD721125912D5400FBF2F5 /* CoreData */,
CDCD721225912D6300FBF2F5 /* Supporting Files */, CDCD721225912D6300FBF2F5 /* Supporting Files */,
@ -201,6 +204,14 @@
path = Other; path = Other;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
CDDCF4792591FE410027CDC5 /* Utility */ = {
isa = PBXGroup;
children = (
CDDCF47A2591FE550027CDC5 /* UtilityFunctions.swift */,
);
path = Utility;
sourceTree = "<group>";
};
/* End PBXGroup section */ /* End PBXGroup section */
/* Begin PBXNativeTarget section */ /* Begin PBXNativeTarget section */
@ -336,6 +347,7 @@
CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */, CDCD72242591316500FBF2F5 /* LocalSettings.swift in Sources */,
CD9FAB8D258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld in Sources */, CD9FAB8D258EC60600D6D0C5 /* DHBW_Service.xcdatamodeld in Sources */,
CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */, CDCD721A25912E1200FBF2F5 /* HomeView.swift in Sources */,
CDDCF47B2591FE550027CDC5 /* UtilityFunctions.swift in Sources */,
CD9FAB81258EC60200D6D0C5 /* DHBW_ServiceApp.swift in Sources */, CD9FAB81258EC60200D6D0C5 /* DHBW_ServiceApp.swift in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
@ -495,7 +507,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\""; DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\"";
DEVELOPMENT_TEAM = G3TRS8UHWN; DEVELOPMENT_TEAM = HS7KNT4MZ2;
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist"; INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 14.0; IPHONEOS_DEPLOYMENT_TARGET = 14.0;
@ -517,7 +529,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\""; DEVELOPMENT_ASSET_PATHS = "\"DHBW-Service/Preview Content\"";
DEVELOPMENT_TEAM = G3TRS8UHWN; DEVELOPMENT_TEAM = HS7KNT4MZ2;
ENABLE_PREVIEWS = YES; ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist"; INFOPLIST_FILE = "DHBW-Service/Supporting Files/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 14.0; IPHONEOS_DEPLOYMENT_TARGET = 14.0;

View File

@ -51,7 +51,7 @@ struct PersistenceController {
if self.context.hasChanges { if self.context.hasChanges {
do { do {
try self.context.save() try self.context.save()
print("In CoreData.stack.save()") print("In PersistenceController.shared.save()")
} catch { } catch {
let nserror = error as NSError let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)") fatalError("Unresolved error \(nserror), \(nserror.userInfo)")

View 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
}
}
}

View File

@ -34,6 +34,7 @@ struct FirstOpeningSettings: View {
.padding(.horizontal) .padding(.horizontal)
Button(action: { Button(action: {
self.settings.isFirstOpening = !self.settings.isFirstOpening self.settings.isFirstOpening = !self.settings.isFirstOpening
self.saveToCoreData()
}){ }){
Text("Confirm") Text("Confirm")
.padding() .padding()
@ -47,6 +48,11 @@ struct FirstOpeningSettings: View {
extension FirstOpeningSettings{ extension FirstOpeningSettings{
func saveToCoreData(){ 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 entity = NSEntityDescription.entity(forEntityName: "User", in: PersistenceController.shared.context)!
let user = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context) let user = NSManagedObject(entity: entity, insertInto: PersistenceController.shared.context)
user.setValue(name, forKey: "name") user.setValue(name, forKey: "name")

View File

@ -10,10 +10,7 @@ import CoreData
struct HomeView: View { struct HomeView: View {
@EnvironmentObject var settings: LocalSettings @EnvironmentObject var settings: LocalSettings
@State var user: NSManagedObject = NSManagedObject() @State private var name: String = ""
var body: some View { var body: some View {
VStack { VStack {
@ -24,7 +21,7 @@ struct HomeView: View {
} }
Text("Test") Text("Test")
// Text(user.value(forKey: "name") as! String) Text(self.name)
}.onAppear{ }.onAppear{
self.readFromCoreData() self.readFromCoreData()
@ -34,14 +31,11 @@ struct HomeView: View {
extension HomeView{ extension HomeView{
func readFromCoreData() { func readFromCoreData() {
let managedContext = PersistenceController.shared.context let fetchedData = UtilityFunctions.getCoreDataObject(entity: "User")
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "User")
do { if(!fetchedData.isEmpty) {
try print(managedContext.fetch(fetchRequest)) let user = fetchedData[0]
self.user = try managedContext.fetch(fetchRequest)[0] self.name = user.value(forKey: "name") as! String
} catch let error as NSError {
print(error)
} }
} }
} }