Write statement is not updating


(Mark spaulding) #1

Hi all,

Can anyone see why the last write statement in the following code would not save to Realm correctly?

When I run the app again, the var reset to the default false as per the model.

Thanks!
Blessings,
—Mark

   var firstLoad:Bool?
    var isUpdated:Bool?
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 13.0, *) {
            self.isModalInPresentation = true
        } else {
            // Fallback on earlier versions
        }

        let realm = try! Realm()
        
//                Print out the fielpath for debug purposes
        print(Realm.Configuration.defaultConfiguration.fileURL!)
        
        let saved = SavedData()
        
        if realm.isEmpty {
            
            if saved.firstLoad {
                try! realm.write {
                    realm.add(saved)
                }
                firstLoad = true
            }
        } else {
            
            //the datebase is not empty so grab the bool
            let saved = realm.object(ofType: SavedData.self, forPrimaryKey: "primeKey")
            firstLoad = saved?.firstLoad
            isUpdated = saved?.update1_2
        }
        
        if isUpdated == false {
            
            let totalReg = realm.objects(Registry.self)
            
            if realm.isEmpty {
                //If user does not have any current tobjects in Realm
                return
            } else {
                for reg in totalReg {
                    //Add existing objuects to lists
                    AllServicesService.addService(r: reg, serviceType: reg.service!)
                }
            }
             try! realm.write {
                saved.update1_2 = true
            }
        }

(Mark spaulding) #2

Hey All,

I tried to delete this post, as I figured it out.

Thanks.
Blessings,
—Mark