如何在Parse.com上避免多个实例?

时间:2015-06-13 14:22:44

标签: swift parse-platform

这是来自Parse docs的代码。每次运行它,我都会创建另一个对象。我只想创建一次,接下来只更新(I.E.位置等,而不是游戏得分的计数器)

你来自Parse,我认为这是一个简单但常见的问题,我该如何解决?我想我应该在第一个条目之后检索objectId,然后进行查询,但是怎么做?

在Wain评论之后编辑

//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)
        }
    }
    }
    //*******************************************************

1 个答案:

答案 0 :(得分:1)

您拥有the object has been saved的地方可以获得id并将其保存在某处,例如用户默认设置。如果你保存了它然后查询它,否则创建一个新的并保存id。