QUESTION OF THE WEEK
“
How to debug crashes in SwiftUI?
— Rajtharan Gopal
While this is a great question, it’s hard to answer completely. Each project is unique, and there are many different types of crashes. Therefore, I’d love to give a more generic answer to how I solve crashes.
Timeboxing is critical when solving crashes. You don’t want to lose yourself in a journey that doesn’t give you a quick solution. The first step would be to use AI or Google to find a fast way out. Yet, this is only possible if you clearly define the cause.
Ideally, you would be able to reproduce the crash quickly. I prefer writing a test for a crash so I can reproduce it consistently and promptly, but I also prevent the crash from returning unexpectedly in the future.
If you cannot find a quick way out, I recommend commenting out unrelated code and reproducing the crash with as minimal code as needed. This helps you to find the exact cause of the crash and likely enables you to define a better search query to find a solution.
At this point, it comes down to the type of crash. An EXC_BAD_ACCESS is common, and I wrote an article to help you solve it. Data races are also a common cause. Yet, SwiftUI-specific crashes might also relate to a particular OS version. In this case, you need to ensure you know the environment boundaries:
- Am I running a beta that causes this? If so, check the release notes and validate whether the crash still happens on a release candidate version.
- Is the crash consistently tight to a specific OS, country, app version, or anything else?
All these metrics combined help you reproduce the crash locally and solve it accordingly. Good luck; hopefully, these insights will help you solve crashes faster.
Want to have your question answered next week? Ask your question via this form (anonymously)