Ali Alebrahim
Ali Alebrahim

Reputation: 647

Alert delay - iOS swift

i'm creating a game, whenever the user answers correctly or the time is done an alert will be presented. im using SweetAlert class from GitHub. a sound will be played just before the alert get presented. im facing an annoying problem. i keep getting a delay when the alert shows the first time. all other times are fine.

i thought that the problem is with the sound, i used prepareToPlay and i tried to run the sound on background queue but still the same.

here is my code.

this code is for when the game is over

func updateTime() {
    let shouldTimerStop = timeManager.updateTime()
    if timeManager.time <= 30 {
        timerLabel.textColor = UIColor.redColor()
        SoundManager.backgroundMusicSharedInstance!.volume = 0.05
        //if !clockSoundIsPlaying {
        SoundManager.playClockSound()
            //clockSoundIsPlaying = true
        //}
    }
    timerLabel.text = timeManager.displayedTime(timeManager.time)
    if shouldTimerStop {
        SoundManager.clockSoundSharedInstance!.stop()
        //SoundManager.backgroundMusicSharedInstance?.volume = 0.05
        timer.invalidate()
        timer = NSTimer()
        ///////////////////////////////////////////////////////////
        let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
        dispatch_async(backgroundQueue, {
            SoundManager.playGameOverSound()
        })
        ////////////////////////////////////////////////////////////
        SweetAlert().showAlert("Game Over!", subTitle: "Better luck next time!", style: AlertStyle.Error, buttonTitle:"Restart", buttonColor:UIColor(red: 56.0/255.0, green: 163.0/255.0, blue: 221.0/255.0, alpha: 1.0) , otherButtonTitle:  "Main Menu", otherButtonColor: UIColor(red: 56.0/255.0, green: 163.0/255.0, blue: 221.0/255.0, alpha: 1.0)) { (isOtherButton) -> Void in
            if isOtherButton == true {
                self.score = 0
                self.scoreLabel.text = "Score: \(self.score)"
                self.timeManager.resetTimer()
                self.resetUI()
            }
            else {
                // go to main menu
                self.performSegueWithIdentifier("backToMainMenu", sender: self)
            }
            SoundManager.backgroundMusicSharedInstance!.volume = 0.2
        }

    }
}

and here is the code for when the answer is correct

func correctAnswer() {
        SoundManager.backgroundMusicSharedInstance!.volume = 0.05
        if self.timeManager.time <= 30{
            SoundManager.stopClockSound()
        }
        let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
        dispatch_async(backgroundQueue, {
            SoundManager.playCorrectSound()
        })
        self.timer.invalidate()
        SweetAlert().showAlert("Good job!", subTitle: "You passed the level!", style: AlertStyle.Success, buttonTitle:"Continue", buttonColor:UIColor.init(red: 56/255, green: 163/255, blue: 221/255, alpha: 1)) { (isOtherButton) -> Void in
            if isOtherButton == true {
                self.continueWithTheGame()

            }
        }

    }

Upvotes: 1

Views: 242

Answers (2)

spadaroapps
spadaroapps

Reputation: 68

This question seems to have a similar issue...

Very strange issue in Swift code. Delay to make operations

Upvotes: 2

kspa
kspa

Reputation: 73

Were you testing on a simulator or a real device? It could just be your device. If you are using a simulator, it could be lag. That happened with my app, but it just stopped happening. There doesn't seem to be anything in your code that could be causing this problem.

Upvotes: 2

Related Questions