简体   繁体   中英

Why I can not save data in realm

I have SaveDataToRealm class. In this class I am trying to store data in realm. But I am not able to get it. When I am printing the realm object after writing it is showing the object without the values I tried to write.

class SaveDataToRealm: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        saveSessionData()
    }

    // MARK:-  save session and Close session
    func saveSessionData() {
        let realm = try! Realm()
        let localSession = TempRingSessionObject()
        if isLiveSession{
           sessionId = RandomStringGenerator.sharedInstance.getUniqueSessionString()
            localSession.sessionId = sessionId!
        } else{
            localSession.class_id = courseSetupDetails["class_id"] as! String
        }
        try! realm.write {
            realm.add(localSession)
        }
    }

By using saveSessionData() function I am initializing Realm. In closeSelfSession() function I am trying to store data into Realm.

func closeSelfSession(currentTimeStamp:String)  {   
    let realm = try! Realm()
    if !(sessionId ?? "").isEmpty{
        let session = realm.objects(TempRingSessionObject.self).filter("sessionId contains '\(String(describing: self.sessionId))'").last
        try! realm.write {        
            session?.score = "60"           
            session?.timeOfSession = currentTimeStamp
            session?.tempSessionData.append(objectsIn:realm.objects(TempRingDataObject.self).filter("tag = '\(tagString)'"))
        }
    } else{  
        let session =  realm.objects(TempRingSessionObject.self).filter("sessionId contains '\(self.courseSetupDetails["class_id"] as! String)'").last       
        try! realm.write {    
            session?.tempSessionData.append(objectsIn: realm.objects(TempRingDataObject.self).filter("tag = '\(tagString)'"))
            session?.timeOfSession = currentTimeStamp
            session?.score = "75"
        } 
    }
}

What am I doing wrong ? Thanks!

Your NSPredicate s used for filtering are all flawed.

You should never use String(describing:) to compare variables, since depending on the exact type it might yield unexpected results. Moreover, when using single quotes in predicates, you tell the compiler to look for the exact result and not for a variable's value. You also shouldn't use String interpolation ( "\\(variable)" ) when working with NSPredicate s, you should be using the %@ format specifier.

let session = realm.objects(TempRingSessionObject.self).filter("sessionId contains '\(String(describing: self.sessionId))'").last

should actually be

let session = realm.objects(TempRingSessionObject.self).filter("sessionId CONTAINS @%",self.sessionID).last

This predicate realm.objects(TempRingDataObject.self).filter("tag = '\\(tagString)'") should be

realm.objects(TempRingDataObject.self).filter("tag = %@",tagString)

And your last predicate,

realm.objects(TempRingSessionObject.self).filter("sessionId contains '\(self.courseSetupDetails["class_id"] as! String)'"

should be

realm.objects(TempRingSessionObject.self).filter("sessionId CONTAINS %@",self.courseSetupDetails["class_id"])

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM