swift - How to avoid multiple instances on Parse.com? -
this code parse docs. each time run it, have object created. want create once, next update (i.e. position etc, not counter of game score)
you parse, think it's simple common issue, how can fix it? suppose should retrieve objectid after first entry, make query, how ?
edit after wain comment
//mark: example parse //******************************************************* //single pfobject countains: score: 1337, playername: "sean plott", cheatmode: false //let's make object let kuserdefaultsparseid = "userdefaultsparseid" let kidgotfromparse: anyobject? = nsuserdefaults.standarduserdefaults().objectforkey(kuserdefaultsparseid) if kidgotfromparse == nil { //let firstentry = "bi4rioeqda" var gamescore = pfobject(classname:"gamescore") gamescore["score"] = 1337 gamescore["playername"] = "sean plott" gamescore["cheatmode"] = false gamescore.saveinbackgroundwithblock { (success: bool, error: nserror?) -> void in if (success) { // object has been saved. var theid = gamescore.objectid nsuserdefaults.standarduserdefaults().setobject(theid, forkey: kuserdefaultsparseid) nsuserdefaults.standarduserdefaults().synchronize() } else { // there problem, check error.description } } } else { // //retrive objects // var query = pfquery(classname:"gamescore") // query.getobjectinbackgroundwithid(firstentry) { // (gamescore: pfobject?, error: nserror?) -> void in // if error == nil && gamescore != nil { // println(gamescore) // } else { // println(error) // } // } var query = pfquery(classname:"gamescore") query.getobjectinbackgroundwithid(theid) { (gamescore: pfobject?, error: nserror?) -> void in if error != nil { println(error) } else if let gamescore = gamescore { gamescore["cheatmode"] = true gamescore["score"] = 30 gamescore.saveinbackgroundwithblock(nil) } } } //*******************************************************
where have the object has been saved
can id
, save somewhere, user defaults. if have saved query it, otherwise create new 1 , save id.
Comments
Post a Comment