-1

I am trying to store the local player score in pScore but after the block the variable always equals 0, How can i store the "localPlayerScore!.value" into pScore:Int.

    let leaderBoardRequest = GKLeaderboard()
    leaderBoardRequest.identifier = "leaderboard ID"

    leaderBoardRequest.loadScoresWithCompletionHandler {
        (scores, error) -> Void in
        if (error != nil) {
            print("Error: \(error!.localizedDescription)")
        } else if (scores != nil) {
            let localPlayerScore = leaderBoardRequest.localPlayerScore
            self.pScore = Int(localPlayerScore!.value)
        }
    }

    print("Local player's score: \(pScore)")
  • 6
    Looks like a classic asynchronous problem which has been covered countless times on this site before. It's not that your `pScore` isn't getting set, it's that `loadScoresWithCompletionHandler` will do the request in the background, and therefore will return immediately. Therefore your print will be called before the completion handler. Move your print into the completion handler body to get the retrieved score to print. – Hamish May 29 '16 at 15:27

3 Answers3

4

My guess is your request is asynchronous which means you can not know when it does finish. So you are trying to call the print function in the main thread which is called before the asynchronous task. Try to put break points in self.pScore = Int(localPlayerScore!.value) and print("Local player's score: \(pScore)") lines. You will understand what i mean. Anyway if you just try to print your data inside the asynchronous task you will access the result such as:

let leaderBoardRequest = GKLeaderboard()
leaderBoardRequest.identifier = "leaderboard ID"

leaderBoardRequest.loadScoresWithCompletionHandler {
    (scores, error) -> Void in
    if (error != nil) {
        print("Error: \(error!.localizedDescription)")
    } else if (scores != nil) {
        let localPlayerScore = leaderBoardRequest.localPlayerScore
        self.pScore = Int(localPlayerScore!.value)
        print("Local player's score: \(self.pScore)")
    }
}
ridvankucuk
  • 2,407
  • 1
  • 23
  • 41
0

Thanks i have sorted the problem if i call the print after its finished.

 let leaderBoardRequest = GKLeaderboard()
 leaderBoardRequest.identifier = "leaderboard ID"

 leaderBoardRequest.loadScoresWithCompletionHandler {
     (scores, error) -> Void in
     if (error != nil) {
         print("Error: \(error!.localizedDescription)")
     } else if (scores != nil) {
         let localPlayerScore = leaderBoardRequest.localPlayerScore
         self.pScore = Int(localPlayerScore!.value)
     }
     self.cont()
 }

 func cont() {
     print("Local player's score: \(pScore)")
 }
0

I have mocked some code and would like to raise some (possibly, architecture / style) questions. See the code. The solution below does work in Playground.

import Foundation.NSError

typealias GKLeaderboardCompletion = (Int?,NSError?)->(Void)

class GKLeaderboard {
    var identifier: String = ""
    var localPlayerScore: Int?
    func loadScoresWithCompletionHandler(completion: GKLeaderboardCompletion) {
        sleep(2)
        localPlayerScore = 10
        completion(localPlayerScore, nil)
    }
}

class GKSomeClass {

    var pScore: Int? = nil

    func someFunc() {
        let leaderBoardRequest = GKLeaderboard()
        leaderBoardRequest.identifier = "leaderboard ID"

        // in order to be safe here, better to put "[unowned self]" (read more)
        leaderBoardRequest.loadScoresWithCompletionHandler { [unowned self]
            (scores, error) -> Void in
            if (error != nil) {
                print("Error: \(error!.localizedDescription)")
            } else if (scores != nil) {

                // #1: Why don't you utilize the "scores"?

                // #2: Does "leaderBoardRequest.localPlayerScore == scores" at the time of the callback?

                // #3: What's the type of "leaderBoardRequest.localPlayerScore" here below?
//                self.pScore = Int(localPlayerScore!.value)

                let localPlayerScore = leaderBoardRequest.localPlayerScore
                self.pScore = localPlayerScore

                print("Local player's score: \(self.pScore)")
            }
        }
    }
}

let someClass = GKSomeClass()
someClass.someFunc()
Yevhen Dubinin
  • 4,657
  • 3
  • 34
  • 57