Post

Replies

Boosts

Views

Activity

Reply to Run something before another thing
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 }
Mar ’21
Reply to Run something before another thing
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" } }
Mar ’21
Reply to Run something before another thing
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)") } } } } }
Mar ’21
Reply to Passing data from an App VC to its widget
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.
Mar ’21