I have a loop in my app that can take a few seconds to process.
I want the screen to dismiss immediately while the loop runs in the background.
Is this possible?
I've tried adding only the loop:
DispatchQueue.main.async {
for _ in 1...self.numberOfTransactionsToAdd {
let newTransaction = Transaction()
var timeAdded = 1.months
newTransaction.transactionAmount = self.amountTextField.text!.toDouble()
newTransaction.transactionDescription = self.descriptionTextField.text
newTransaction.transactionDate = self.datePicked
newTransaction.transactionCategory = self.categoryPicked
newTransaction.repeatInterval = self.repeatInterval
newTransaction.transactionSubCategory = self.subcategoryPicked
newTransaction.subCategoryName = self.subcategoryPicked!.subCategoryName
try! self.realm.write {
self.realm.add(newTransaction)
}
self.datePicked = self.datePicked + timeAdded
}
}
}
I've tried adding the whole function:
@IBAction func doneButtonPressed(_ sender: UIButton) {
self.dismiss(animated: true, completion: nil)
DispatchQueue.main.async {
self.saveTransaction()
}
}
But the screen still hangs before the view is dismissed.
This is my first time using DispatchQueue. I've read Apple's documentation on it, but it is extremely vague.
Is this even the right way to go about this?
Can someone help guide a newbie to the correct way to do this?