I'm not sure what's going on here. I'm trying to save the test data to the database and it won't save. It saves the data in my Realm Class the defaults I set, but it doesn't save any of the ones I'm trying to save, they show empty. It's freaking weird stuff.
Here is My Save Code:
let medHub: MedInfo!
medHub = MedInfo()
if let txtMedName = txtMedName.text {
medHub.medName = txtMedName
}
if let txtMedDose = txtMedDose.text {
medHub.medDose = txtMedDose
}
if let txtDoctorsName = txtDoctorsName.text {
medHub.doctorName = txtDoctorsName
}
if let txtRxDate = txtRxDate.text {
medHub.rxDate = txtRxDate
}
if let imageViewDisplay = imageViewDisplay.image {
if let nsDataImage = imageViewDisplay.jpegData(compressionQuality: 0.1) as NSData? {
medHub.imageNSDATA = nsDataImage
}
// Save Data To Realm
//MARK: Save Data
let medHub = MedInfo()
let realm = try? Realm()
try? realm?.write {
realm?.add(medHub)
}
print("Data Was Saved To Realm Database.")
}
Here is my Realm Class
class MedInfo: Object {
// Info To Be Stored
@objc dynamic var keyID = UUID().uuidString
@objc dynamic var medName: String = ""
@objc dynamic var medDose: String = ""
@objc dynamic var currentDate: Date = Date()
@objc dynamic var timeTook: Date = Date()
@objc dynamic var lastTook: Date = Date()
@objc dynamic var durationOfTime: Date = Date()
@objc dynamic var doctorName: String = ""
@objc dynamic var rxDate: String = ""
@objc dynamic var medInfo: String = ""
@objc dynamic var linkPicture: String = ""
@objc dynamic var imageNSDATA: NSData?
@objc dynamic var isGood: Bool = false
@objc dynamic var extraDate: Date = Date()
@objc dynamic var extraString: String = ""
@objc dynamic var extraBool: Bool = false
override static func primaryKey() -> String? {
return "keyID"
}
}
I call the function to save the data after I fill in all my textfields. Then I open up my Real Database and watch it as the Data Saves it saves all the default dynamic vars in my Realm Class MedInfo but it doesn't make record of any of the data from my textfields. I've provided screen shot of database. Also Screen shot of my View Where I type in TextFileds To Save.
Here is Screen shot of my View I'm filling out, it doesn't save anything I put in Textfields.
Any help of what I'm doing wrong would be cool. I'm new to all this so I'm just learning here. As you can see in Database Screen Shot I tried saving the data like Three times No Luck.
because you save the empty object
let medHub = MedInfo()
let realm = try? Realm()
try? realm?.write {
realm?.add(medHub)
}
let medHub = MedInfo() is create a new object with empty value / default value
You are creating two instances from MedInfo, the first instance you fill it from your textfelid, the second instance you create it before you save it in realm for that it take the default values, for that you got this issue, first you should fill your instance like how you show us in your code, then send your instance as perimeter to save method without creating new instance:
let medHub: MedInfo!
medHub = MedInfo()
if let txtMedName = txtMedName.text {
medHub.medName = txtMedName
}
if let txtMedDose = txtMedDose.text {
medHub.medDose = txtMedDose
}
if let txtDoctorsName = txtDoctorsName.text {
medHub.doctorName = txtDoctorsName
}
if let txtRxDate = txtRxDate.text {
medHub.rxDate = txtRxDate
}
if let imageViewDisplay = imageViewDisplay.image {
if let nsDataImage = imageViewDisplay.jpegData(compressionQuality: 0.1) as NSData? {
medHub.imageNSDATA = nsDataImage
}
}
saveRealmObject(medHub)
Here is the save method that you can use to save your object
// Save Data To Realm
//MARK: Save Data
func saveRealmObject(medHub:MedInfo) {
let realm = try? Realm()
try? realm?.write {
realm?.add(medHub)
}
print("Data Was Saved To Realm Database.")
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.