Reputation: 1943
This is code from Parse docs. Each time I run it, I have another object created. I just want to create it once, next only update (I.E. position etc, not counter of a game score)
You from Parse, I think it's a simple but common issue, how can I fix it? I suppose I should retrieve the objectId after the first entry, then make a query, but how ?
edit after Wain comment
//MARK: example from 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) {
// The object has been saved.
var theId = gameScore.objectId
NSUserDefaults.standardUserDefaults().setObject(theId, forKey: kUserDefaultsParseId)
NSUserDefaults.standardUserDefaults().synchronize()
} else {
// There was a 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)
}
}
}
//*******************************************************
Upvotes: 0
Views: 47
Reputation: 119031
Where you have the object has been saved
you can get the id
and save it somewhere, like user defaults. If you have it saved then query for it, otherwise create a new one and save the id.
Upvotes: 1