Frage

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?

War es hilfreich?

Lösung 2

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

Andere Tipps

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.

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.

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()
        }
    }
}

Update October 2021: Sentry now offers freeze observation, if you don't wanna roll this yourself.

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.

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.

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top