Displaying Activity indicator with OCR and using threads - swift

I am trying to display an activity indicator whilst some text recognition happens. If i just start and stop[ the indicator around the recognition code it never shows. The issue i have is that if use:
activityIndicator.startAnimating()
DispatchQueue.main.async( execute: {
self.performTextRecognition()
})
activityIndicator.stopAnimating()
self.performSegue(withIdentifier: "segueToMyBills", sender: self)
The indicator never shows as it performs the segue and the table view in the next view controller shows no information because the text recognition hasn't completed. I've never touched on threads until now so a little insight on what to do would be much appreciated. Thanks!

Well your problem is that your OCR happens on the main thread. That blocks the thread thus never having time to draw the activity indicator. Try to modify your code into this:
activityIndicator.startAnimating()
DispatchQueue.global(qos: .background).async { [weak weaKSelf = self] in
// Use a weak reference to self here to make sure no retain cycle is created
weakSelf?.performTextRecognition()
DispatchQueue.main.async {
// Make sure weakSelf is still around
guard let weakSelf = weakSelf else { return }
weakSelf.activityIndicator.stopAnimating()
weakSelf.performSegue(withIdentifier: "segueToMyBills", sender: weakSelf)
}
}

Try to add a completion handler to your self.performTextRecognition() function in this way
function performTextRecognition(completion: ((Error?) -> Void)? = .none) {
//you can replace Error with Any type or leave it nil
//do your logic here
completion?(error)
}
and then call the function like this :
performTextRecognition(completion: { error in
activityIndicator.stopAnimating()
self.performSegue(withIdentifier: "segueToMyBills", sender: self)
})

Related

change UIVIew visibility during a long func call

I have a search page with uses OAuth to make an external call to a website for data. Sometimes the data is very quick and others quite long. So I have created a custom object (Searching) to display on screen to indicate that a search is happening (the custom object is just 2 UIImageViews in a UIView)
The problem is that the searching.isHidden = false doesn't actually happen until the end of the func which happens after it gets all the data, even though it is called first. Which is obviously too late.
I tried moving the isHidden to a background thread but get an error saying UIView calls must be on the main thread
I tried moving the display call to its own func with an #escaping callback and then run the search after it returns but it still does not update.
If I remove the search() line it displays properly.
I've also tried forcing a refresh on the object using
self.view.setNeedsLayout()
self.view.setNeedsDisplay()
and it didn't work
class Search {
override func viewDidLoad() {
super.viewDidLoad()
searching.isHidden = true
}
#IBAction func search(_ sender: Any) {
if self.searching.isHidden == false {
self.searching.isHidden = true
}
else {
self.searching.isHidden = false
}
self.view.setNeedsLayout()
self.view.setNeedsDisplay()
//I've also tried using an escaping func to call the isHidden and call back when complete
//self.searching.show() {
//self.view.setNeedsLayout()
//self.view.setNeedsDisplay()
//self.search()
//}
//I've tried an async call
// DispatchQueue.main.async {
// self.search()
// }
}
func search() {
keywordText.resignFirstResponder()
//perform OAuth external search
if results.count > 1 {
searching.isHidden = true
self.performSegue(withIdentifier: "results", sender: nil)
}
return
}
}
On iOS (and MacOS) UI updates don’t get displayed until your code returns and the event loop gets a chance to run. Code that makes UI changes and then immediately does a long-running task on the main thread will not see those changes on-screen until the long-running task completes.
One way to handle this is to change you UI and then use dispatchAsync to trigger the long-running task:
searching.isHidden = false
DispatchQueue.main.async {
// Put your long-running code here.)
}

Trigger a segue from a function (without UI element)

I'm very new with programming. Currently, I need to trigger a segue directly after a function is being executed.
This is my code:
func onlineSearch() {
let urlToGoogle = "https://www.google.com/search?q=\(resultingText)"
let urlString = urlToGoogle.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
url = URL(string: urlString!)
performSegue(withIdentifier: K.goToBrowser, sender: nil)
}
}
When I run this, I get this error:
Warning: Attempt to present <MyApp.SFSafariViewController: 0x10153be70> on <MyApp.CameraViewController: 0x101708460> whose view is not in the window hierarchy!
But if I run all the same, but simply trigger the segue from a button, instead of the way I want, it actually works perfectly.
#IBAction func webViewButtonPressed(_ sender: UIButton) { // TEMP
performSegue(withIdentifier: K.goToBrowser, sender: self)
}
P.S.: Nevermind the grotesque unwrapping, it is just working as a placeholder right now.
Reposting my comment as an answer with a code snippet -
If the caller of onlinesearch() may be on anything other than the main thread, you need to wrap the performSegue call in a DispatchQueue.main.async{} block.
DispatchQueue.main.async {
performSegue(withIdentifier: K.goToBrowser, sender: nil)
}
Generally, methods which affect UI (updating controls, performing segues, etc.) need to be on the main thread. Calls from UIKit are already on the main thread (viewDidLoad, draw, events, notifications, timers etc.), but other task completion handlers (and anything run on explicitly on a different queue) may not be.
try using this to open the safari URL
if let url = URL(string: "https://www.google.com/search?q=\(resultingText)") {
UIApplication.shared.open(url)
}
Here's a function if you want to use it anywhere else.
But you should mark Chris Comas answer as the correct one.
func openURL(url: URL) {
UIApplication.shared.open(url, options: [:])
}
}
Just for the sake of knowledge:
let urlGoogle = URL(string: "https://www.google.com")
openURL(url: urlGoogle)
if you want to open the browser inside the app check Swift documentation: https://developer.apple.com/documentation/uikit/uiwebview

Proper use of Dispatch to show activity indicator during a long task

During a computationally intensive task, I wish to show the user an activity indicator. What is the best way to do this?
My task (contrived of course), lasts a couple of seconds:
func startThinking(howMany: Int) {
for i in 0...howMany {
let p:Double = Double(i)
let _ = p / Double.pi
}
delegate?.finishedThinking()
}
This is called on a button tap:
#IBAction func startTap(_ sender: Any) {
Thinker.sharedInstance.startThinking(howMany: 500000000)
myActivity.startAnimating()
}
And stopped when the thinking task is finished:
func finishedThinking() {
print ("finished thinking")
myActivity.stopAnimating()
}
But the activity indicator is not showing up; the UI is blocked by the difficult thinking task.
I've tried putting the startAnimating on the main thread:
DispatchQueue.main.async {
self.myActivity.startAnimating()
}
or the difficult task onto its own thread:
DispatchQueue.global().async {
Thinker.sharedInstance.startThinking(howMany: 500000000)
}
and various other combinations that I've run across in Stack. What am I doing wrong?
Firstly, I would move the call to start animating to before the thinker call, and verify that it works if you don't start thinking. You also need to stop the animation from the main thread.
#IBAction func startTap(_ sender: Any) {
myActivity.startAnimating()
DispatchQueue.global(qos: .userInitiated).async {
Thinker.sharedInstance.startThinking(howMany: 500000000)
}
}
func finishedThinking() {
DispatchQueue.main.async {
myActivity.stopAnimating()
}
}
I adjusted a few things:
moved the .startAnimating() call to be first. It is already on the main thread since it was called from the interface
specify the qos as .userInitiated
run the .stopAnimating() on the main thread

Swift 3 hide NSButton immediately after click

How can I have a button disappear after it's been clicked?
#IBAction func onClick(_ sender: NSButton) {
sender.isHidden = true;
//...a lot of blocking instructions below this line
}
The above works to a certain extent, as the "sender" / button is hidden only after all of the instructions in the function have been processed. I have some blocking IO in the function (socket connections etc.) and I want the button to disappear before all that happens.
I tried using both outlets and sender.
#IBAction func onClick(_ sender: NSButton) {
sender.isHidden = true;
DispatchQueue.main.async {
//...a lot of blocking instructions below this line
}
}
I managed to achieve the desired effect by putting the "blocking" piece of code in the following statement (and pushing the .isHidden setting through immediately, in a synchronous fashion):
DispatchQueue.main.async { /*code*/ }

Cannot reload UITable from didReceiveLocalNotification

My issue is that I am getting local notifications and then telling one of my VCs to updates its UITableView. However, the tableview doesn't reload. I know for a fact that all the if statement checks are passing and it is running the code. This originally made me believe it was a thread issue so I wrapped the reload in dispatch_async(dispatch_get_main_queue(), {}). This however doesn't work either. So if it is not a thread issues what is it? What could be causing this? Am I doing something wrong here?
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
if notification.alertBody != nil{
if notification.alertBody!.hasSuffix("starting") || notification.alertBody!.hasSuffix("ending"){
dispatch_async(dispatch_get_main_queue(), {
if topMostContoller() is SecondViewController{
let vc = self.tabBarController?.getVC(.Attendance) as! SecondViewController
vc.myTableView?.reloadRowsAtIndexPaths((vc.myTableView?.indexPathsForVisibleRows)!, withRowAnimation: .None)
// vc.myTableView?.reloadData() <- this doesn't work either
}
})
}
}
}
Edit:
func getVC(vc:tabBarVC)->UIViewController?{
switch vc{
case .Home:
return fullTabBar[0]
case .Attendance:
return fullTabBar[1]
case .Location:
return fullTabBar[2]
case .Sign:
return fullTabBar[3]
case .Settings:
return fullTabBar[4]
}
}
Where fullTabBar is set in the tabBar's viewDidLoad.
fullTabBar = self.viewControllers as [UIViewController]!
self being the tabBar instance.
I unfortunately can't see anything wrong with your code, logically it should be working. Possibly your data source is being updated after reloadData is being called, although considering the cleanliness of your code I doubt that is the case.
I would try the following pattern, if only to see if the code in your answer is in fact faulty.
In SecondViewController's viewDidLoad:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadTableView:", name: "reloadSecondVCTableView", object: nil)
And in the body:
func reloadTableView(_: NSNotification) {
tableView.reloadData()
}
And after the second if statement in your posted code send the notification :
NSNotificationCenter.defaultCenter().postNotificationName("reloadSecondVCTableView", object: nil)