I have 2 structs codable : Student and Adress (which is linked to Student)
On my app, I fetch data from Firebase RTDB and then I store it with userdefault Let's say a student changes his email and I just want to update the stored userdefault only with that updated email.
Do I need to specify all other data when I want to store it(name,adress,dob...) or can I just only update/store the email on my userdefault without specifying the other data ?
struct Adress : Codable, Identifiable {
var id: String { Student_UID }
var Student_UID: String
var Student_Street: String
var Student_Country: String
var Student_City: String
enum CodingKeys: String, CodingKey {
case Student_UID = "Adress_Student_UID"
case Student_Street = "Student_Street"
case Student_Country = "Student_Country"
case Student_City = "Student_City"
}
}
struct Student_Profile_Data:Codable, Identifiable {
var id: String { Student_UID }
var Student_UID: String
var Student_firstName: String?
var Student_username : String?
var Student_Email : String?
var Student_lastName: String
var Student_Address: Adress?
var Student_DOB: String?
var Student_Studentpoints : Int?
enum CodingKeys: String, CodingKey {
case Student_UID = "Student_UID"
case Student_firstName = "Student_firstName"
case Student_username = "Student_username"
case Student_Email = "Student_Email"
case Student_lastName = "Student_lastName"
case Student_Address = "Student_Address"
case Student_DOB = "Student_DOB"
case Student_Studentpoints = "Student_Studentpoints"
}
}
The USERDEfault part:
//READ
NSLog("userdefault TEST READ")
let defaults = UserDefaults.standard
if let savedStudent = defaults.object(forKey: "SavedStudent") as? Data {
let decoder = JSONDecoder()
if let loadedStudent = try? decoder.decode(Student_Profile_Data.self, from: savedStudent) {
NSLog("TEST PROFILE- username : \(loadedStudent.Student_username)")
}}
//WRITE
let add1 = Adress(Student_UID: "", Student_Street: "", Student_Country: "", Student_City: "")
let stud = Student_Profile_Data(Student_firstName: "", Student_username: "Martin", Student_Email: "", Student_lastName: "", Student_Address: add1, Student_DOB: "", Student_Studentpoints: 0, Student_UID: "")
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(stud) {
let defaults = UserDefaults.standard
defaults.set(encoded, forKey: "SavedStudent")
NSLog("WRITE OK »)
}
//UPDATE ONE (OR TWO) VALUES ?
CodePudding user response:
If you are storing the student in UserDefault
in this way, then updating it would involve the three-step process of reading the student, updating its value, then writing it back.
// read
let defaults = UserDefaults.standard
let decoder = JSONDecoder()
var savedStudent = defaults.data(forKey: "SavedStudent").flatMap {
try? decoder.decode(StudentProfileData.self, from: $0)
} ?? StudentProfileData(...) // some "empty" student to use when there is no previously saved student
// update
savedStudent.firstName = "John"
// write
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(savedStudent) {
defaults.set(encoded, forKey: "SavedStudent")
}
To make this more convenient, you can extract this as a function:
func updateSavedStudent(updateBlock: (inout StudentProfileData) -> Void) {
// read
let defaults = UserDefaults.standard
let decoder = JSONDecoder()
var savedStudent = defaults.data(forKey: "SavedStudent").flatMap {
try? decoder.decode(StudentProfileData.self, from: $0)
} ?? StudentProfileData(...) // some "empty" student to use when there is no previously saved student
// update
updateBlock(&savedStudent)
// write
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(savedStudent) {
defaults.set(encoded, forKey: "SavedStudent")
}
}
// usage:
updateSavedStudent {
$0.firstName = "John"
$0.lastName = "Smith"
}
Alternatively, make a computed property for this saved student and put it in a utility class somewhere. Do note that this will encode and decode the student once for every property you update though.
static var savedStudent: StudentProfileData {
get {
let defaults = UserDefaults.standard
let decoder = JSONDecoder()
return defaults.data(forKey: "SavedStudent").flatMap {
try? decoder.decode(StudentProfileData.self, from: $0)
} ?? StudentProfileData(...) // some "empty" student to use when there is no previously saved student
}
set {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(newValue) {
defaults.set(encoded, forKey: "SavedStudent")
}
}
}
// usage
savedStudent.firstName = "John"