gpt4 book ai didi

ios - Swift 中根据 whereKey 解析查询更新或追加

转载 作者:行者123 更新时间:2023-11-30 14:06:49 25 4
gpt4 key购买 nike

所以我想检查 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 {

它没有找到对象,但我不知道如何检测它。必须有一个简单的方法来做到这一点,有人知道吗?

谢谢!

最佳答案

如果将功能分解为逻辑单元,则可以直接实现您正在做的事情。认识到每个程序都是异步运行的,为每个程序提供一个看起来像(也许完全一样)解析 SDK block 的完成 block ...

在您的情况下,您想要找到一个对象,如果它不存在则创建它,并以任何一种方式更新和保存它。我会做类似下面的事情。 (请注意,我写得并不快,并且在这里学到的知识仅足以理解更大的想法):

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

关于ios - Swift 中根据 whereKey 解析查询更新或追加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32280320/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com