내 앱에서 이벤트를 만들 때마다 사용자에게 포인트를 주려고합니다. 현재 점수를 검색 한 다음 필요한 점수를 클래스에 다시 저장하도록 PFQuery를 설정하고 있습니다. 내 문제는 일단 생성 된 점수를 업데이트 할 수 없기 때문에 추가 된 점수로 현재 점수 데이터를 "업데이트"하는 방법이 필요하다는 것입니다.
이것은 내 코드입니다.
// Give the User Points
let saveScore = PFUser.currentUser()
var query = PFQuery(className:"User")
query.whereKey("score", equalTo: saveScore!)
query.findObjectsInBackgroundWithBlock ({
objects, error in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
let Score = object["score"] as! String
println(object.objectId)
let Points = ("100" + Score)
saveScore!.setObject(Points, forKey: "score")
saveScore!.saveInBackgroundWithBlock { (success: Bool,error: NSError?) -> Void in
println("Score added to User.");
}
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
})
누구든지 도울 수 있습니까? 감사
현재 사용자가 이미 있으므로 쿼리 할 이유가 없습니다. 그러나 최신 데이터로 작업하고 있는지 확인하기 위해 필요한 경우 가져와야합니다. 가져온 점수 변수를 설정하고 100 문자열을 추가 한 다음 업데이트 된 점수 변수를 다음과 같이 저장합니다.
if let currentUser = PFUser.currentUser() {
currentUser.fetchIfNeededInBackgroundWithBlock({ (foundUser: PFObject?, error: NSError?) -> Void in
// Get and update score
if foundUser != nil {
let score = foundUser!["score"] as! String
let points = "100" + score
foundUser!["score"] = points
foundUser?.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in
if succeeded {
println("score added to user")
}
})
}
})
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다