所以我想检查Parse中的一个类并检查一下:
var query = PFQuery(className:"Score")
query.whereKey("playerName", equalTo:"Bob")
query.whereKey("opponent", equalTo:"Jim")
query.findObjectsInBackgroundWithBlock {
(objects, error) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
// if there is a match, update the record
object["wins"] = 5
}
} //else if there is no match on the whereKey, append the record to "Score" class.
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
有一个基本功能的小示例代码。如果我找到匹配项,我想更新,但如果它不存在则插入它。我正在尝试这样的事情:
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
if objects == [] {
//println(objects)
}
for object in objects {
它没有找到对象,但我不知道如何检测它。必须有一个简单的方法来做到这一点,有人知道吗?
谢谢!
答案 0 :(得分:0)
如果将功能分解为逻辑单元,您可以直接实现的目标。认识到每个都是异步运行,给每个完成块看起来像(可能完全像)解析SDK块......
在您的情况下,您希望找到一个对象,如果它不存在则创建它,并以任何方式更新和保存它。我做了类似以下的事情。 (请注意,我不会写得很快,并且在这里只学到了足够的知识):
// record that player named playerName won against opponent named opponent
func playerBeatOpponent(playerName: String, opponent: String, completion: (success: Bool, error: NSError?) -> Void) {
self.findOrCreateScore(playerName, opponent: opponent, completion: { (scoreObject: PFObject?, error: NSError?) -> Void in
if let scoreObject = scoreObject {
let wins = scoreObject["wins"] as! Int
scoreObject["wins"] = wins + 1
scoreObject.saveInBackgroundWithBlock(completion)
}
})
}
// answer a Score object recording a win between playerName and opponent
// find and return it if it exists, or create it if it doesn't exist
func findOrCreateScore(playerName: String, opponent: String, completion: (scoreObject: PFObject?, error: NSError?) -> Void) {
self.findScore(playerName, opponent: opponent, completion: { (scoreObject: PFObject?, error: NSError?) -> Void in
if scoreObject == nil {
self.createScore(playerName, opponent: opponent, completion: completion)
} else {
completion(scoreObject: scoreObject, error: nil)
}
})
}
// find a score matching the criteria given
func findScore(playerName: String, opponent: String, completion: (scoreObject: PFObject?, error: NSError?) -> Void) {
var query = PFQuery(className:"Score")
query.whereKey("playerName", equalTo:playerName)
query.whereKey("opponent", equalTo:opponent)
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let object: AnyObject = objects?[0] {
completion(scoreObject:object as? PFObject, error: nil)
} else {
completion(scoreObject:nil, error: nil)
}
} else {
completion(scoreObject:nil, error: error)
}
}
}
// create a new score object with the given params
func createScore(playerName: String, opponent: String, completion: (scoreObject: PFObject?, error: NSError?) -> Void) {
var scoreObject = PFObject(className:"Score")
scoreObject["playerName"] = playerName
scoreObject["oponent"] = opponent
scoreObject["wins"] = 1
scoreObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
if (success) {
completion(scoreObject: scoreObject, error: nil)
} else {
completion(scoreObject: nil, error: error)
}
}
}