Post

Replies

Boosts

Views

Activity

Reply to Run something before another thing
Here you go:     override func viewDidLoad() {         super.viewDidLoad()         // Do any additional setup after loading the view.         if NetworkMonitor.shared.isConnected {             Utilities.checkads(bannerView: bannerView, bannerViewHeight: bannerViewHeight)             checkAppVersion()             setRandomJoke()             DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {                // Code you want to be delayed                 self.setRandomJoke()                 self.checkRandomJokeFail()             }         }else {             randomJoke.text = "Random Joke: Failed to connect to server"             adLoadError.text = "Error Loading Ad"             Utilities.checkToHideAds(bannerViewHeight: bannerViewHeight)         }         Utilities.styleFilledButton(changeJoke)         recentCat.delegate = self         recentCat.dataSource = self         checkRecentCat()         if Utilities.openNum == 1 {             Welcome.text = "Welcome! Check out our app. In the categories section you will find all of our jokes!"         }         bannerView.rootViewController = self         bannerView.delegate = self         print("saveCat Defaults = \(Utilities.saveCat)")     }     override func viewWillAppear(_ animated: Bool) {         super.viewWillAppear(animated)         if NetworkMonitor.shared.isConnected {             print("connected to internet")             adLoadError.text = "Loading Ad"             Utilities.checkads(bannerView: bannerView, bannerViewHeight: bannerViewHeight)             bannerView.rootViewController = self             checkRecentCat()             recentCat.reloadData()             if randomJoke.text == "Random Joke: " || randomJoke.text == "Random Joke: Failed to connect to server" {                 setRandomJoke()                 DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {                    // Code you want to be delayed                     self.setRandomJoke()                     self.checkRandomJokeFail()                 }             }         }else {             print("not connected to internet")             adLoadError.text = "Error Loading Ad"         }         Utilities.checkToHideAds(bannerViewHeight: bannerViewHeight)     }     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {         let backItem = UIBarButtonItem()         backItem.title = "Back"         navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed     }
Topic: UI Frameworks SubTopic: UIKit Tags:
Mar ’21
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 }
Topic: UI Frameworks SubTopic: UIKit Tags:
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" } }
Topic: UI Frameworks SubTopic: UIKit Tags:
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)") } } } } }
Topic: UI Frameworks SubTopic: UIKit Tags:
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.
Topic: Programming Languages SubTopic: Swift Tags:
Mar ’21