modo-studio/SugarRecord

The update operation doesn't work

Opened this issue · 0 comments

What

when I update the entity first it works , but it failed when I update it again with different data;

Context

Here is my code:
// I called flowed function first , and it worked when I restart my app.

static func storeAuth(
        grantType:String,
        clientId:Int16,
        clientSecret:String
        ) -> Bool{
        let auth = self.getUserAuth();
        if auth == nil{
            do{
                try AppUser.db.operation({ (context, save) throws in
                    let auth:UserAuth = try context.new();
                    auth.grantType = grantType;
                    auth.clientId = clientId;
                    auth.clientSecret = clientSecret
                    try context.insert(auth)
                    save()
                })
                return true
            }catch{
                debugPrint(error.localizedDescription)
                return false;
            }
        }else{
            do{
                try AppUser.db.operation({ (context, save) throws in
                    auth?.grantType = grantType;
                    auth?.clientId = clientId;
                    auth?.clientSecret = clientSecret
                    save()
                })
                return true;
            }catch{
                debugPrint(error.localizedDescription)
                return false;
            }
        }
    }

// and I call this function to update it again, but when I restart my app, the updated data was not stored. the data was that updated before;

static func updateAuthToken(expiresIn:Int,
                                refreshToken:String,
                                tokenType:String,
                                accessToken:String){
        let auth = self.getUserAuth();
        if auth != nil{
            do{
                try AppUser.db.operation({ (context, save) throws in
                    auth?.tokenExpiredAt = NSDate().addingSeconds(expiresIn) as NSDate?
                    auth?.refreshToken = refreshToken
                    auth?.tokenType = tokenType
                    auth?.accessToken = accessToken
                    save()
                })
            }catch{
                debugPrint(error.localizedDescription)
            }
        }
    }