10
votes

Does any body know what I need to check if app freezes after some time? I mean, I can see the app in the iPhone screen but no view responds.

I did some google and i found that, i've blocked the main thread somehow.

But my question is how to identify which method causes blocking of main thread? is there any way to identify?

7
Run your processes on the main thread...IronManGill
There are lotz of process in my project all are running on background thread, like downloading image, animation of activity indicator, loading a tableview etc. which one should i run on main thread ?Krunal
Maybe describe more what you app is doing, show some code you are executing when you get freezeGrzegorz Krukowski
Well I think that is where the issue lies ... you are running too many processes simultaneously ... Try to start one thread when the previous one is finished....IronManGill
The app freezes when i navigates to multiple pages from one tab to another.Krunal

7 Answers

35
votes

Launch your app and wait for it to freeze. Then press the "pause" button in Xcode. The left pane should show you what method is currently running.

5
votes

Generally, it is highly recommended to perform on the main thread all animations method and interface manipulation, and to put in background tasks like download data from your server, etc...

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    //here everything you want to perform in background

    dispatch_async(dispatch_get_main_queue(), ^{ 
        //call back to main queue to update user interface
    });
});

Source : http://www.raywenderlich.com/31166/25-ios-app-performance-tips-tricks

3
votes

Set a break point from where the freeze occurs and find which line cause that.

Chances may be,Loading of large data,disable the controls,overload in main thread,Just find out where that occurs using breakpoints and rectify based on that.

0
votes

I reached an error similar to this, but it was for different reasons. I had a button that performed a segue to another ViewController that contained a TableView, but it looked like the application froze whenever the segue was performed.

My issue was that I was infinitely calling reloadData() due to a couple of didSet observers in one of my variables. Once I relocated this call elsewhere, the issue was fixed.

0
votes

Most Of the Time this happened to me when a design change is being called for INFINITE time. Which function can do that? well it is this one:

override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }

Solution is to add condition where the function inside of viewDidLayoutSubviews get calls only 1 time.

0
votes

I believe it should be possible to periodically check to see if the main thread is blocked or frozen. You could create an object to do this like so:

final class FreezeObserver {
    
    private let frequencySeconds: Double = 10
    private let acceptableFreezeLength: Double = 0.5
    
    func start() {
        DispatchQueue.global(qos: .background).async {
            
            let timer = Timer(timeInterval: self.frequencySeconds, repeats: true) { _ in
                
                var isFrozen = true
                
                DispatchQueue.main.async {
                    isFrozen = false
                }
                
                DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + self.acceptableFreezeLength) {
                    guard isFrozen  else { return }
                    
                    print("your app is frozen, so crash or whatever")
                }
            }
        
            let runLoop = RunLoop.current
            runLoop.add(timer, forMode: .default)
            runLoop.run()
        }
    }
}
0
votes

It could be that another view is not properly dismissed and it's blocking user interaction! Check the UI Debugger, and look at the top layer, to see if there is any strange thing there.