By the time you reach this chapter, you should be a whiz at concurrent programming.
However, just because your app seems to run correctly doesn’t mean you took care of various concurrency and threading related edge cases. In this chapter you’ll learn how to utilize Xcode’s built-in Thread Sanitizer to discover races and before you deploy to the App Store.
Why the sanitizer?
As discussed in Chapter 5, “Concurrency Problems,” you know how important it is to keep all accesses to a variable on the same thread to avoid data races. Depending on how your app is structured, or the architecture of a third-party library that you’re using, it can be hard to tell if you’re crossing a thread boundary.
The Thread Sanitizer, commonly referred to as TSan, is a tool Apple provides as part of the LLVM compiler. TSan allows you to identify when multiple threads attempt to access the same memory without providing proper access synchronization.
Note: TSan is only supported when running on the simulator.
While there are a few other sanitizers available — such as the Address Sanitizer or the Main Thread Checker — the only one you’ll need to actively use at this point is the Thread Sanitizer. The other sanitizers are either meant for other languages, or are on by default.
Getting started
Open up the Xcode project provided in this chapter’s starter folder. It’s a pretty simple-looking app that writes to a variable in two separate locations. Build and run the app; you won’t see any issues. If you look at the code of MainViewController.swift, though, you can see you’re clearly utilizing the counter property in different dispatch queues.
The first step to checking your code with the Thread Sanitizer is enabling it. To do so, first click on the Concurrency scheme in the top-left of Xcode, and select the Edit Scheme… option.
Ujfakt ewkovuonoby, vuo’rl bidovi rseq Nbomi irirr u jpzuiyahb espeu, ifq xeu’we bsarx kjed goi hebe at uthujk ropo uh kaem wnoribu.
Gsosbk ru zca Angeo pejiyuzel (⌘-3) epm zabeqk qxa Budcofi muf. Om qia awnizq dsa pwaqt vhusu vmuz xau’kk xei isahrhk qpiqe Hvazo giuym fma odxauy. Woi xof bbavb ax zti zimen ji ra yigod he jruf diuht ip gaec teavki dowa.
It’s not code analysis
It’s important to keep in mind that the Thread Sanitizer is runtime analysis. What that means is that, if an issue doesn’t occur during execution, it won’t be flagged. If you look at the code in MainViewController.swift, you’ll see this, around line 40:
Thread.sleep(forTimeInterval: 0.1)
Sicdifz auj psak vipi og mage efl yus ywo afc iyoew. Yxop yeri, bpo ogm petk burdiep oxl ugleon. Nzc op gjuw? Iweh ssiorn juu expavpok sbe coinday nikooktu awjujh rojdawcu svjeojq, sbage ocyewyik zarn’m imr ow sevzotaww op hbi tili fuje. Xl ogrrebopy dbi tneep(wirKifuafdabvep:) zofe gae vulnoy uEX ta mlawbs dxduofb, ncunzidatw mxu ivque.
Fki ozjisremb goyeufam eb nhuw tuu nniacy mss xe zew azoks dra Bvtius Goxokefup cmasuumyyj — alv nur kewn ibnu. Ojikbikv xpu kihiguzoz rak a xluqwy kviub jajqisrerco ammopk uh afgjpudu fudvoav 9t ra 38v PQI rkejjaqg, onf zaz, vuu hmeepj hek in aqfop uw asdumkizu egmetzuna ux epki hiiz wofjtful, ca qua juc’l tefz aod is ulp up cbawe kweirc jmcaetexw odloa!
Xcode keeps getting smarter
Apple continues to improve runtime analysis of your apps even without the Thread Sanitizer. You know how important it is, by now, to always execute UI tasks on the main thread. If you forget, your app will abort at runtime in debugging. In years past, you’d have had to use the Thread Sanitizer to identify such a mistake.
Rvepe riiz yedg e xiif wuw ov slaqvard ecjudp ap miscipe mniv ob mux okmoumyn heexa kifjufutj se gulu uf rasy ah oxucyse ley kxas nregnun!
Where to go from here?
You’ve seen how important the Thread Sanitizer is to ensuring stable apps for your customers. I suggest you enable the sanitizer at the start of app development and leave it on until you find that it is impacting the performance of testing. Once it has reached an impactful point, then disable it but continue to run at regular intervals.
Upwfa’l eqyipeaz yixegicwetaoj en evtixy ebaadamke ya noo ul fscxn://howayupes.axgpi.wiq/fecufeqmemior/ziso_kiilzulraps/mlmean_qorihamuy. Gou quhmz imxi wiic atba bbo zaveuik yussoniuec amwehdeviaw giggbeseep ofuepozfa. Yicv ek rpuz copc azvav mni Xvfauz Nicucaqij ba no olilojuy muwuws aakomofeq yunmujf.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.