I figured out that for my case, I can just call it and then wait 0.6 seconds and call it again. To wait blank seconds use (replace 0.6 with the wanted amount):
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
// Put your code which should be executed with a delay here
}
Post
Replies
Boosts
Views
Activity
Ok, thank you for trying.
Generally, you should better not pick up some fragments of codes, but better show whole method.
I don’t really understand what you are saying, could you please try to phrase it differently?
What is the method header for the lines let pickedCat = Int.random(in: 1...4);...and how are you calling it?
I am calling it with the ifs and if elses below it.
Here is the entire function:
var countDadJokes = Int()
var countAssistantJokes = Int()
var countKnockKnockJokes = Int()
var countRandomJokes = Int()
func getDadJokes() {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Dad Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countDadJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Dad Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countDadJokes))") as? String {
self.DadJokes = Joke
print("Joke: \(self.DadJokes)")
}
}
}
}
}
func getAssistantJokes() {
if countAssistantJokes == 0 {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Assistant Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countAssistantJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Assistant Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countAssistantJokes))") as? String {
self.AssistantJokes = Joke
print("Joke: \(self.AssistantJokes)")
}
}
}
}
}
}
func getKnockKnockJokes() {
if countDadJokes == 0 {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Knock Knock Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countKnockKnockJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Knock Knock Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countKnockKnockJokes))") as? String {
self.KnockKnockJokes = Joke
print("Joke: \(self.KnockKnockJokes)")
}
}
}
}
}
}
func getRandomJokes() {
if countDadJokes == 0 {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Random Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countRandomJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Dad Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countRandomJokes))") as? String {
self.RandomJokes = Joke
print("Joke: \(self.RandomJokes)")
}
}
}
}
}
}
let pickedCat = Int.random(in: 1...4)
//print("picked cat: \(pickedCat)")
if pickedCat == 1 {
getRandomJokes()
randomJoke.text = "Random Joke: \(self.DadJokes)"
//print("Random Joke: \(self.DadJokes)")
}else if pickedCat == 2 {
getAssistantJokes()
randomJoke.text = "Random Joke: \(self.AssistantJokes)"
//print("Random Joke: \(self.AssistantJokes)")
}else if pickedCat == 3 {
getKnockKnockJokes()
randomJoke.text = "Random Joke: \(self.KnockKnockJokes)"
//print("Random Joke: \(self.KnockKnockJokes)")
}else if pickedCat == 4 {
getRandomJokes()
randomJoke.text = "Random Joke: \(self.RandomJokes)"
//print("Random Joke: \(self.RandomJokes)")
}
if randomJoke.text == "Random Joke: " {
randomJoke.text = "Random Joke: Failed to connect to server"
}
}
I haven’t really played with completion handlers, could you give me an example?
I want it to: Pick the category
Get count of the documents on firestore in that category
Randomly pick a document
display that document
Here is the DadJokes function, they are all the same except for some pass ins:
func getDadJokes() {
let collectionRef = self.db.collection("jokes")
let documentRef = collectionRef.document("Dad Jokes")
documentRef.getDocument(completion: { documentSnapshot, error in
if let error = error {
print(error.localizedDescription)
return
}
countDadJokes = (documentSnapshot?.data()!.count)! + 1
//print("count = \(count)")
})
db.collection("jokes").document("Dad Jokes").addSnapshotListener { document, error in
//check for error
if error == nil {
//check if document exists
if document != nil && document!.exists {
if let Joke = document!.get("\(Int.random(in: 0...countDadJokes))") as? String {
self.DadJokes = Joke
print("Joke: \(self.DadJokes)")
}
}
}
}
}
I mean the
get_Jokes()
Function. This isn’t running before I set the text and I need it to.
I need the function to run before it sets the text.
Apple recommends logging out (and unchecking reopen applications box) and logging back in and trying to eject. If that doesn't work shut it down, pull it out and boot back up.
You pretty much have to. I use:
import UIKit
import WidgetKit
...
//code
//code for widget
UDM.shared.UserDefaults(suiteName: "group.whatever")?.setValue(urCode, forKey: "I'm a key")
WidgetCenter.shared.reloadAllTimelines()
Note: you have to set up a shared group between the app and the widget in the capabilities of the targets.
Yes, that worked. Thank You!
Thank you! The second thing worked
I meant to do the first, i made a mistake.