Home > Back-end >  Is there is any way to print two or more toast message in our app?
Is there is any way to print two or more toast message in our app?

Time:06-16

I am showing the toast message using the following method,

DispatchQueue.main.async {
    NotificationCenter.default.post(name: Notification.Name.toastMessage, object: nil, userInfo: ["toastMessage": errorMessage.localized])
}

I want to show this two times, is there is any way to do that ?

CodePudding user response:

delay in seconds:

   func doubleToast(message:String,delay:Double) {
        NotificationCenter.default.post(name: Notification.Name.toastMessage, object: nil, userInfo: ["toastMessage": message])
        DispatchQueue.main.asyncAfter(deadline: .now()   delay) {
            NotificationCenter.default.post(name: Notification.Name.toastMessage, object: nil, userInfo: ["toastMessage": message])
        }
    }

how to show two toast with delay of 1.5 seconds:

 doubleToast(message: errorMessage.localized, delay:1.5)

for mulitple times you can use this trick

func doubleToast(message:String,delay:Double) {
          DispatchQueue.main.asyncAfter(deadline: .now()   delay) {
              NotificationCenter.default.post(name: Notification.Name.toastMessage, object: nil, userInfo: ["toastMessage": message])
          }
    }

var delay:Double = 2

func tenTimeShow() {
    doubleToast(message: "some message", delay: 0)
    for i in 2...10  { //starting from 2 because already fired one above
        doubleToast(message: "some message", delay: delay)
        delay = delay * Double(i)
    }
}

but here one more thing i want to mention that the removing time of your toast if the delay is less than removing time then it will override on previous toast so if you want show multiple toast on same time as like facebook notification in stack then you need set up a stack toast or if you just want to show 2 toast one after another then it will work fine

  • Related