<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mike Salari</title>
    <description>The latest articles on DEV Community by Mike Salari (@salari_dev).</description>
    <link>https://dev.to/salari_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4100087%2F0ab13e53-fc98-4f69-823a-51d3a60ad5a2.png</url>
      <title>DEV Community: Mike Salari</title>
      <link>https://dev.to/salari_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/salari_dev"/>
    <language>en</language>
    <item>
      <title>Image loading system design interview: a scrolling feed, answered from the hiring side</title>
      <dc:creator>Mike Salari</dc:creator>
      <pubDate>Tue, 17 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/salari_dev/image-loading-system-design-interview-a-scrolling-feed-answered-from-the-hiring-side-541c</link>
      <guid>https://dev.to/salari_dev/image-loading-system-design-interview-a-scrolling-feed-answered-from-the-hiring-side-541c</guid>
      <description>&lt;p&gt;Ask a mobile engineer to design image loading for a feed and the first answer is usually a library name and "a memory cache and a disk cache". Then I ask: your memory cache holds 100 images. How many bytes is that? Most candidates have never had to say.&lt;/p&gt;

&lt;p&gt;I have run 500+ technical interviews from the hiring side over 15 years in mobile. This prompt looks like the easy one in the set, and it is where answers that sound complete turn out to have no numbers in them. &lt;strong&gt;A limit of 100 images is not a budget. A hundred thumbnails and a hundred full-resolution photos are different apps.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a worked answer for iOS and Android together, because the decisions are the same and only the APIs differ. For the wider platform rounds, see &lt;a href="https://salari.dev/writing/ios-system-design-interview/" rel="noopener noreferrer"&gt;the iOS messaging answer&lt;/a&gt; and &lt;a href="https://salari.dev/writing/android-system-design-interview/" rel="noopener noreferrer"&gt;the Android field app answer&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The prompt: design the image loading for a social feed with avatars, photo thumbnails and a full-screen viewer, on iOS and Android.&lt;/li&gt;
&lt;li&gt;The shape: requirements per kind of image, decoded size, cache tiers and keys, byte budgets, one load per key, cancellation, cell reuse, prefetch, memory pressure, observability.&lt;/li&gt;
&lt;li&gt;The test underneath: can you put a number of bytes on every decision, and say who still wants the work you are doing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The 45-minute round, minute by minute
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Minutes&lt;/th&gt;
&lt;th&gt;What you do&lt;/th&gt;
&lt;th&gt;What the interviewer is checking&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0 to 5&lt;/td&gt;
&lt;td&gt;Clarify the kinds of image, scroll speed, device range&lt;/td&gt;
&lt;td&gt;Do you ask before you build?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5 to 12&lt;/td&gt;
&lt;td&gt;Requirements per kind of image, and the decoded size&lt;/td&gt;
&lt;td&gt;Do you know what an image costs in memory?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12 to 20&lt;/td&gt;
&lt;td&gt;Memory and disk tiers, keys, budgets in bytes&lt;/td&gt;
&lt;td&gt;Is the cache bounded by something real?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20 to 30&lt;/td&gt;
&lt;td&gt;One load per key, cancellation, cell reuse&lt;/td&gt;
&lt;td&gt;What happens during a fast scroll?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;30 to 37&lt;/td&gt;
&lt;td&gt;Prefetch and memory pressure&lt;/td&gt;
&lt;td&gt;What do you give back, and when?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;37 to 45&lt;/td&gt;
&lt;td&gt;Metrics per device class, rollout&lt;/td&gt;
&lt;td&gt;Have you run this in production?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Most candidates spend the first fifteen minutes on HTTP caching headers and the choice of library. Both matter, and both are decisions the round assumes you can make. The points are in the bytes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements: three kinds of image, three policies
&lt;/h2&gt;

&lt;p&gt;The question that earns the most in the first ten minutes: &lt;strong&gt;which images are in this product, and does each one deserve the same treatment?&lt;/strong&gt; A feed carries at least three, and one rule for all of them is the most common weak answer.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Image&lt;/th&gt;
&lt;th&gt;What the user expects&lt;/th&gt;
&lt;th&gt;Policy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Avatar&lt;/td&gt;
&lt;td&gt;Always there, tiny, the same few faces again and again&lt;/td&gt;
&lt;td&gt;Small target size, high memory hit rate, long disk lifetime&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Feed thumbnail&lt;/td&gt;
&lt;td&gt;Appears before the row settles during a scroll&lt;/td&gt;
&lt;td&gt;Decode at cell size off the main thread, cancel when the row leaves&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Full-screen photo&lt;/td&gt;
&lt;td&gt;Sharp after a tap, zoomable&lt;/td&gt;
&lt;td&gt;Load on demand at screen size, keep at most one or two in memory&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Then write down what you will not build: the upload path, video, the CDN itself. And say one sentence about the server: &lt;strong&gt;the cheapest decode is the one where the server already sent the size you need.&lt;/strong&gt; If the CDN can resize by URL parameter, the client asks for its display size and most of this design gets cheaper.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decoded size is the number that matters
&lt;/h2&gt;

&lt;p&gt;A JPEG is compressed on the wire and on disk. On screen it is a bitmap, and a bitmap costs width times height times bytes per pixel, usually four. The file size tells you nothing about the memory.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Image&lt;/th&gt;
&lt;th&gt;Pixels&lt;/th&gt;
&lt;th&gt;Decoded at 4 bytes per pixel&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Camera photo, decoded as is&lt;/td&gt;
&lt;td&gt;4000 by 3000&lt;/td&gt;
&lt;td&gt;48,000,000 bytes, about 48 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Same photo for a 120 point cell at 3x&lt;/td&gt;
&lt;td&gt;360 by 360&lt;/td&gt;
&lt;td&gt;518,400 bytes, about 0.5 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That is close to a hundred times the memory for the same row. So the pipeline decodes &lt;strong&gt;at display size&lt;/strong&gt;: on iOS with ImageIO thumbnails (the code is in &lt;a href="https://salari.dev/writing/why-ios-apps-feel-slow-even-when-the-api-is-fast/" rel="noopener noreferrer"&gt;why iOS apps feel slow&lt;/a&gt;), on Android with &lt;code&gt;ImageDecoder&lt;/code&gt; and a target size, or &lt;code&gt;inSampleSize&lt;/code&gt; on older paths. Coil and Glide do this for you when they can resolve the target size, so make sure they can: a view or composable with a known size, or an explicit size on the request.&lt;/p&gt;

&lt;p&gt;And decode off the main thread. On iOS a &lt;code&gt;UIImage&lt;/code&gt; made from data may defer its decode until the first draw, which lands on the main thread in the middle of a scroll. Decoding into the final bitmap on a background task moves that cost to where nobody is waiting for a frame.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory and disk: what each tier holds
&lt;/h2&gt;

&lt;p&gt;Two tiers, two different things stored. The &lt;strong&gt;memory tier&lt;/strong&gt; holds decoded bitmaps at display size, ready to draw. The &lt;strong&gt;disk tier&lt;/strong&gt; holds the downloaded bytes, compressed, so one file can be decoded at several sizes. Both sit behind the network, which is the slowest and most expensive tier.&lt;/p&gt;

&lt;p&gt;The keys follow from that. The disk key is the URL. The memory key is the URL plus the target pixel size, because a 360 pixel thumbnail and a 1170 pixel full-screen image of the same photo are different bitmaps. Key the memory tier by URL alone and the viewer shows a blurry thumbnail stretched to the screen, or the feed holds a full-screen bitmap in a small cell.&lt;/p&gt;

&lt;p&gt;One more decision that removes a whole class of bugs: &lt;strong&gt;treat image URLs as immutable.&lt;/strong&gt; A new avatar gets a new URL. Then a cached entry is never stale, only old, and invalidation becomes eviction. If the server cannot promise that, use the HTTP validators it sends, and say so.&lt;/p&gt;

&lt;h2&gt;
  
  
  Budgets in bytes, not in images
&lt;/h2&gt;

&lt;p&gt;Now the number. The memory tier gets a budget in bytes, and the budget depends on the device, because a phone with 2 GB of RAM and a flagship with 12 GB cannot afford the same cache. On Android:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DecodedImageCache&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// A budget in bytes, scaled to the device class. A starting point you measure, not a constant.&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;maxBytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;am&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSystemService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ActivityManager&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;classBytes&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;memoryClass&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;am&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLowRamDevice&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;classBytes&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;classBytes&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;cache&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;object&lt;/span&gt; &lt;span class="err"&gt;: &lt;/span&gt;&lt;span class="nc"&gt;LruCache&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Bitmap&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;maxBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Count what a bitmap costs, not that it exists.&lt;/span&gt;
        &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sizeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bitmap&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;allocationByteCount&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Bitmap&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bitmap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Called from Application.onTrimMemory.&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nc"&gt;ComponentCallbacks2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TRIM_MEMORY_BACKGROUND&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;evictAll&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;            &lt;span class="c1"&gt;// rebuild from disk later&lt;/span&gt;
            &lt;span class="n"&gt;level&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="nc"&gt;ComponentCallbacks2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TRIM_MEMORY_UI_HIDDEN&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trimToSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxBytes&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// the user may come back&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three decisions are in there. &lt;code&gt;sizeOf&lt;/code&gt; counts bytes, so the cache holds many thumbnails or a few large images, whatever fits. The budget scales with the device and is smaller on low-RAM devices. And the cache gives memory back when the app leaves the screen. The other trim levels are deprecated and current Android versions no longer deliver them to apps, so the snippet uses only &lt;code&gt;TRIM_MEMORY_UI_HIDDEN&lt;/code&gt; and &lt;code&gt;TRIM_MEMORY_BACKGROUND&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On iOS, the memory tier is usually &lt;code&gt;NSCache&lt;/code&gt; with &lt;code&gt;totalCostLimit&lt;/code&gt; in bytes and each image's cost set to its decoded size, &lt;code&gt;bytesPerRow&lt;/code&gt; times &lt;code&gt;height&lt;/code&gt;. Its limit is not strict, and it evicts on its own under memory pressure, which is what a memory tier should do. The disk tier gets its own budget in bytes, with eviction by last access, and a cap the user would accept on a 64 GB phone.&lt;/p&gt;

&lt;p&gt;A senior candidate says the budget is a starting point. A staff candidate says how it will be checked: memory high-water mark per device class, measured on the cheapest device the product supports.&lt;/p&gt;

&lt;h2&gt;
  
  
  One load per image, however many cells ask
&lt;/h2&gt;

&lt;p&gt;Ten cells show the same avatar. If each one starts a download and a decode, you pay ten times for one image. The pipeline keeps a map of work in flight, keyed like the memory tier, and every caller for the same key joins the same task.&lt;/p&gt;

&lt;p&gt;The Swift version of this has a known trap: an actor alone does not prevent the duplicate, because it is reentrant across the &lt;code&gt;await&lt;/code&gt;. That follow-up is worked in &lt;a href="https://salari.dev/writing/senior-ios-interview-follow-up-questions/" rel="noopener noreferrer"&gt;the senior iOS follow-up questions&lt;/a&gt;. Here the interesting part is the next one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cancel the interest, not the download
&lt;/h2&gt;

&lt;p&gt;The user flings past 200 rows in a second. Every row that appeared asked for an image, and almost none of them will be on screen when it arrives. Without cancellation, the network and the decoder work through a queue of images nobody will see, and the rows the user stopped on wait behind them.&lt;/p&gt;

&lt;p&gt;So a cell cancels its request when it is reused. On iOS that is &lt;code&gt;prepareForReuse&lt;/code&gt; cancelling the cell's task; on Android it is the request bound to the view or composable lifecycle, which Coil and Glide do for you. But deduplication makes this harder: &lt;strong&gt;if three cells share one load and one of them is reused, cancelling the load breaks the other two.&lt;/strong&gt; The load must stop only when the last interested cell leaves.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;ImageKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Hashable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Sendable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;URL&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;maxPixelSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;   &lt;span class="c1"&gt;// the display size is part of the key&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;actor&lt;/span&gt; &lt;span class="kt"&gt;ImageLoader&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;Entry&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;task&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;CGImage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;waiters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;UUID&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;inFlight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;ImageKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Entry&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[:]&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;load&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@Sendable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;ImageKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;CGImage&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;load&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@escaping&lt;/span&gt; &lt;span class="kd"&gt;@Sendable&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;ImageKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;CGImage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;load&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;image&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nv"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ImageKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;CGImage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;leave&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;withTaskCancellationHandler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nv"&gt;onCancel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;Task&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;leave&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;// this cell stopped caring&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ImageKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;CGImage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inFlight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;waiters&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;inFlight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;load&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;inFlight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;task&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;waiters&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;leave&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ImageKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inFlight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;waiters&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;waiters&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;inFlight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;
            &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   &lt;span class="c1"&gt;// nobody on screen wants it any more&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;inFlight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every caller registers an id. Cancelling a caller removes its id; the shared task is cancelled only when no id is left. A cancelled cell may still wait for a result it no longer needs, which is fine, because of the next rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cell reuse: the wrong image in the right cell
&lt;/h2&gt;

&lt;p&gt;A slow load for row 12 completes after the cell has been reused for row 40. If the completion writes into the cell, the user sees someone else's photo for a moment, or for good. Cancellation alone does not prevent this, because a result can arrive in the instant between the reuse and the cancel.&lt;/p&gt;

&lt;p&gt;The rule: &lt;strong&gt;the cell checks that the result is still for the key it is showing before it draws.&lt;/strong&gt; Store the current key on the cell, compare on arrival, drop the result if they differ. The memory tier still keeps the image, so the work is not wasted if row 12 comes back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prefetch: a bet you must be able to call off
&lt;/h2&gt;

&lt;p&gt;Prefetch starts loads for rows just below the screen so they arrive before the user does. On iOS, &lt;code&gt;UICollectionViewDataSourcePrefetching&lt;/code&gt; tells you which index paths are coming and which ones to cancel. On Android, RecyclerView and LazyColumn prefetch item views ahead, and the image loaders offer a preload call you trigger from the list position.&lt;/p&gt;

&lt;p&gt;Prefetch is a bet, and a bad bet costs data, battery and memory. So it runs at lower priority than visible images, it covers a distance you measured rather than guessed, and it is cancelled when the user changes direction. The number to watch is wasted prefetch: images loaded ahead that were never displayed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Memory pressure: shed the tier you can rebuild
&lt;/h2&gt;

&lt;p&gt;When the system asks for memory, give back what is cheap to recreate. Decoded bitmaps are the first to go, because the disk tier can rebuild them. Bytes on disk stay. Work in flight for rows the user is looking at stays.&lt;/p&gt;

&lt;p&gt;On iOS, listen for the memory warning and clear the memory tier, and keep the footprint small in the background, because a large backgrounded app is a likely candidate for termination. On Android, &lt;code&gt;onTrimMemory&lt;/code&gt; as in the snippet, and remember that bitmap pixels have lived in native memory since Android 8, so a Java heap that looks flat does not mean the images are under control.&lt;/p&gt;

&lt;p&gt;Say what you would refuse, too: &lt;code&gt;android:largeHeap&lt;/code&gt; as the fix for an image OOM. It moves the limit without bounding the growth.&lt;/p&gt;

&lt;h2&gt;
  
  
  Observability and rollout
&lt;/h2&gt;

&lt;p&gt;Name the numbers, each split by device class and app version: memory tier hit rate and disk tier hit rate; time from a row becoming visible to its image appearing, at p50 and p95; decode time off the main thread; bytes downloaded per session; cancelled loads and wasted prefetch as shares of all loads; memory high-water mark. Then the one that matters most: terminations for memory, from MetricKit's exit metrics on iOS and &lt;code&gt;ApplicationExitInfo&lt;/code&gt; on Android, because your crash reporter never sees them.&lt;/p&gt;

&lt;p&gt;Roll a new decode size or budget out behind a remote flag, cheapest devices first, and compare memory terminations against the old path before widening it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mid, senior and staff: what the answers sound like
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Topic&lt;/th&gt;
&lt;th&gt;Mid-level&lt;/th&gt;
&lt;th&gt;Senior&lt;/th&gt;
&lt;th&gt;Staff&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Size&lt;/td&gt;
&lt;td&gt;"Load the image in the background."&lt;/td&gt;
&lt;td&gt;Decode at display size; key memory by URL and size.&lt;/td&gt;
&lt;td&gt;Asks the server for the display size, and measures decoded bytes.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Budget&lt;/td&gt;
&lt;td&gt;"A cache of 100 images."&lt;/td&gt;
&lt;td&gt;Budgets in bytes, scaled to the device.&lt;/td&gt;
&lt;td&gt;Names the memory owner and the high-water mark per device class.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scroll&lt;/td&gt;
&lt;td&gt;"Cancel on reuse."&lt;/td&gt;
&lt;td&gt;One load per key, cancelled when the last cell leaves; result checked against the key.&lt;/td&gt;
&lt;td&gt;Wasted prefetch and cancelled loads as metrics with a target.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pressure&lt;/td&gt;
&lt;td&gt;"Clear the cache when memory is low."&lt;/td&gt;
&lt;td&gt;Drop decoded bitmaps, keep disk, keep visible work.&lt;/td&gt;
&lt;td&gt;Tracks memory terminations by device class and gates rollout on them.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The staff column is not more components. It is the same pipeline with numbers attached, and a way to know when one of them moved.&lt;/p&gt;

&lt;h2&gt;
  
  
  The follow-ups interviewers use to push
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"How much memory does your cache use?" Tests byte budgets and decoded size.&lt;/li&gt;
&lt;li&gt;"The server starts sending 4000 pixel originals." Tests decode at display size and the server contract.&lt;/li&gt;
&lt;li&gt;"Ten cells show the same avatar." Tests one load per key.&lt;/li&gt;
&lt;li&gt;"The user flings through 500 rows." Tests cancellation that respects shared loads.&lt;/li&gt;
&lt;li&gt;"A row shows someone else's photo for a moment." Tests the key check on arrival.&lt;/li&gt;
&lt;li&gt;"It crashes only on 2 GB phones." Tests budgets per device class, memory pressure and how you would see it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prepare one sentence for each. If a follow-up makes you add a box, your design was missing a decision. If it makes you point at a number you already said, you are having the conversation the round is for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Questions engineers ask about image loading interviews
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you answer "design an image loading pipeline" in a mobile system design interview?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Start from the three kinds of image in the product and the policy each one needs. Then decode at the size the image is displayed, keep the memory tier in decoded bytes and the disk tier in downloaded bytes, set both budgets in bytes per device class, share one load among every cell that wants the same image, cancel it when the last one leaves, and say what you drop first when the system asks for memory back.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Should I just say I would use Coil, Glide, Kingfisher or Nuke?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Name the library you would ship, then design as if you had to build it. The interviewer is scoring whether you know what the library decides for you: the decode size, the cache keys, the budgets, the cancellation and the eviction under pressure. A library with the wrong target size still decodes the wrong number of pixels.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is NSCache an LRU cache?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Do not promise that. Apple documents NSCache as evicting objects automatically when memory is tight and treats totalCostLimit as a limit it may not enforce strictly. It is a good memory tier precisely because it gives memory back under pressure. If you need a strict order of eviction, you build it yourself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How should I practise this round?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Take one feed with avatars, thumbnails and a full-screen viewer, answer out loud against a 45-minute timer, then change one fact: the device has 2 GB of RAM, the user flings through 500 rows, or the server starts sending 4000-pixel originals. Answer again. The second pass shows which parts of your design were budgets and which were guesses.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;First published on &lt;a href="https://salari.dev/writing/image-loading-pipeline-system-design-interview/?utm_source=devto&amp;amp;utm_medium=syndication&amp;amp;utm_campaign=image-loading-pipeline-system-design-interview" rel="noopener noreferrer"&gt;salari.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>mobile</category>
      <category>ios</category>
      <category>android</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>15 React Native Interview Questions From Junior to Staff, With the Follow-Ups That Matter</title>
      <dc:creator>Mike Salari</dc:creator>
      <pubDate>Thu, 12 Nov 2026 13:37:05 +0000</pubDate>
      <link>https://dev.to/salari_dev/15-react-native-interview-questions-from-junior-to-staff-with-the-follow-ups-that-matter-2pok</link>
      <guid>https://dev.to/salari_dev/15-react-native-interview-questions-from-junior-to-staff-with-the-follow-ups-that-matter-2pok</guid>
      <description>&lt;p&gt;A React Native interview can begin with a familiar question about components or state and end with a discussion about native threads, memory, release risk or architecture.&lt;/p&gt;

&lt;p&gt;That progression matters.&lt;/p&gt;

&lt;p&gt;A junior engineer might need to show that they understand how to build a reliable screen. A senior engineer should also be able to explain what breaks in production and how to investigate it. At staff level, the discussion increasingly concerns platform constraints, ownership and decisions that affect multiple teams.&lt;/p&gt;

&lt;p&gt;These 15 questions are designed as a practice exercise. The sample answers are deliberately short. For each question, the more useful exercise is to answer the follow-up yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Junior: Get the fundamentals right
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. What's the difference between props and state?
&lt;/h3&gt;

&lt;p&gt;Props are inputs provided to a component. State represents information the component or its owner manages over time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; If two screens need to edit the same value, where should it live, and why?&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Why do lists need stable keys?
&lt;/h3&gt;

&lt;p&gt;React uses keys to associate elements across renders. Unstable keys can cause incorrect identity reuse and unexpected component state behaviour.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; What can go wrong if you use the array index as a key in a list where users insert and delete items?&lt;/p&gt;

&lt;h3&gt;
  
  
  3. When would you use &lt;code&gt;FlatList&lt;/code&gt; instead of &lt;code&gt;ScrollView&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;For a potentially large collection, &lt;code&gt;FlatList&lt;/code&gt; provides windowed rendering instead of eagerly rendering the entire list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; Why might a &lt;code&gt;FlatList&lt;/code&gt; still appear blank during fast scrolling?&lt;/p&gt;

&lt;h3&gt;
  
  
  4. What happens when a component unmounts?
&lt;/h3&gt;

&lt;p&gt;React runs the relevant effect cleanup. Any work that outlives the component may need explicit cancellation, unsubscription or protection against publishing stale results.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; Does removing a component from the screen automatically cancel a network request started by your own service?&lt;/p&gt;

&lt;h3&gt;
  
  
  5. How do you handle an API request failure?
&lt;/h3&gt;

&lt;p&gt;Represent loading, success and failure distinctly. Give the user an appropriate recovery path.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; How does your behaviour change when the request creates or modifies something rather than simply fetching data?&lt;/p&gt;

&lt;h2&gt;
  
  
  Mid-level: Explain the failure modes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  6. Why does a component keep re-rendering?
&lt;/h3&gt;

&lt;p&gt;State changes, context updates, changing props and parent renders may all contribute. Re-rendering is not automatically a performance defect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; How would you establish whether the re-render is expensive enough to matter?&lt;/p&gt;

&lt;h3&gt;
  
  
  7. How would you optimise a slow &lt;code&gt;FlatList&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;First reproduce and measure the problem. Then investigate row complexity, image work, unstable props, unnecessary renders and list configuration.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getItemLayout&lt;/code&gt; can help when item dimensions are predictable. It is not a universal fix for dynamic-height rows.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; Which optimisation would you reject if it improved scrolling but caused missing content?&lt;/p&gt;

&lt;h3&gt;
  
  
  8. How does your application handle foreground and background transitions?
&lt;/h3&gt;

&lt;p&gt;React Native provides &lt;code&gt;AppState&lt;/code&gt; for observing application states, but backgrounding is not the same as process termination.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; What happens to an unsaved form if the operating system later kills the process?&lt;/p&gt;

&lt;h3&gt;
  
  
  9. When would you write a native module?
&lt;/h3&gt;

&lt;p&gt;When a requirement needs platform functionality or characteristics that cannot reasonably be achieved through the available JavaScript-facing APIs and libraries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; How would you test the boundary between JavaScript and native code, including error handling and lifecycle behaviour?&lt;/p&gt;

&lt;h3&gt;
  
  
  10. How would you debug a crash that only happens in release builds?
&lt;/h3&gt;

&lt;p&gt;Start with a reproducible release configuration, symbolicated native crash reports where available, JavaScript error evidence and the differences between development and production behaviour.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; How would you distinguish a JavaScript failure from a native crash, and what evidence would you collect before changing code?&lt;/p&gt;

&lt;h2&gt;
  
  
  Senior: Defend the trade-offs
&lt;/h2&gt;

&lt;h3&gt;
  
  
  11. How would you design reliable offline writes?
&lt;/h3&gt;

&lt;p&gt;I would expect a durable representation of pending work, explicit retry and conflict handling, and a way to establish whether the server accepted an operation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; Your user presses Pay, loses connectivity and retries. How do you avoid creating two payments?&lt;/p&gt;

&lt;h3&gt;
  
  
  12. How would you investigate intermittent screen freezes?
&lt;/h3&gt;

&lt;p&gt;Separate JavaScript work, React rendering, native UI work, expensive synchronisation and I/O. Build a timeline rather than assuming that every freeze comes from the JavaScript thread.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; What instrumentation would let you connect a user-visible hitch to the work causing it?&lt;/p&gt;

&lt;h3&gt;
  
  
  13. How would you migrate an established application to the New Architecture?
&lt;/h3&gt;

&lt;p&gt;Start with dependency compatibility, platform support, a controlled rollout and measurements that matter to the application.&lt;/p&gt;

&lt;p&gt;The fact that an architecture is newer is not evidence that every app will become faster.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; A critical third-party dependency is incompatible. Do you replace it, isolate it, defer the migration or accept the cost? What drives the decision?&lt;/p&gt;

&lt;h2&gt;
  
  
  Staff: Think beyond one application feature
&lt;/h2&gt;

&lt;h3&gt;
  
  
  14. Multiple teams share one mobile foundation. Who owns breaking changes?
&lt;/h3&gt;

&lt;p&gt;A platform boundary needs documented contracts, compatibility expectations, versioning and a process for changes that affect downstream teams.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; A feature team needs an urgent exception to a platform rule. Who can approve it, and how do you keep the exception from becoming permanent architecture?&lt;/p&gt;

&lt;h3&gt;
  
  
  15. Your React Native app has performance problems across several product teams. What do you do first?
&lt;/h3&gt;

&lt;p&gt;I wouldn't begin with a rewrite.&lt;/p&gt;

&lt;p&gt;I would define the user-visible failures, establish comparable measurements, identify the most expensive recurring causes and determine which changes require platform ownership rather than local fixes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Follow-up:&lt;/strong&gt; How would you prove that a shared optimisation improved the product without simply moving the cost elsewhere?&lt;/p&gt;

&lt;h2&gt;
  
  
  How to practise these questions
&lt;/h2&gt;

&lt;p&gt;For a junior round, make sure you can explain the concept and demonstrate a working example.&lt;/p&gt;

&lt;p&gt;For a mid-level round, add a production failure and how you would test your response.&lt;/p&gt;

&lt;p&gt;For a senior round, be ready to explain your chosen trade-off, a rejected alternative, the evidence you would collect and how you would recover.&lt;/p&gt;

&lt;p&gt;For a staff-level conversation, expect the interviewer to change the scope. The decision may now affect multiple teams, multiple releases or users on several app versions.&lt;/p&gt;

&lt;p&gt;And don't invent production stories to make your answer sound senior.&lt;/p&gt;

&lt;p&gt;If you have not worked on a system of that scale, explain how you would approach it and which assumptions you would need to verify.&lt;/p&gt;

&lt;p&gt;That's stronger than pretending.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further preparation
&lt;/h2&gt;

&lt;p&gt;I wrote &lt;strong&gt;Top 300 React Native Interview Questions&lt;/strong&gt; to help engineers practise these increasingly difficult conversations, including production depth, common pitfalls and what each question is intended to evaluate.&lt;/p&gt;

&lt;p&gt;The free sample explains the answer structure and how expectations change from junior through staff.&lt;/p&gt;

&lt;p&gt;The point of practising isn't to memorise 300 definitions.&lt;/p&gt;

&lt;p&gt;It's to become comfortable when the interviewer asks the question that follows your first answer.&lt;/p&gt;

&lt;p&gt;These 15 questions are a starting point.&lt;/p&gt;

&lt;p&gt;I wrote &lt;em&gt;Top 300 React Native Interview Questions&lt;/em&gt; for engineers who want to go beyond definitions and prepare for the follow-ups that reveal their actual level.&lt;/p&gt;

&lt;p&gt;It covers junior foundations through staff-level architecture, reliability and platform decisions, with production depth, common pitfalls and what the interviewer is testing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://salari.dev/books/top-react-native-interview-questions/" rel="noopener noreferrer"&gt;Explore the book and read a free sample&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>interview</category>
      <category>mobile</category>
      <category>rn</category>
    </item>
    <item>
      <title>Offline sync system design interview: what happens when two devices disagree</title>
      <dc:creator>Mike Salari</dc:creator>
      <pubDate>Tue, 10 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/salari_dev/offline-sync-system-design-interview-what-happens-when-two-devices-disagree-j33</link>
      <guid>https://dev.to/salari_dev/offline-sync-system-design-interview-what-happens-when-two-devices-disagree-j33</guid>
      <description>&lt;p&gt;Ask a room of mobile engineers to design offline sync and most of them reach for a library and a merge strategy within five minutes. Then I ask: the user edits the same note on a phone in the metro and on a laptop at home, and both sync an hour later. Which edit survives, and who decided that?&lt;/p&gt;

&lt;p&gt;I have run 500+ technical interviews from the hiring side over 15 years in mobile. Offline sync is the prompt where "we use last write wins" sounds like an answer and is really the absence of one. &lt;strong&gt;Last write wins is a conflict policy too. It just lets a clock decide.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is a worked answer, platform-neutral, because nothing in it depends on iOS or Android. For the platform rounds, see &lt;a href="https://salari.dev/writing/ios-system-design-interview/" rel="noopener noreferrer"&gt;the iOS messaging answer&lt;/a&gt; and &lt;a href="https://salari.dev/writing/android-system-design-interview/" rel="noopener noreferrer"&gt;the Android field app answer&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The prompt: design offline sync for a notes and checklists app, with several devices per user and shared lists.&lt;/li&gt;
&lt;li&gt;The shape: requirements, conflict cost per field, what you sync, cursors, detection versus resolution, deletes, resync, old clients, observability.&lt;/li&gt;
&lt;li&gt;The test underneath: can you say what a conflict costs, and who is allowed to settle it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The 45-minute round, minute by minute
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Minutes&lt;/th&gt;
&lt;th&gt;What you do&lt;/th&gt;
&lt;th&gt;What the interviewer is checking&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;0 to 5&lt;/td&gt;
&lt;td&gt;Clarify devices per user, sharing, offline duration&lt;/td&gt;
&lt;td&gt;Do you ask before you build?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5 to 12&lt;/td&gt;
&lt;td&gt;Requirements, and the cost of a conflict per field&lt;/td&gt;
&lt;td&gt;Do you know the data matters differently?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12 to 20&lt;/td&gt;
&lt;td&gt;What you sync, and the cursor&lt;/td&gt;
&lt;td&gt;Where is data silently lost?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;20 to 30&lt;/td&gt;
&lt;td&gt;Detection, resolution, who decides&lt;/td&gt;
&lt;td&gt;Is the policy a decision or a default?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;30 to 37&lt;/td&gt;
&lt;td&gt;Deletes, compaction, full resync&lt;/td&gt;
&lt;td&gt;What happens after a month offline?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;37 to 45&lt;/td&gt;
&lt;td&gt;Old app versions, metrics, rollout&lt;/td&gt;
&lt;td&gt;Have you run this in production?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Most candidates spend the first 20 minutes on the transport, WebSocket or polling. That choice can change three times in an app's life. The conflict policy is what the round is about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements: the cost of a conflict, field by field
&lt;/h2&gt;

&lt;p&gt;The question that earns the most points in the first ten minutes: &lt;strong&gt;what does it cost if two edits to this field collide?&lt;/strong&gt; Answer it per field, because one sync engine carries data with very different stakes.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Data&lt;/th&gt;
&lt;th&gt;Cost of losing one side of a conflict&lt;/th&gt;
&lt;th&gt;Policy&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Note body text&lt;/td&gt;
&lt;td&gt;Annoying: a paragraph needs retyping&lt;/td&gt;
&lt;td&gt;Last write wins, ordered by server version&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Checklist item done or not&lt;/td&gt;
&lt;td&gt;Small, and easy to see&lt;/td&gt;
&lt;td&gt;Last write wins per item, not per list&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared list membership&lt;/td&gt;
&lt;td&gt;Someone loses access, or keeps it&lt;/td&gt;
&lt;td&gt;Server decides; the device only proposes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Anything with money or safety in it&lt;/td&gt;
&lt;td&gt;Real harm&lt;/td&gt;
&lt;td&gt;Never automatic: keep both, ask a person&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Write down what you will not build: the editor, real-time co-editing of one paragraph, the search index. Then say the per-field idea out loud. &lt;strong&gt;Consistency is a decision per invariant, not a database setting.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What you sync: snapshots, changes or operations
&lt;/h2&gt;

&lt;p&gt;Three shapes, three different promises. A &lt;strong&gt;snapshot&lt;/strong&gt; (the whole note as of version 47) is simple and turns every concurrent edit into a total conflict, because the server receives two full candidates and can only pick one. A &lt;strong&gt;change&lt;/strong&gt; (this field became that) is smaller, but a naked change applied twice by a retry corrupts the record, so it needs an idempotency key. An &lt;strong&gt;operation log&lt;/strong&gt; (who did what, in which order) keeps the intent a merge needs, and costs you compaction and replay.&lt;/p&gt;

&lt;p&gt;A good answer mixes them on purpose: operations for the fields where the order of edits matters, snapshots with last write wins for the fields where a lost edit is acceptable. One engine, two policies, chosen by what a conflict costs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cursors: move them only when the data is stored
&lt;/h2&gt;

&lt;p&gt;A device asks "what changed since I last looked?". The answer is a cursor, and cursors are where correct-looking sync quietly loses data. Use the server's version, which only goes up, never a timestamp from a device clock.&lt;/p&gt;

&lt;p&gt;The rule that separates levels: &lt;strong&gt;store the records and move the cursor in one local transaction.&lt;/strong&gt; Move the cursor when the response arrives, and an app killed before the write believes it is up to date while it is missing changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ChangePage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nv"&gt;db&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;LocalDatabase&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;change&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;changes&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upsert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;change&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;         &lt;span class="c1"&gt;// the records&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="n"&gt;tx&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setCursor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;// and the cursor, in the same transaction&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Say the invariant so a test can check it: after any interruption, the stored cursor never points past a change the local database does not have.&lt;/p&gt;

&lt;h2&gt;
  
  
  Detection and resolution are different jobs
&lt;/h2&gt;

&lt;p&gt;This is the structural move interviewers wait for. &lt;strong&gt;Detection&lt;/strong&gt; is mechanical and knows nothing about notes: every edit carries the server version it started from, and if the record moved on since then, two edits diverged. &lt;strong&gt;Resolution&lt;/strong&gt; is policy: what to do about it, decided by the people who know what a conflict costs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt;
&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Accepted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;newVersion&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt;
&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Conflict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;current&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Edit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt;

&lt;span class="c1"&gt;// Detection, on the server. It knows nothing about what the record means.&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;detect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Edit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;baseVersion&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Accepted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;Conflict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Resolution, per field class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Resolution&lt;/span&gt;
&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Winner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Resolution&lt;/span&gt;
&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;NeedsReview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Resolution&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;FieldClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Record&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Edit&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Resolution&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// convergence is enough: the server's order decides, never a device clock&lt;/span&gt;
        &lt;span class="nc"&gt;FieldClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FREE_TEXT&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Winner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;// never automatic: keep both values, a person chooses&lt;/span&gt;
        &lt;span class="nc"&gt;FieldClass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CRITICAL&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;NeedsReview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;incoming&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why &lt;code&gt;Winner(incoming.value)&lt;/code&gt; for free text: the server applies edits in the order it accepts them, so the later accepted edit wins by the server's sequence. What must never decide is the device clock. A phone whose clock runs ninety seconds fast would let an old edit beat a newer correction.&lt;/p&gt;

&lt;h2&gt;
  
  
  The guarantees, in the user's words
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Read your own writes:&lt;/strong&gt; the device that made an edit shows it at once, and never flickers back to the old value while it syncs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never go backwards:&lt;/strong&gt; once a device has shown version 48, a late response carrying 47 must not replace it. The cursor rule gives you this, as long as no code path writes records without checking versions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Converge:&lt;/strong&gt; with no new edits, all devices reach the same value in bounded time. Last write wins gives you this, and nothing more.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A senior candidate states these three without naming a library. A staff candidate adds a number: how quickly a second device must reflect an edit while it is in the foreground.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deletes: the edit that comes back from the dead
&lt;/h2&gt;

&lt;p&gt;The user deletes a list on the phone. The delete syncs as a &lt;strong&gt;tombstone&lt;/strong&gt;, a record that says "this is gone, as of version 49". A tablet that was offline for a month comes back holding version 47. If the server already threw the tombstone away to save space, the sync sees "the tablet has a list the server does not know" and helpfully uploads it again. The deleted list is back.&lt;/p&gt;

&lt;p&gt;So tombstone lifetime is a correctness setting, not a storage one. It must outlast your longest realistic offline window, measured, not guessed. The same goes for compacting the operation log: you cannot throw away operations an old cursor still needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Full resync: the last resort, never the first
&lt;/h2&gt;

&lt;p&gt;Past the compaction horizon, a returning device cannot catch up by replay. It has to discard its state and download a fresh copy. That is correct, expensive and visible to the user, so treat it as a metric, not a quiet fallback.&lt;/p&gt;

&lt;p&gt;And one rule above all: &lt;strong&gt;send the device's pending edits before you replace its data.&lt;/strong&gt; A full resync that throws away an edit the user made offline turns a slow sync into data loss. Pending edits go up first, conflicts get resolved by the policy above, and only then does the fresh copy come down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Old app versions and the migration
&lt;/h2&gt;

&lt;p&gt;You will not change the sync model on every device at once. Some users update next month. During the switch, the server accepts the old shape of writes from old versions and converts them into the new model, so its history stays coherent. That converter is temporary: give it an owner and a removal condition tied to how many old installs remain.&lt;/p&gt;

&lt;p&gt;This is version skew, and &lt;a href="https://salari.dev/books/controlled-change/" rel="noopener noreferrer"&gt;Controlled Change&lt;/a&gt; gives it a chapter, next to the ones on building a sync engine and on conflicts, idempotency and reconciliation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Observability and rollout
&lt;/h2&gt;

&lt;p&gt;Name the numbers: conflicts per thousand edits, by field class; time for a second device to reflect an edit, at p50 and p99; full resyncs per thousand sessions; how long conflicts wait for a person to decide. Never log the contents of a conflict, only that it happened and for which field class.&lt;/p&gt;

&lt;p&gt;Roll the new sync model out behind a remote flag, to internal users first, then a small share, and keep the old path readable so you can turn the flag off without losing anyone's data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mid, senior and staff: what the answers sound like
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Topic&lt;/th&gt;
&lt;th&gt;Mid-level&lt;/th&gt;
&lt;th&gt;Senior&lt;/th&gt;
&lt;th&gt;Staff&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Conflicts&lt;/td&gt;
&lt;td&gt;"Last write wins."&lt;/td&gt;
&lt;td&gt;Policy per field; server order, never device clocks.&lt;/td&gt;
&lt;td&gt;Names who resolves which field class, and the metric that shows conflicts.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cursor&lt;/td&gt;
&lt;td&gt;"Fetch changes since last sync."&lt;/td&gt;
&lt;td&gt;Server versions; cursor moves in the same transaction as the data.&lt;/td&gt;
&lt;td&gt;States the invariant as a test and alarms on violations.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deletes&lt;/td&gt;
&lt;td&gt;"Delete it on the server."&lt;/td&gt;
&lt;td&gt;Tombstones that outlast the offline window.&lt;/td&gt;
&lt;td&gt;Measures the offline window and owns the tombstone lifetime.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Resync&lt;/td&gt;
&lt;td&gt;"Just resync everything."&lt;/td&gt;
&lt;td&gt;Last resort, after sending pending edits.&lt;/td&gt;
&lt;td&gt;A metric with a budget, and a plan for old app versions.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The staff column is not more components. It is the same design with owners, budgets and a way to know when it is wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  The follow-ups interviewers use to push
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;"Both devices edited the same note offline. Which wins?" Tests policy per field, and server order.&lt;/li&gt;
&lt;li&gt;"The phone's clock is wrong." Tests whether a device clock ever decides.&lt;/li&gt;
&lt;li&gt;"The app is killed right after the sync response arrives." Tests the cursor transaction.&lt;/li&gt;
&lt;li&gt;"A tablet comes back after a month." Tests tombstones and compaction.&lt;/li&gt;
&lt;li&gt;"The server is too far ahead to replay." Tests resync that keeps pending edits.&lt;/li&gt;
&lt;li&gt;"Half the users are still on last year's version." Tests the migration path.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Prepare one sentence for each. If a follow-up makes you add a box, your design was missing a decision. If it makes you point at a box that already handles it, you are having the conversation the round is for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Questions engineers ask about offline sync interviews
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;How do you answer "design offline sync" in a mobile system design interview?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Start with what a conflict costs for each kind of data, and who is allowed to resolve it. Then separate conflict detection from conflict resolution, use server versions as the cursor, move the cursor only when the data is stored, keep deletes as tombstones long enough for your longest offline device, and make a full resync the last resort, never the first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is last write wins wrong?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not always. It converges, and for free text where a lost edit is an annoyance it is often the right choice. It is wrong when the data is important enough that someone must decide, and it is always wrong when the device clock decides which write is last.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Should I use CRDTs in the interview?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Name them as an option and say what they cost. They merge automatically, which is exactly what you do not want for a field where a human must choose between two values. Interviewers score the reason for the choice, not the acronym.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How should I practise this round?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Take one app with two devices per user, answer out loud against a 45-minute timer, then change one fact: one device was offline for a month, or the server compacted its history. Answer again. The second pass shows which parts of the design were decisions.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;First published on &lt;a href="https://salari.dev/writing/offline-sync-conflicts-system-design-interview/?utm_source=devto&amp;amp;utm_medium=syndication&amp;amp;utm_campaign=offline-sync-conflicts-system-design-interview" rel="noopener noreferrer"&gt;salari.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>mobile</category>
      <category>ios</category>
      <category>android</category>
    </item>
    <item>
      <title>Kotlin coroutines interview questions that senior Android loops ask</title>
      <dc:creator>Mike Salari</dc:creator>
      <pubDate>Tue, 03 Nov 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/salari_dev/kotlin-coroutines-interview-questions-that-senior-android-loops-ask-1kb3</link>
      <guid>https://dev.to/salari_dev/kotlin-coroutines-interview-questions-that-senior-android-loops-ask-1kb3</guid>
      <description>&lt;p&gt;Every Android candidate I interview says they use coroutines. Most of them do. Fewer can tell me what happens to a child coroutine when its sibling throws, or why a test with &lt;code&gt;delay(5_000)&lt;/code&gt; finishes in milliseconds.&lt;/p&gt;

&lt;p&gt;After 15 years in mobile and 500+ technical interviews from the hiring side, coroutines is the topic where the gap between mid-level and senior shows fastest. The API is small. The rules behind it are not.&lt;/p&gt;

&lt;p&gt;Below are ten questions I ask, and the interviewers I work with ask, what each one tests, and the follow-up that decides the score. If you want the wider loop first, read &lt;a href="https://salari.dev/writing/senior-android-interview-follow-up-questions/" rel="noopener noreferrer"&gt;the 5 follow-ups that decide a senior Android loop&lt;/a&gt;. This article stays inside coroutines and Flow and goes further down.&lt;/p&gt;

&lt;h2&gt;
  
  
  The ten questions at a glance
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;What it tests&lt;/th&gt;
&lt;th&gt;The follow-up&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;What is structured concurrency?&lt;/td&gt;
&lt;td&gt;Ownership of work&lt;/td&gt;
&lt;td&gt;What happens to children when the parent fails?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;viewModelScope or lifecycleScope?&lt;/td&gt;
&lt;td&gt;Which lifetime owns the work&lt;/td&gt;
&lt;td&gt;Where does a save that must finish belong?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How does cancellation work?&lt;/td&gt;
&lt;td&gt;Cooperation&lt;/td&gt;
&lt;td&gt;Why does this loop keep running?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Job or SupervisorJob?&lt;/td&gt;
&lt;td&gt;Failure propagation&lt;/td&gt;
&lt;td&gt;Does supervisorScope catch the exception?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Where does CoroutineExceptionHandler work?&lt;/td&gt;
&lt;td&gt;Uncaught versus awaited&lt;/td&gt;
&lt;td&gt;Why did the handler never fire on async?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What does withContext do?&lt;/td&gt;
&lt;td&gt;Main safety&lt;/td&gt;
&lt;td&gt;Who should switch the dispatcher?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flow, StateFlow or SharedFlow?&lt;/td&gt;
&lt;td&gt;State versus events&lt;/td&gt;
&lt;td&gt;What happens to an event nobody collects?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cold or hot?&lt;/td&gt;
&lt;td&gt;Who starts the producer&lt;/td&gt;
&lt;td&gt;What does stateIn with WhileSubscribed buy you?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What does flowOn change?&lt;/td&gt;
&lt;td&gt;Upstream context&lt;/td&gt;
&lt;td&gt;Where does the collector run?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;How do you test a coroutine?&lt;/td&gt;
&lt;td&gt;Virtual time&lt;/td&gt;
&lt;td&gt;Why does your test pass without waiting?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  1. What is structured concurrency?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; whether you see coroutines as a tree with an owner, or as fire and forget threads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The answer that earns it:&lt;/strong&gt; "Every coroutine runs in a scope, and the scope's Job is the parent. A parent does not complete until its children complete. Cancelling the parent cancels every child. When a child fails with an exception, the failure goes up, cancels the parent and with it the siblings. No coroutine is orphaned, and no error disappears silently."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "So what is &lt;code&gt;GlobalScope&lt;/code&gt; for?" The senior answer is that it breaks the tree on purpose. Nothing cancels it and nothing waits for it. In an Android app that almost always means a leak or lost work, and the real fix is a scope with a named owner: the ViewModel, an application scope you inject, or WorkManager.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. viewModelScope or lifecycleScope?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; whether you choose a scope by lifetime, not by habit.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;viewModelScope&lt;/code&gt; is cancelled in &lt;code&gt;onCleared()&lt;/code&gt;, so it survives rotation and dies when the user leaves the screen for good. &lt;code&gt;lifecycleScope&lt;/code&gt; is tied to the Activity or Fragment and is cancelled on destroy, which includes rotation. In a Fragment, UI collection belongs in &lt;code&gt;viewLifecycleOwner.lifecycleScope&lt;/code&gt;, because the Fragment can outlive its view.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "The user taps Save and closes the screen at once. Which scope?" Neither is enough if the write must land. A candidate who says so, and moves the write to an application scope owned by the repository or to WorkManager when it must also survive the process, has answered a senior question.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. How does cancellation work, and why is this loop still running?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; that cancellation is cooperative. &lt;code&gt;cancel()&lt;/code&gt; sets a flag. Nothing stops until the code checks it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bug: no suspension point, so cancel() is never observed&lt;/span&gt;
&lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;hashFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;// blocking CPU work&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suspending functions from &lt;code&gt;kotlinx.coroutines&lt;/code&gt; such as &lt;code&gt;delay&lt;/code&gt;, &lt;code&gt;withContext&lt;/code&gt; and &lt;code&gt;yield&lt;/code&gt; check for cancellation and throw &lt;code&gt;CancellationException&lt;/code&gt;. A plain CPU loop does not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Fix: check the flag between units of work&lt;/span&gt;
&lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;ensureActive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;          &lt;span class="c1"&gt;// throws CancellationException once cancelled&lt;/span&gt;
        &lt;span class="nf"&gt;hashFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "You need to close a file after cancellation, and closing is a suspend call." The answer is &lt;code&gt;withContext(NonCancellable)&lt;/code&gt; inside &lt;code&gt;finally&lt;/code&gt;, because a cancelled coroutine that calls a suspending function throws again immediately. Keep that block short. It is for cleanup, not for new work.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Job or SupervisorJob?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; failure propagation, and the most common misunderstanding in this whole topic.&lt;/p&gt;

&lt;p&gt;With a regular &lt;code&gt;Job&lt;/code&gt;, one failing child cancels the parent and every sibling. With a &lt;code&gt;SupervisorJob&lt;/code&gt;, a child's failure stays with that child. &lt;code&gt;viewModelScope&lt;/code&gt; uses a &lt;code&gt;SupervisorJob&lt;/code&gt;, which is why one failed request does not kill every other coroutine in the ViewModel.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bug: the SupervisorJob is the parent of launch, not of its children&lt;/span&gt;
&lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SupervisorJob&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;loadProfile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;    &lt;span class="c1"&gt;// throws&lt;/span&gt;
    &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;loadFeed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;       &lt;span class="c1"&gt;// cancelled anyway&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Passing &lt;code&gt;SupervisorJob()&lt;/code&gt; to &lt;code&gt;launch&lt;/code&gt; makes it the parent of that one coroutine. The children inside get a regular &lt;code&gt;Job&lt;/code&gt; created by &lt;code&gt;launch&lt;/code&gt;, so a failure still cancels the sibling. It also detaches the coroutine from the outer scope, which breaks structured concurrency.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Fix: supervisorScope makes the children independent&lt;/span&gt;
&lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;supervisorScope&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;loadProfile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;loadFeed&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;// keeps running if loadProfile fails&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "Does &lt;code&gt;supervisorScope&lt;/code&gt; catch the exception?" No. It stops propagation to siblings. The failing child's exception still goes to a &lt;code&gt;CoroutineExceptionHandler&lt;/code&gt; or, without one, to the thread's uncaught exception handler, which on Android crashes the app.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Where does CoroutineExceptionHandler actually work?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; the difference between an exception that is uncaught and one that is waiting to be awaited.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;CoroutineExceptionHandler&lt;/code&gt; is a last resort. It is only called for uncaught exceptions, and only when installed on a root coroutine or a direct child of a supervisor. Installed on an inner &lt;code&gt;launch&lt;/code&gt; of a regular job, it is ignored, because the child hands the failure to its parent instead.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; does not report to the handler at all when it is a root. It stores the exception in the &lt;code&gt;Deferred&lt;/code&gt; and rethrows it from &lt;code&gt;await()&lt;/code&gt;. So the handler never fires, and a &lt;code&gt;try&lt;/code&gt; around &lt;code&gt;await()&lt;/code&gt; is where you handle it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "Then how do you handle errors in a ViewModel?" The strong answer catches expected failures close to the call, maps them to UI state, and rethrows &lt;code&gt;CancellationException&lt;/code&gt; if a broad catch could see it. The handler is for logging what nobody expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. What does withContext do, and who should call it?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; main safety as a contract, not a habit.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;withContext&lt;/code&gt; suspends the caller, runs the block on another dispatcher, and resumes with the result. It does not start a new coroutine that outlives the call. &lt;code&gt;Dispatchers.Main&lt;/code&gt; is for UI, &lt;code&gt;Dispatchers.Default&lt;/code&gt; for CPU work sized to the cores, &lt;code&gt;Dispatchers.IO&lt;/code&gt; for blocking IO on a larger pool that shares threads with Default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "Should the ViewModel write &lt;code&gt;withContext(Dispatchers.IO)&lt;/code&gt; before calling the repository?" No. The class that does the blocking work owns the switch, so every suspend function is safe to call from the main thread. The strong candidate also injects the dispatcher so a test can replace it. Room and Retrofit suspend functions are already main safe, so wrapping them again only adds noise.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Flow, StateFlow or SharedFlow?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; whether you separate state from events.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Hot or cold&lt;/th&gt;
&lt;th&gt;Holds a value&lt;/th&gt;
&lt;th&gt;Use it for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Flow&lt;/td&gt;
&lt;td&gt;Cold&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;A stream that runs per collector: a query, a paged load&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;StateFlow&lt;/td&gt;
&lt;td&gt;Hot&lt;/td&gt;
&lt;td&gt;Always one, conflated, equal values skipped&lt;/td&gt;
&lt;td&gt;Screen state the UI renders&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;SharedFlow&lt;/td&gt;
&lt;td&gt;Hot&lt;/td&gt;
&lt;td&gt;Configurable replay and buffer&lt;/td&gt;
&lt;td&gt;Broadcasts to several collectors&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "Where do you put a one-off event like 'show a snackbar'?" A &lt;code&gt;SharedFlow&lt;/code&gt; with no replay drops an event when nobody is collecting, for example during rotation. A &lt;code&gt;StateFlow&lt;/code&gt; replays it on every new collector, so it shows twice. The senior answer names both failures, then models the event as part of state that the UI clears after handling it, or uses a &lt;code&gt;Channel&lt;/code&gt; received as a flow when exactly one consumer must see each event once.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Cold or hot, and what does stateIn buy you?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; who starts the producer, and when it stops.&lt;/p&gt;

&lt;p&gt;A cold flow runs its block again for every collector. Two collectors on one Room query means two queries. &lt;code&gt;stateIn&lt;/code&gt; turns it hot and shares one upstream.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StateFlow&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observeOrders&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loaded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stateIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SharingStarted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WhileSubscribed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5_000&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="n"&gt;initialValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "Why 5,000 milliseconds?" &lt;code&gt;WhileSubscribed(5_000)&lt;/code&gt; keeps the upstream alive for five seconds after the last collector leaves. A rotation takes less than that, so the query does not restart. When the app goes to the background for longer, the upstream stops and stops costing work. &lt;code&gt;Eagerly&lt;/code&gt; never stops, and &lt;code&gt;Lazily&lt;/code&gt; never stops once started.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. What does flowOn change, and where does the collector run?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; that a flow's context flows upstream only.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bug: everything runs on IO, including render()&lt;/span&gt;
&lt;span class="n"&gt;lifecycleScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;withContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;readLines&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;// render off Main&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Fix: flowOn moves only what is above it&lt;/span&gt;
&lt;span class="n"&gt;viewLifecycleOwner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lifecycleScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;viewLifecycleOwner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeatOnLifecycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Lifecycle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;STARTED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;readLines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flowOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// readLines and parse run here&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;        &lt;span class="c1"&gt;// on Main, in the collector's context&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;flowOn&lt;/code&gt; changes the context of every operator above it and leaves the collector where it is. You cannot use &lt;code&gt;withContext&lt;/code&gt; inside a &lt;code&gt;flow { }&lt;/code&gt; builder to emit from another dispatcher. That breaks context preservation and throws &lt;code&gt;IllegalStateException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "What does &lt;code&gt;flowOn&lt;/code&gt; do to the buffer?" It introduces a channel between the two contexts, so the producer can run ahead of the collector. The candidate who knows that also knows why &lt;code&gt;conflate()&lt;/code&gt; or &lt;code&gt;buffer()&lt;/code&gt; placed next to it change behaviour.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. How do you test coroutine code?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What it tests:&lt;/strong&gt; virtual time and dispatcher control.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;runTest&lt;/code&gt; runs the test body on a &lt;code&gt;TestScope&lt;/code&gt; with a &lt;code&gt;StandardTestDispatcher&lt;/code&gt;. &lt;code&gt;delay&lt;/code&gt; skips virtual time instead of waiting, which is why a five second delay finishes at once. New coroutines are queued, not run, until the test yields or calls &lt;code&gt;advanceUntilIdle()&lt;/code&gt;. &lt;code&gt;UnconfinedTestDispatcher&lt;/code&gt; runs them eagerly, which is simpler for collectors and hides ordering bugs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;refresh_showsLoadedState&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runTest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;dispatcher&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StandardTestDispatcher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testScheduler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setMain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dispatcher&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;vm&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrdersViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;FakeRepository&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;ioDispatcher&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dispatcher&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;advanceUntilIdle&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;assertEquals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;UiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loaded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sampleOrders&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;vm&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resetMain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The follow-up:&lt;/strong&gt; "Your &lt;code&gt;stateIn&lt;/code&gt; with &lt;code&gt;WhileSubscribed&lt;/code&gt; never updates in the test. Why?" Nothing collects it, so the upstream never starts. Collect it in &lt;code&gt;backgroundScope&lt;/code&gt; for the length of the test, or read it with a library such as Turbine. The candidate who has hit this in real code answers in one sentence.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recovery lines when you wobble
&lt;/h2&gt;

&lt;p&gt;Every senior candidate gets one of these wrong at some point. What I score is the recovery.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;"Let me draw the job tree before I answer." Then say who the parent is. Half of these questions answer themselves once the tree is on the board.&lt;/li&gt;
&lt;li&gt;"I said the handler catches it. It doesn't for &lt;code&gt;async&lt;/code&gt;, the exception waits in &lt;code&gt;await()&lt;/code&gt;. Let me correct that."&lt;/li&gt;
&lt;li&gt;"I'm not sure of the exact default for that buffer. What I do know is the behaviour I'd test for, and here is the test."&lt;/li&gt;
&lt;li&gt;"I've used &lt;code&gt;lifecycleScope&lt;/code&gt; there before. Given what you've said about the save, I'd move it to a scope that outlives the screen."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A correction in your own words beats a confident wrong answer every time. It shows the interviewer how you would behave in a code review.&lt;/p&gt;

&lt;h2&gt;
  
  
  A 20-minute coroutines drill
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Write a scope with two &lt;code&gt;launch&lt;/code&gt; children and throw in one. Predict, run, explain.&lt;/li&gt;
&lt;li&gt;Swap the parent for &lt;code&gt;supervisorScope&lt;/code&gt;. Predict, run, explain.&lt;/li&gt;
&lt;li&gt;Write a CPU loop, cancel it, and watch it keep running. Add &lt;code&gt;ensureActive()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Expose a &lt;code&gt;StateFlow&lt;/code&gt; with &lt;code&gt;stateIn&lt;/code&gt; and test it with &lt;code&gt;runTest&lt;/code&gt; and &lt;code&gt;backgroundScope&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Say each answer out loud in under 45 seconds, then state the follow-up and answer that too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Reading these answers helps. Watching your own prediction fail on a test run is what makes them stay.&lt;/p&gt;

&lt;h2&gt;
  
  
  Questions engineers ask about Kotlin coroutines interviews
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What Kotlin coroutines questions come up in Android interviews?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Structured concurrency, scopes, cancellation, SupervisorJob, exception handling, dispatchers, Flow versus StateFlow and SharedFlow, lifecycle-aware collection and testing with runTest. The first question checks the name. The follow-up checks whether you know who owns the work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How should I prepare for a Kotlin coroutines interview?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Write small programs that fail. Launch two children and throw in one. Cancel a loop that never suspends. Collect a StateFlow in the background. Then explain out loud what happened and why. Reading the documentation gives you names. Breaking code gives you answers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Is Flow or LiveData expected in a senior Android interview?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Flow. LiveData still exists in older codebases and you should know how it relates, but current AndroidX guidance exposes StateFlow from the ViewModel and collects it with repeatOnLifecycle or collectAsStateWithLifecycle.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;First published on &lt;a href="https://salari.dev/writing/kotlin-coroutines-interview-questions/?utm_source=devto&amp;amp;utm_medium=syndication&amp;amp;utm_campaign=kotlin-coroutines-interview-questions" rel="noopener noreferrer"&gt;salari.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>coroutines</category>
      <category>career</category>
    </item>
    <item>
      <title>Your SwiftUI app works. Production is where it starts lying to you.</title>
      <dc:creator>Mike Salari</dc:creator>
      <pubDate>Tue, 27 Oct 2026 14:00:00 +0000</pubDate>
      <link>https://dev.to/salari_dev/your-swiftui-app-works-production-is-where-it-starts-lying-to-you-2j55</link>
      <guid>https://dev.to/salari_dev/your-swiftui-app-works-production-is-where-it-starts-lying-to-you-2j55</guid>
      <description>&lt;p&gt;A SwiftUI screen can be completely correct in development and still have the wrong production model.&lt;/p&gt;

&lt;p&gt;The request returned. The list rendered. The state changed when it was supposed to change. Everything worked, on the simulator, with fixture data, with one person tapping in a predictable order. Then real timing arrived, then real data, then real users, and the screen began reporting things that were no longer true.&lt;/p&gt;

&lt;p&gt;None of the failures below start with a missing SwiftUI modifier. They are not the kind of bug a component reference can catch, because the UI already works when they appear. They show up when the system underneath the view has to answer questions the happy path never asked: which result still owns the screen, where the expensive work actually happens, and who owns this state and for how long.&lt;/p&gt;

&lt;p&gt;Three cases. Each is small enough to reproduce in an afternoon and common enough that you have probably shipped at least one of them.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The latest request is not necessarily the latest result
&lt;/h2&gt;

&lt;p&gt;Take a search field backed by an async API. Every keystroke calls &lt;code&gt;search&lt;/code&gt;, every call starts a task, and every task writes its results when it finishes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;@MainActor&lt;/span&gt;
&lt;span class="kd"&gt;@Observable&lt;/span&gt;
&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;SearchModel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;results&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;SearchResult&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchAPI&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchAPI&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;Task&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Real failure policy goes here.&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The user types &lt;code&gt;swift&lt;/code&gt;, then a moment later &lt;code&gt;swiftui&lt;/code&gt;. Two requests are in flight. The second one hits a warm cache, or gets a faster connection, or simply has less to return, and it completes first. The screen shows results for &lt;code&gt;swiftui&lt;/code&gt;. Then the older request for &lt;code&gt;swift&lt;/code&gt; completes and overwrites them. The user is now looking at results for a query they are no longer asking.&lt;/p&gt;

&lt;p&gt;Nothing in this sequence failed. Both requests succeeded. Both writes to &lt;code&gt;results&lt;/code&gt; happened on the main actor, one at a time, so there is no data race for the compiler or the sanitizer to catch. Every mutation was legal. The UI is still wrong.&lt;/p&gt;

&lt;p&gt;It is tempting to file this under concurrency and reach for cancellation. Cancellation helps, but it is cooperative: a request that has already returned cannot be un-returned, and a task that checks for cancellation late will still publish. &lt;code&gt;async/await&lt;/code&gt; orders the steps inside one task. It says nothing about which of several completed tasks still has the right to publish state. That is a question about intent, and only the model can answer it, because only the model knows what the user asked for most recently.&lt;/p&gt;

&lt;p&gt;The smallest correction that proves this diagnosis is to give each request an identity and let only the newest one write.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;@MainActor&lt;/span&gt;
&lt;span class="kd"&gt;@Observable&lt;/span&gt;
&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;SearchModel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;results&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;SearchResult&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchAPI&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;generation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;SearchAPI&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;api&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;generation&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;requestGeneration&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;generation&lt;/span&gt;

        &lt;span class="kt"&gt;Task&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

                &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;requestGeneration&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;generation&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;return&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;

                &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Real failure policy goes here.&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;generation&lt;/code&gt; is incremented synchronously, on the main actor, before the task starts, so the comparison after the &lt;code&gt;await&lt;/code&gt; is against whatever value the newest call left behind. A stale result is not an error. It is a result that has lost the right to be shown, and it is dropped. If the API supports it, cancelling the previous task on top of this saves work, but the guard is what makes the screen correct. Cancellation is an optimisation of it, not a substitute for it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Request lifetime and user intent are not the same thing.&lt;/strong&gt; A request lives until it returns. The user's intent lives until the next keystroke. The state has to follow the intent.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. "It is lazy" is not "it is fast"
&lt;/h2&gt;

&lt;p&gt;The second failure arrives with scale, and it usually arrives with a sentence: it is fine, the list is lazy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;ScrollView&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;LazyVStack&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;ForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
            &lt;span class="kt"&gt;ItemRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is correct, and for a few hundred items it is the whole story. &lt;code&gt;LazyVStack&lt;/code&gt; constructs its children on demand, so rows far below the fold cost nothing until they scroll in. Then the feature grows. &lt;code&gt;items&lt;/code&gt; becomes a feed of 10,000 rows with a search field above it, and every keystroke freezes the screen for long enough that the keyboard visibly stalls. The instinct is to blame SwiftUI, or to blame the list, and to start swapping containers.&lt;/p&gt;

&lt;p&gt;The container is not where the time goes. The lazy container answers exactly one question: which rows need to exist right now. It has no opinion about what &lt;code&gt;items&lt;/code&gt; is or how it was produced. In the typical version of this screen, a keystroke normalises the query, filters all 10,000 items against it, sorts the survivors, groups them into sections and builds a fresh array. Because the array is new, SwiftUI then has to work out which rows changed, and if the ids are unstable the answer is all of them. Rows that survived are reconstructed, each row looks up an image, some of those images are decoded and resized on demand, and layout runs for whatever is visible. All of that happens before laziness is even consulted, and most of it happens on the main actor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The lazy container solved the one problem it solves. It did not make the pipeline lazy.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It helps to see the whole route, because the freeze can live in any stage of it.&lt;/p&gt;

&lt;p&gt;Data to pixels&lt;/p&gt;

&lt;p&gt;1.&lt;/p&gt;

&lt;p&gt;User input&lt;/p&gt;

&lt;p&gt;keystroke, scroll, refresh&lt;br&gt;
2.&lt;/p&gt;

&lt;p&gt;Normalization&lt;/p&gt;

&lt;p&gt;trim, lowercase, diacritics&lt;br&gt;
3.&lt;/p&gt;

&lt;p&gt;Filtering&lt;/p&gt;

&lt;p&gt;every row, every keystroke&lt;br&gt;
4.&lt;/p&gt;

&lt;p&gt;Sorting / grouping&lt;br&gt;
5.&lt;/p&gt;

&lt;p&gt;Snapshot&lt;/p&gt;

&lt;p&gt;the array the view sees&lt;br&gt;
6.&lt;/p&gt;

&lt;p&gt;Image lookup&lt;/p&gt;

&lt;p&gt;cache hit or miss&lt;br&gt;
7.&lt;/p&gt;

&lt;p&gt;Decode / resize&lt;/p&gt;

&lt;p&gt;full-size image on the main thread&lt;br&gt;
8.&lt;/p&gt;

&lt;p&gt;Row construction&lt;br&gt;
9.&lt;/p&gt;

&lt;p&gt;Layout&lt;br&gt;
10.&lt;/p&gt;

&lt;p&gt;Pixels&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A large SwiftUI list is not merely a view hierarchy. It is a data-to-pixels pipeline.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once it is drawn this way, the question changes. Not "is SwiftUI slow", but "which stage is slow, and why is it running on this keystroke at all". Every stage has its own fix and its own evidence. Filtering and sorting can move off the main actor and sit behind a request generation, the same mechanism as the first case, so that a keystroke never publishes a snapshot the user has already typed past. Snapshots can keep stable identity, so a new array does not read as 10,000 replacements. Image decoding can leave the row and be cached at display size. Invalidation can be narrowed, so a change in one row does not rebuild the section around it. None of these is the answer until measurement says which stage is the bottleneck.&lt;/p&gt;

&lt;p&gt;That evidence comes from tools, not from intuition. A Time Profiler trace recorded while typing shows where the main actor spends each keystroke. Instruments' SwiftUI template shows whether view bodies are being evaluated broadly or narrowly, and a counter in a body will tell you the same thing more crudely. Allocation and memory traces show whether images are being decoded at scroll time and at what size. Row identity is worth checking by hand: if the model's &lt;code&gt;id&lt;/code&gt; changes when the content does not, every diff is a replacement. There are deliberately no timings in this article. They depend on the device, the dataset and the row, and the point of the exercise is to produce your own.&lt;/p&gt;

&lt;p&gt;The lesson is a discipline rather than a technique: do not diagnose "SwiftUI is slow" before you have found the slowest stage. The framework is rarely that stage. The pipeline built in front of it usually is.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. Your state model worked until everybody needed it
&lt;/h2&gt;

&lt;p&gt;The third failure is the slowest to appear and the hardest to name, because nothing crashes and nothing measurably stalls. It is what happens to a state model that was designed for one screen and then adopted by the whole app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;@Observable&lt;/span&gt;
&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;AppModel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;feed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;notifications&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Notification&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;searchResults&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;SearchResult&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;settings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Settings&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a reasonable starting point, and it is worth being precise about what is and is not wrong with it. It is not a rendering problem. Observation tracks property access, so a view that reads &lt;code&gt;feed&lt;/code&gt; is not re-evaluated when &lt;code&gt;notifications&lt;/code&gt; changes. The claim that one large &lt;code&gt;@Observable&lt;/code&gt; model redraws every view on every change is not true, and if you split this model for that reason you will spend the effort and keep the real problem.&lt;/p&gt;

&lt;p&gt;The real problem is ownership. Ask the questions the type does not answer. Who owns &lt;code&gt;user&lt;/code&gt;, and what happens to &lt;code&gt;feed&lt;/code&gt; and &lt;code&gt;searchResults&lt;/code&gt; when the user signs out? How long does &lt;code&gt;searchResults&lt;/code&gt; live: as long as the search screen, or as long as the app? Which async work belongs to this object, and who cancels it? If notifications are removed as a feature next quarter, what else in this class disappears with them? Can the feed logic be tested without constructing a user, a settings object and a search? Every property here has a different answer, and the type has flattened them into one.&lt;/p&gt;

&lt;p&gt;Different answers are the reason to draw boundaries. A &lt;code&gt;SessionState&lt;/code&gt; that owns the user and the sign-out sequence. A &lt;code&gt;FeedState&lt;/code&gt; that lives while the feed is on screen and owns its own pagination and refresh tasks. A &lt;code&gt;SearchState&lt;/code&gt; that owns the request generation from the first case and nothing else. A &lt;code&gt;SettingsState&lt;/code&gt; that persists across sessions. These are not four models because four is a better number than one. They are four because they have four different lifetimes, four different mutation policies and four different sets of async work to cancel.&lt;/p&gt;

&lt;p&gt;Splitting is not automatically better. A model with three properties that always change together, live for the same time and are mutated from the same place should stay one model; splitting it adds plumbing and nothing else. The test is not size. It is whether the parts give different answers to the ownership questions. When they do, the single model is hiding a design decision that will be made anyway, later, by whoever has to add the next feature under a deadline.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;SwiftUI architecture is often state ownership disguised as view architecture.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  These are not SwiftUI syntax problems
&lt;/h2&gt;

&lt;p&gt;Put the three cases next to each other and the pattern is the same. The happy path worked. The failure appeared when the system had to answer a question the happy path never asked: which result still owns the UI, where the expensive work actually happens, and who owns this state and for how long. In each case the correction was not a modifier. It was a decision about ownership, made explicit in code.&lt;/p&gt;

&lt;p&gt;The way through them is the same as well. Reproduce the failure first, in the smallest project that still shows it, because a failure you cannot trigger on demand is one you cannot prove fixed. Then find the mechanism, which means naming the exact step that produces the wrong state rather than the general area: a stale write after an &lt;code&gt;await&lt;/code&gt;, a full sort on the main actor per keystroke, a lifetime that outlives its screen. Apply the smallest correction that proves the diagnosis, a generation guard, a moved filter, a split model. If the failure disappears, the diagnosis was right; if it does not, you have learned that before building anything on top of it. Only then design the boundary that prevents the whole class of failure rather than this one instance, and prove the correction with something you can run again, a test, a trace or a counter, so the next change cannot quietly bring the failure back.&lt;/p&gt;

&lt;h2&gt;
  
  
  SwiftUI gets harder after you know SwiftUI
&lt;/h2&gt;

&lt;p&gt;Early SwiftUI is about getting the screen to work: the layout, the binding, the modifier that does what you meant. That stage ends, and it ends sooner than it feels like it should. What comes after is production: real latency, real data volumes, real images, state that has to live for a specific time and then go away, cancellation that is cooperative rather than immediate, migrations away from the code that was there before, and real users who do things in an order nobody sketched.&lt;/p&gt;

&lt;p&gt;At that stage, building the view is no longer the hard part. Keeping the system underneath it truthful is.&lt;/p&gt;

&lt;p&gt;That is the stage I wrote SwiftUI Under Load for. I did not want another component reference or another collection of isolated SwiftUI recipes. The material starts with failures you can reproduce, then works through the mechanism, the smallest correction, the design that holds, and the evidence that proves it.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;First published on &lt;a href="https://salari.dev/writing/swiftui-production-failures/?utm_source=devto&amp;amp;utm_medium=syndication&amp;amp;utm_campaign=swiftui-production-failures" rel="noopener noreferrer"&gt;salari.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>swiftui</category>
      <category>ios</category>
      <category>swift</category>
      <category>mobile</category>
    </item>
    <item>
      <title>You cancelled the caller. Why is the shared Task still running?</title>
      <dc:creator>Mike Salari</dc:creator>
      <pubDate>Tue, 20 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/salari_dev/you-cancelled-the-caller-why-is-the-shared-task-still-running-3m2k</link>
      <guid>https://dev.to/salari_dev/you-cancelled-the-caller-why-is-the-shared-task-still-running-3m2k</guid>
      <description>&lt;p&gt;Here is a Swift concurrency question that looks easier than it is.&lt;/p&gt;

&lt;p&gt;A caller is waiting for the value of a shared &lt;code&gt;Task&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The caller gets cancelled.&lt;/p&gt;

&lt;p&gt;What happens next?&lt;/p&gt;

&lt;p&gt;A very tempting answer is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;await&lt;/code&gt; throws &lt;code&gt;CancellationError&lt;/code&gt;, so the caller leaves immediately.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is not what the shared-task case guarantees.&lt;/p&gt;

&lt;p&gt;If the task you are awaiting is a separate unstructured &lt;code&gt;Task&lt;/code&gt;, cancelling the caller does not automatically cancel that task. Accessing the task's &lt;code&gt;value&lt;/code&gt; waits for the task to complete and then returns or propagates that task's result.&lt;/p&gt;

&lt;p&gt;That distinction matters because Swift cancellation is not a general-purpose interrupt.&lt;/p&gt;

&lt;p&gt;It is a signal.&lt;/p&gt;

&lt;h2&gt;
  
  
  The small example
&lt;/h2&gt;

&lt;p&gt;Consider a service that deduplicates refresh work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;actor&lt;/span&gt; &lt;span class="kt"&gt;RefreshStore&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;refreshTask&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;Error&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;refreshTask&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;refreshTask&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;task&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;performRefresh&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;refreshTask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;

        &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;refreshTask&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;performRefresh&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;throws&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"fresh"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important part is not the actor.&lt;/p&gt;

&lt;p&gt;It is that &lt;code&gt;task&lt;/code&gt; is its own task.&lt;/p&gt;

&lt;p&gt;Several callers may end up awaiting the same handle.&lt;/p&gt;

&lt;p&gt;Now imagine one of those callers is cancelled while this line is suspended:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is easy to read &lt;code&gt;try await&lt;/code&gt; and mentally substitute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;caller cancelled
↓
await throws CancellationError
↓
shared task stops
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But those are separate events.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cancelling a Task is cooperative
&lt;/h2&gt;

&lt;p&gt;Swift's &lt;code&gt;Task.cancel()&lt;/code&gt; does three important things: it marks the task as cancelled, triggers its cancellation handlers, and propagates cancellation through its structured child tasks.&lt;/p&gt;

&lt;p&gt;What it does &lt;strong&gt;not&lt;/strong&gt; do is force arbitrary asynchronous work to stop.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://docs.swift.org/latest/documentation/swift/task/cancel%28%29/" rel="noopener noreferrer"&gt;Swift documentation&lt;/a&gt; is explicit about this: cancellation is cooperative. Code has to observe cancellation and decide how to react.&lt;/p&gt;

&lt;p&gt;That can happen through:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isCancelled&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="kt"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;checkCancellation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or because an operation you call is itself cancellation-aware.&lt;/p&gt;

&lt;p&gt;A cancelled task can continue running if its code never responds to the cancellation signal.&lt;/p&gt;

&lt;p&gt;That is the first mental model to keep.&lt;/p&gt;

&lt;h2&gt;
  
  
  The caller and the shared task are not the same task
&lt;/h2&gt;

&lt;p&gt;Now return to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The current task is the caller.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;task&lt;/code&gt; is another task represented by a &lt;code&gt;Task&lt;/code&gt; handle.&lt;/p&gt;

&lt;p&gt;Cancelling the current task marks the &lt;strong&gt;current task&lt;/strong&gt; as cancelled.&lt;/p&gt;

&lt;p&gt;It does not mean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;was called on the shared task.&lt;/p&gt;

&lt;p&gt;And it does not mean the shared task must inherit that cancellation just because another task is awaiting its value.&lt;/p&gt;

&lt;p&gt;That is the key difference between waiting on an unstructured task handle and cancellation propagation inside a structured task tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Task.value actually promises
&lt;/h2&gt;

&lt;p&gt;For a throwing task, &lt;code&gt;value&lt;/code&gt; is an &lt;a href="https://developer.apple.com/documentation/swift/task/value-60t02" rel="noopener noreferrer"&gt;async throwing property&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If the task has not completed, accessing &lt;code&gt;value&lt;/code&gt; waits for it.&lt;/p&gt;

&lt;p&gt;If that task eventually throws, &lt;code&gt;value&lt;/code&gt; propagates that error.&lt;/p&gt;

&lt;p&gt;If that task responds to its own cancellation by throwing &lt;code&gt;CancellationError&lt;/code&gt;, then awaiting its value propagates that error.&lt;/p&gt;

&lt;p&gt;Notice which task those statements are about.&lt;/p&gt;

&lt;p&gt;They are about the task represented by the handle.&lt;/p&gt;

&lt;p&gt;That is not the same as saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the caller waiting for &lt;code&gt;value&lt;/code&gt; becomes cancelled, &lt;code&gt;value&lt;/code&gt; automatically throws &lt;code&gt;CancellationError&lt;/code&gt; from the caller's cancellation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That assumption is exactly where the bug in the mental model starts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Structured cancellation is different
&lt;/h2&gt;

&lt;p&gt;Swift does propagate cancellation through structured concurrency.&lt;/p&gt;

&lt;p&gt;If you cancel a task that owns child tasks created through a &lt;a href="https://docs.swift.org/latest/documentation/swift/taskgroup/" rel="noopener noreferrer"&gt;task group&lt;/a&gt; or another structured construct, cancellation is propagated down that hierarchy.&lt;/p&gt;

&lt;p&gt;But an unstructured &lt;code&gt;Task { ... }&lt;/code&gt; stored in a property and shared among callers is a different shape.&lt;/p&gt;

&lt;p&gt;You now have independent task lifetimes.&lt;/p&gt;

&lt;p&gt;That can be useful.&lt;/p&gt;

&lt;p&gt;For example, you may intentionally want one network refresh to continue even if one screen disappears, because another caller is still waiting for the same refresh.&lt;/p&gt;

&lt;p&gt;But once you make that choice, you have to decide explicitly what caller cancellation should mean.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sometimes you do want the caller to stop waiting
&lt;/h2&gt;

&lt;p&gt;Suppose the shared refresh should continue for other clients, but a cancelled caller should stop caring about its result.&lt;/p&gt;

&lt;p&gt;Those are two different cancellation decisions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Should the underlying shared operation be cancelled?&lt;/li&gt;
&lt;li&gt;Should this particular caller continue waiting for it?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do not collapse them into one boolean.&lt;/p&gt;

&lt;p&gt;If you need the waiting caller to react immediately, you need to design that behaviour explicitly. A &lt;a href="https://docs.swift.org/latest/documentation/swift/withtaskcancellationhandler%28operation%3Aoncancel%3A%29/" rel="noopener noreferrer"&gt;cancellation handler&lt;/a&gt; can observe cancellation immediately, but you still need a safe mechanism for racing the shared result against the caller's cancellation without accidentally cancelling work needed by other consumers.&lt;/p&gt;

&lt;p&gt;The correct design depends on the ownership model of the operation.&lt;/p&gt;

&lt;p&gt;That is why "just cancel the task" is usually too vague.&lt;/p&gt;

&lt;h2&gt;
  
  
  The inverse problem is just as dangerous
&lt;/h2&gt;

&lt;p&gt;There is another common mistake:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;withTaskCancellationHandler&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nv"&gt;onCancel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This may be correct if the caller owns the task.&lt;/p&gt;

&lt;p&gt;It can be very wrong if &lt;code&gt;task&lt;/code&gt; is shared.&lt;/p&gt;

&lt;p&gt;Imagine three callers waiting on the same refresh:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Caller A ─┐
Caller B ─┼──&amp;gt; Shared Task
Caller C ─┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Caller A goes away.&lt;/p&gt;

&lt;p&gt;If A's cancellation handler blindly calls:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A has now changed the operation for B and C too.&lt;/p&gt;

&lt;p&gt;That may be exactly what you did not want.&lt;/p&gt;

&lt;p&gt;Shared work needs shared ownership semantics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cancellation is not ownership
&lt;/h2&gt;

&lt;p&gt;This is the broader lesson.&lt;/p&gt;

&lt;p&gt;Cancellation is often discussed as if it were a universal "stop" command.&lt;/p&gt;

&lt;p&gt;It is not.&lt;/p&gt;

&lt;p&gt;In Swift it is cooperative, and propagation depends on the task relationship you built.&lt;/p&gt;

&lt;p&gt;Before writing cancellation code, ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which task was cancelled?&lt;/li&gt;
&lt;li&gt;Which task owns the work?&lt;/li&gt;
&lt;li&gt;Is the operation structured or unstructured?&lt;/li&gt;
&lt;li&gt;Is the task handle shared?&lt;/li&gt;
&lt;li&gt;Should one caller be able to cancel work used by another caller?&lt;/li&gt;
&lt;li&gt;Does the operation check cancellation itself?&lt;/li&gt;
&lt;li&gt;Do I want to cancel the work, stop waiting, or both?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those questions are much more useful than simply asking whether something is &lt;code&gt;async throws&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this matters in interviews
&lt;/h2&gt;

&lt;p&gt;A senior Swift concurrency answer should not stop at:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cancellation throws &lt;code&gt;CancellationError&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is too broad.&lt;/p&gt;

&lt;p&gt;A stronger answer separates the pieces:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cancelling a Swift task sets cancellation state and propagates through structured children. It does not forcibly stop arbitrary work. If I am awaiting a separate shared &lt;code&gt;Task&lt;/code&gt; handle, cancelling my caller does not by itself cancel that shared task. I need to decide explicitly whether the caller should stop waiting, whether the shared work should be cancelled, or both.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is the answer I would want to hear before we start editing code.&lt;/p&gt;

&lt;h2&gt;
  
  
  This was corrected in Share Your Screen 1.3
&lt;/h2&gt;

&lt;p&gt;An earlier version of Share Your Screen described this shared-task case incorrectly. It said that a cancelled caller would leave the &lt;code&gt;await&lt;/code&gt; with &lt;code&gt;CancellationError&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Version 1.3 corrects that explanation.&lt;/p&gt;

&lt;p&gt;The point of the correction is not that cancellation "never throws". It absolutely can. Swift provides &lt;code&gt;Task.checkCancellation()&lt;/code&gt;, cancellation-aware APIs can throw, and a task can propagate its own cancellation error through &lt;code&gt;value&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The important correction is narrower:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Cancelling the caller is not, by itself, a rule that makes an await on a separate shared task handle immediately throw &lt;code&gt;CancellationError&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once you separate the caller from the shared work, the behaviour becomes much easier to reason about.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Swift, &lt;a href="https://docs.swift.org/latest/documentation/swift/task/cancel%28%29/" rel="noopener noreferrer"&gt;Task.cancel()&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Apple, &lt;a href="https://developer.apple.com/documentation/swift/task/value-60t02" rel="noopener noreferrer"&gt;Task.value&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Swift, &lt;a href="https://docs.swift.org/latest/documentation/swift/withtaskcancellationhandler%28operation%3Aoncancel%3A%29/" rel="noopener noreferrer"&gt;withTaskCancellationHandler(operation:onCancel:)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Swift, &lt;a href="https://docs.swift.org/latest/documentation/swift/taskgroup/" rel="noopener noreferrer"&gt;TaskGroup&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;First published on &lt;a href="https://salari.dev/writing/swift-shared-task-cancellation/?utm_source=devto&amp;amp;utm_medium=syndication&amp;amp;utm_campaign=swift-shared-task-cancellation" rel="noopener noreferrer"&gt;salari.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>concurrency</category>
      <category>programming</category>
    </item>
    <item>
      <title>An actor protects state. It does not own every retain and release.</title>
      <dc:creator>Mike Salari</dc:creator>
      <pubDate>Tue, 13 Oct 2026 13:00:00 +0000</pubDate>
      <link>https://dev.to/salari_dev/an-actor-protects-state-it-does-not-own-every-retain-and-release-27ba</link>
      <guid>https://dev.to/salari_dev/an-actor-protects-state-it-does-not-own-every-retain-and-release-27ba</guid>
      <description>&lt;p&gt;Swift actors make an important promise.&lt;/p&gt;

&lt;p&gt;They isolate mutable state.&lt;/p&gt;

&lt;p&gt;That promise is strong enough for Swift 6 to reject programs that cross concurrency boundaries unsafely, and it gives us a much better language for reasoning about data races.&lt;/p&gt;

&lt;p&gt;But there is a tempting shortcut hiding inside that model:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If an object is actor-isolated, its references must be isolated too.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;From there it is easy to jump again:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If those references are isolated, ARC should not need atomic reference counting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That sounds reasonable.&lt;/p&gt;

&lt;p&gt;It is also more than actor isolation actually promises.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start with the guarantee actors do make
&lt;/h2&gt;

&lt;p&gt;An &lt;code&gt;actor&lt;/code&gt; protects its &lt;a href="https://docs.swift.org/main/documentation/swift/actor/" rel="noopener noreferrer"&gt;actor-isolated state&lt;/a&gt; from concurrent access that violates Swift's isolation rules.&lt;/p&gt;

&lt;p&gt;An actor also conforms to &lt;code&gt;Sendable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Those are language-level concurrency properties.&lt;/p&gt;

&lt;p&gt;They help answer questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can this mutable state be accessed from here?&lt;/li&gt;
&lt;li&gt;Does this value cross an isolation boundary legally?&lt;/li&gt;
&lt;li&gt;Does this code need &lt;code&gt;await&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Is the compiler able to prove that a particular access is safe?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those are valuable guarantees.&lt;/p&gt;

&lt;p&gt;But notice what is missing.&lt;/p&gt;

&lt;p&gt;Actor isolation does not say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every reference-count operation associated with every object reachable from this actor happens only while executing on this actor.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That would be a much broader lifetime guarantee.&lt;/p&gt;

&lt;h2&gt;
  
  
  A reference can participate in more than one mechanism
&lt;/h2&gt;

&lt;p&gt;Consider an actor that stores a class instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Payload&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;actor&lt;/span&gt; &lt;span class="kt"&gt;Store&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Payload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;currentValue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actor isolates access to &lt;code&gt;payload&lt;/code&gt; as actor state.&lt;/p&gt;

&lt;p&gt;That tells you something important about how the property can be accessed.&lt;/p&gt;

&lt;p&gt;But ARC is not merely a record of source-level property accesses.&lt;/p&gt;

&lt;p&gt;Reference lifetime can be affected by generated temporaries, function arguments, return values, captures, ownership conventions, optimizer decisions and other lowering details.&lt;/p&gt;

&lt;p&gt;So there are two different questions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Who may access this state?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;How is this object's lifetime represented and updated in generated code?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first is where actor isolation operates.&lt;/p&gt;

&lt;p&gt;The second belongs to ownership, ARC and compiler lowering.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is exactly what the ARC experiment tested
&lt;/h2&gt;

&lt;p&gt;I built a &lt;a href="https://github.com/salari-dev/swift6-arc-atomicity-lab" rel="noopener noreferrer"&gt;small Swift 6 lab&lt;/a&gt; around this distinction.&lt;/p&gt;

&lt;p&gt;The negative control was a deliberately illegal case: non-Sendable mutable reference state crossing a concurrency boundary.&lt;/p&gt;

&lt;p&gt;Swift 6 rejected it.&lt;/p&gt;

&lt;p&gt;Then I tested valid cases using actor isolation, &lt;code&gt;Sendable&lt;/code&gt;, and &lt;code&gt;@unchecked Sendable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Those cases compiled.&lt;/p&gt;

&lt;p&gt;If concurrency isolation also acted as a direct ARC atomicity selector, this would be a reasonable place to expect the generated reference-counting path to change.&lt;/p&gt;

&lt;p&gt;In the Apple Swift 6.3.3 cases I tested, it did not.&lt;/p&gt;

&lt;p&gt;Ordinary builds still emitted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;swift_retain&lt;/span&gt;
&lt;span class="n"&gt;swift_release&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actor-isolated and Sendable cases did not switch those observed calls to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;swift_nonatomic_retain&lt;/span&gt;
&lt;span class="n"&gt;swift_nonatomic_release&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I changed a different assumption.&lt;/p&gt;

&lt;p&gt;I compiled with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-Xfrontend -assume-single-threaded
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the observed calls changed to the non-atomic variants.&lt;/p&gt;

&lt;p&gt;That is a much cleaner signal than trying to infer ARC behaviour from the presence of &lt;code&gt;actor&lt;/code&gt; or &lt;code&gt;Sendable&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The A/B
&lt;/h2&gt;

&lt;p&gt;For the controlled retain/release case in the lab:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Build&lt;/th&gt;
&lt;th&gt;Retain&lt;/th&gt;
&lt;th&gt;Release&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;normal &lt;code&gt;-Onone&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_retain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_release&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;single-threaded &lt;code&gt;-Onone&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_nonatomic_retain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_nonatomic_release&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;normal &lt;code&gt;-O&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_retain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_release&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;single-threaded &lt;code&gt;-O&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_nonatomic_retain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_nonatomic_release&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The result is deliberately narrow.&lt;/p&gt;

&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; prove that Swift ARC is always atomic.&lt;/p&gt;

&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; prove that actor isolation can never participate in an ARC optimization.&lt;/p&gt;

&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; prove that &lt;code&gt;Sendable&lt;/code&gt; can never influence optimization.&lt;/p&gt;

&lt;p&gt;It shows that in these tested Apple Swift 6.3.3 cases, changing the concurrency validity of the program did not change the observed ARC runtime entry-point selection, while the explicit single-threaded compiler assumption did.&lt;/p&gt;

&lt;p&gt;That is compiler/runtime implementation evidence for the tested toolchain, not a Swift language guarantee.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Sendable does not close the gap
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Sendable&lt;/code&gt; is another place where the same mental shortcut appears.&lt;/p&gt;

&lt;p&gt;A type conforming to &lt;code&gt;Sendable&lt;/code&gt; is saying something about crossing concurrency boundaries safely under Swift's concurrency model.&lt;/p&gt;

&lt;p&gt;For unchecked conformance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;@unchecked&lt;/span&gt; &lt;span class="kt"&gt;Sendable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// You are taking responsibility for the safety contract.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the compiler is not proving your synchronization strategy for you.&lt;/p&gt;

&lt;p&gt;You are asserting that the type can be used safely across those boundaries.&lt;/p&gt;

&lt;p&gt;That still does not mean:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sendable
↓
non-atomic refcount
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Sendable&lt;/code&gt; is not an ARC annotation.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://docs.swift.org/latest/documentation/swift/sendable/" rel="noopener noreferrer"&gt;Swift documentation&lt;/a&gt; describes &lt;code&gt;@unchecked Sendable&lt;/code&gt; in exactly that spirit: you assume responsibility for correctness when the compiler cannot enforce the conformance itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Isolation is about access, not exclusive lifetime ownership
&lt;/h2&gt;

&lt;p&gt;The easiest way to remember the distinction is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Actor isolation controls access to isolated state. It does not mean the actor is the only place where all lifetime bookkeeping for every related reference can occur.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That difference becomes more visible when values move across APIs.&lt;/p&gt;

&lt;p&gt;An actor method can return a Sendable value.&lt;/p&gt;

&lt;p&gt;A reference may be captured by generated code.&lt;/p&gt;

&lt;p&gt;A function call may introduce ownership traffic that is not interesting at the source level.&lt;/p&gt;

&lt;p&gt;Optimization may remove, combine or reposition retain/release operations.&lt;/p&gt;

&lt;p&gt;None of that means actor isolation is broken.&lt;/p&gt;

&lt;p&gt;It means source-level isolation and runtime lifetime accounting are different layers of the implementation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this matters for performance reasoning
&lt;/h2&gt;

&lt;p&gt;This is where developers can accidentally turn a correctness feature into an imagined performance guarantee.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I moved this behind an actor, so ARC can be cheaper now.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That conclusion does not follow from the actor model.&lt;/p&gt;

&lt;p&gt;The actor may absolutely improve the correctness and architecture of the code.&lt;/p&gt;

&lt;p&gt;It may also change performance in many ways, including scheduling and serialization costs.&lt;/p&gt;

&lt;p&gt;But if your performance claim is specifically about reference-count atomicity, you need evidence from generated code or runtime behaviour.&lt;/p&gt;

&lt;p&gt;Do not infer it from &lt;code&gt;actor&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Do not infer it from &lt;code&gt;Sendable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Measure the layer you are making the claim about.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why SIL alone is not enough
&lt;/h2&gt;

&lt;p&gt;This also explains why looking at one intermediate representation can be misleading.&lt;/p&gt;

&lt;p&gt;SIL exposes ownership operations, but the question in the ARC experiment was narrower:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Which runtime ARC entry points survive into the generated program?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For that, LLVM IR and assembly are more directly useful.&lt;/p&gt;

&lt;p&gt;Optimization can eliminate or combine ARC traffic, so even the presence of an ownership operation in one stage does not guarantee a corresponding runtime call later.&lt;/p&gt;

&lt;p&gt;This is why the lab keeps SIL, LLVM IR and assembly evidence separate.&lt;/p&gt;

&lt;p&gt;The more specific your claim, the closer your evidence should be to the layer you are claiming something about.&lt;/p&gt;

&lt;h2&gt;
  
  
  A better senior-level mental model
&lt;/h2&gt;

&lt;p&gt;When reasoning about Swift concurrency and ownership, keep three layers separate:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Isolation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Who is allowed to access this state, and from which concurrency domain?&lt;/p&gt;

&lt;p&gt;Actors and global actors live here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Transfer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Can this value cross a concurrency boundary safely?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Sendable&lt;/code&gt; lives here.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Lifetime accounting&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;How does the generated program keep the referenced object alive and eventually destroy it?&lt;/p&gt;

&lt;p&gt;ARC and compiler lowering live here.&lt;/p&gt;

&lt;p&gt;These layers interact.&lt;/p&gt;

&lt;p&gt;They are not interchangeable.&lt;/p&gt;

&lt;p&gt;That is why a program can become valid under Swift 6's concurrency checker while the ARC entry points observed in generated code remain unchanged.&lt;/p&gt;

&lt;h2&gt;
  
  
  The practical rule
&lt;/h2&gt;

&lt;p&gt;If Swift 6 rejects the program, fix the concurrency problem.&lt;/p&gt;

&lt;p&gt;If you want to know what ARC is doing, inspect ARC.&lt;/p&gt;

&lt;p&gt;Do not use one as a proxy for the other.&lt;/p&gt;

&lt;p&gt;That sounds obvious when written down.&lt;/p&gt;

&lt;p&gt;It is much less obvious when an actor has just given the compiler a strong new proof and you are staring at a pair of &lt;code&gt;swift_retain&lt;/code&gt; / &lt;code&gt;swift_release&lt;/code&gt; calls that refuse to disappear.&lt;/p&gt;

&lt;p&gt;The compiler is not contradicting itself.&lt;/p&gt;

&lt;p&gt;You were asking two different questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproduce the ARC experiment
&lt;/h2&gt;

&lt;p&gt;The full lab is public:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/salari-dev/swift6-arc-atomicity-lab" rel="noopener noreferrer"&gt;https://github.com/salari-dev/swift6-arc-atomicity-lab&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the negative concurrency control;&lt;/li&gt;
&lt;li&gt;actor-isolated, &lt;code&gt;Sendable&lt;/code&gt;, and &lt;code&gt;@unchecked Sendable&lt;/code&gt; cases;&lt;/li&gt;
&lt;li&gt;SIL, LLVM IR and assembly evidence;&lt;/li&gt;
&lt;li&gt;the atomic versus non-atomic A/B;&lt;/li&gt;
&lt;li&gt;compiler-selection tracing;&lt;/li&gt;
&lt;li&gt;reproduction scripts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tested environment documented in the repo is Apple Swift 6.3.3, Xcode 26.6, macOS 26.6.2, arm64, Swift language mode 6, with iPhone 17 Simulator used for simulator validation.&lt;/p&gt;

&lt;p&gt;No second official Swift toolchain was tested, so this should not be presented as a permanent language rule.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Swift, &lt;a href="https://docs.swift.org/main/documentation/swift/actor/" rel="noopener noreferrer"&gt;Actor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Swift, &lt;a href="https://docs.swift.org/latest/documentation/swift/sendable/" rel="noopener noreferrer"&gt;Sendable&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Reproduction repository: &lt;a href="https://github.com/salari-dev/swift6-arc-atomicity-lab" rel="noopener noreferrer"&gt;salari-dev/swift6-arc-atomicity-lab&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;First published on &lt;a href="https://salari.dev/writing/swift-actor-isolation-arc-lifetime/?utm_source=devto&amp;amp;utm_medium=syndication&amp;amp;utm_campaign=swift-actor-isolation-arc-lifetime" rel="noopener noreferrer"&gt;salari.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>concurrency</category>
      <category>programming</category>
    </item>
    <item>
      <title>Swift 6 rejected the race. ARC stayed atomic.</title>
      <dc:creator>Mike Salari</dc:creator>
      <pubDate>Tue, 06 Oct 2026 15:06:00 +0000</pubDate>
      <link>https://dev.to/salari_dev/swift-6-rejected-the-race-arc-stayed-atomic-4lnh</link>
      <guid>https://dev.to/salari_dev/swift-6-rejected-the-race-arc-stayed-atomic-4lnh</guid>
      <description>&lt;p&gt;Swift 6 rejected the unsafe program exactly where I expected it to.&lt;/p&gt;

&lt;p&gt;I had a small reference-type experiment with state shared across concurrency boundaries. In Swift 6 language mode, the negative control failed with concurrency diagnostics. Good. That part was boring in the best possible way.&lt;/p&gt;

&lt;p&gt;Then I fixed the concurrency problem.&lt;/p&gt;

&lt;p&gt;I moved the state behind actor isolation. I tested &lt;code&gt;Sendable&lt;/code&gt; variants. I also tested an &lt;code&gt;@unchecked Sendable&lt;/code&gt; case where I was explicitly taking responsibility for the safety guarantee.&lt;/p&gt;

&lt;p&gt;The code compiled.&lt;/p&gt;

&lt;p&gt;Then I looked at what ARC was doing.&lt;/p&gt;

&lt;p&gt;I expected the generated code to change.&lt;/p&gt;

&lt;p&gt;It did not.&lt;/p&gt;

&lt;p&gt;In the Apple Swift 6.3.3 cases I tested, the runtime still went through the normal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;swift_retain&lt;/span&gt;
&lt;span class="n"&gt;swift_release&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Actor isolation had changed what Swift could prove about access to the program state.&lt;/p&gt;

&lt;p&gt;It had not changed the ARC runtime entry points I was observing.&lt;/p&gt;

&lt;p&gt;That was the interesting part.&lt;/p&gt;

&lt;h2&gt;
  
  
  The question
&lt;/h2&gt;

&lt;p&gt;Swift 6 has a much stronger concurrency model than earlier Swift versions.&lt;/p&gt;

&lt;p&gt;The compiler can reject code that moves non-Sendable state across isolation boundaries. Actors provide isolation. &lt;code&gt;Sendable&lt;/code&gt; describes values that can safely cross concurrency domains. Strict concurrency checking catches classes of mistakes that previously survived compilation.&lt;/p&gt;

&lt;p&gt;So I wanted to test a fairly simple question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If Swift 6 can prove that some object access is isolated from concurrent mutation, can ARC use cheaper non-atomic reference counting for that object?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That sounds plausible.&lt;/p&gt;

&lt;p&gt;If the compiler knows an object is actor-isolated, why pay synchronization costs for reference-count updates that should not race?&lt;/p&gt;

&lt;p&gt;The problem is that two different questions are being mixed together.&lt;/p&gt;

&lt;p&gt;Swift concurrency asks whether program state is being accessed or transferred safely.&lt;/p&gt;

&lt;p&gt;ARC asks how object lifetime is accounted for.&lt;/p&gt;

&lt;p&gt;Those are related, but they are not the same contract.&lt;/p&gt;

&lt;p&gt;I wanted to see where that distinction showed up in generated code rather than stopping at the language model.&lt;/p&gt;

&lt;h2&gt;
  
  
  The experiment
&lt;/h2&gt;

&lt;p&gt;I ran the experiment using &lt;strong&gt;Apple Swift 6.3.3 from Xcode 26.6&lt;/strong&gt;, explicitly compiling in Swift 6 language mode.&lt;/p&gt;

&lt;p&gt;The test environment was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Apple Swift 6.3.3
Xcode 26.6
macOS 26.6.2
arm64
Swift language mode 6
iPhone 17 Simulator for simulator validation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The repository contains a negative control plus several valid cases, including actor isolation and &lt;code&gt;Sendable&lt;/code&gt; variants.&lt;/p&gt;

&lt;p&gt;The negative control matters.&lt;/p&gt;

&lt;p&gt;Without it, seeing a successful Swift 6 build tells us almost nothing. I wanted a case where the compiler demonstrably rejected unsafe sharing, then closely related cases where the concurrency problem had actually been addressed.&lt;/p&gt;

&lt;p&gt;The unsafe case failed as expected.&lt;/p&gt;

&lt;p&gt;The valid cases compiled.&lt;/p&gt;

&lt;p&gt;Then I looked at multiple stages of compiler output.&lt;/p&gt;

&lt;p&gt;That distinction matters because SIL ownership instructions are not the same thing as the final ARC runtime calls.&lt;/p&gt;

&lt;p&gt;At the SIL level you can see ownership operations and retain/release behaviour represented in Swift's intermediate representation.&lt;/p&gt;

&lt;p&gt;But if the question is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Which runtime reference-counting entry point does this build actually use?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;then LLVM IR and assembly are the more interesting places to look.&lt;/p&gt;

&lt;p&gt;In the ordinary Swift 6 builds I tested, I continued to find:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;swift_retain&lt;/span&gt;
&lt;span class="n"&gt;swift_release&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That remained true when the concurrency model around the object changed.&lt;/p&gt;

&lt;p&gt;Actor isolation did not turn the observed calls into non-atomic ARC operations.&lt;/p&gt;

&lt;p&gt;Neither did the &lt;code&gt;Sendable&lt;/code&gt; or &lt;code&gt;@unchecked Sendable&lt;/code&gt; cases I tested.&lt;/p&gt;

&lt;h2&gt;
  
  
  Then I changed one compiler assumption
&lt;/h2&gt;

&lt;p&gt;There is a compiler option that makes this experiment much more useful:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-Xfrontend -assume-single-threaded
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The name is refreshingly direct.&lt;/p&gt;

&lt;p&gt;Instead of asking the compiler to infer from actor isolation that some individual object's reference counting can be cheaper, this flag changes a much broader assumption about the program.&lt;/p&gt;

&lt;p&gt;The compiler is being told to assume single-threaded execution.&lt;/p&gt;

&lt;p&gt;I compiled the controlled case again.&lt;/p&gt;

&lt;p&gt;This time, the output changed.&lt;/p&gt;

&lt;p&gt;Instead of the ordinary ARC runtime entry points, I observed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;swift_nonatomic_retain&lt;/span&gt;
&lt;span class="n"&gt;swift_nonatomic_release&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that happened in both the unoptimized and optimized configurations I tested.&lt;/p&gt;

&lt;p&gt;The clean A/B looked like this:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Build&lt;/th&gt;
&lt;th&gt;Retain&lt;/th&gt;
&lt;th&gt;Release&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;normal &lt;code&gt;-Onone&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_retain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_release&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;single-threaded &lt;code&gt;-Onone&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_nonatomic_retain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_nonatomic_release&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;normal &lt;code&gt;-O&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_retain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_release&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;single-threaded &lt;code&gt;-O&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_nonatomic_retain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;swift_nonatomic_release&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That gave me the selector I wanted.&lt;/p&gt;

&lt;p&gt;The same general reference-counting operation could be lowered differently when the compiler was explicitly told to assume a single-threaded program.&lt;/p&gt;

&lt;p&gt;But Swift 6 actor isolation by itself had not produced the same result in my experiment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why this distinction matters
&lt;/h2&gt;

&lt;p&gt;It would be easy to look at Swift 6 concurrency and reason like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An actor guarantees isolated access.&lt;/li&gt;
&lt;li&gt;Therefore this reference cannot be touched concurrently.&lt;/li&gt;
&lt;li&gt;Therefore ARC for this reference does not need atomic operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first statement can be valid.&lt;/p&gt;

&lt;p&gt;The jump from the first statement to the third is where things become much less obvious.&lt;/p&gt;

&lt;p&gt;Actor isolation is a source-level concurrency guarantee about access to isolated state.&lt;/p&gt;

&lt;p&gt;ARC lifetime operations may happen for references that move through code outside the isolated body, through calling conventions, temporary values, captures, ownership transfers, bridging, optimizer transformations, and other parts of the generated program.&lt;/p&gt;

&lt;p&gt;A proof that some mutable state is actor-isolated is not automatically the same thing as a whole-program proof that every relevant lifetime operation for an object can use non-atomic accounting.&lt;/p&gt;

&lt;p&gt;The experiment does not prove why every individual retain or release remains on a particular runtime path.&lt;/p&gt;

&lt;p&gt;What it does show is narrower and more useful:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In the Apple Swift 6.3.3 cases I tested, making the program concurrency-safe through actor isolation, &lt;code&gt;Sendable&lt;/code&gt;, or &lt;code&gt;@unchecked Sendable&lt;/code&gt; did not change the observed ARC calls from &lt;code&gt;swift_retain&lt;/code&gt; / &lt;code&gt;swift_release&lt;/code&gt; to their non-atomic counterparts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Explicitly switching the compiler to a single-threaded assumption did.&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;compiler/runtime implementation evidence for the tested toolchain, not a Swift language guarantee&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That distinction matters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Following the compiler
&lt;/h2&gt;

&lt;p&gt;I also traced the relevant compiler path instead of treating the runtime symbol names as magic.&lt;/p&gt;

&lt;p&gt;For the toolchain path examined in the repository, the single-threaded assumption flows roughly like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;-Xfrontend -assume-single-threaded&lt;/code&gt; (frontend option)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CompilerInvocation&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SILOptions::AssumeSingleThreaded&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SILModule::isDefaultAtomic()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;IRGen atomicity&lt;/li&gt;
&lt;li&gt;GenHeap runtime call selection&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A normal build emits &lt;code&gt;swift_retain&lt;/code&gt; / &lt;code&gt;swift_release&lt;/code&gt;. With the single-threaded assumption it emits &lt;code&gt;swift_nonatomic_retain&lt;/code&gt; / &lt;code&gt;swift_nonatomic_release&lt;/code&gt;. For an actor-isolated object or a &lt;code&gt;Sendable&lt;/code&gt; type, no rule was found: that branch does not reach IRGen.&lt;/p&gt;

&lt;p&gt;That is useful because it connects the frontend assumption to the ARC lowering decision.&lt;/p&gt;

&lt;p&gt;The interesting part is what I did not find in that path.&lt;/p&gt;

&lt;p&gt;I did not find a corresponding rule that says:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;actor-isolated object
↓
non-atomic ARC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kt"&gt;Sendable&lt;/span&gt;
&lt;span class="err"&gt;↓&lt;/span&gt;
&lt;span class="n"&gt;non&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;atomic&lt;/span&gt; &lt;span class="kt"&gt;ARC&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And the generated output from the experiment matched that.&lt;/p&gt;

&lt;p&gt;That does not mean such an optimization could never exist.&lt;/p&gt;

&lt;p&gt;It means I could not demonstrate it in these cases, with this toolchain, and the compiler path I traced points to a broader default-atomicity decision rather than a simple mapping from Swift concurrency annotations to ARC atomicity.&lt;/p&gt;

&lt;p&gt;There is one additional limitation worth stating clearly: the open-source Swift source revision inspected in the repo is not proven to be the exact source revision Apple used to build Swift 6.3.3.&lt;/p&gt;

&lt;p&gt;So the compiler-source tracing is supporting evidence, not a claim of exact source-build identity.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sendable is not an ARC annotation
&lt;/h2&gt;

&lt;p&gt;This is also a useful reminder about &lt;code&gt;Sendable&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Sendable&lt;/code&gt; is frequently discussed as if it were a general "thread safe" marker.&lt;/p&gt;

&lt;p&gt;That is too vague.&lt;/p&gt;

&lt;p&gt;Its job is about whether values can safely cross concurrency boundaries under Swift's concurrency model.&lt;/p&gt;

&lt;p&gt;It does not say:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use non-atomic reference counting for this instance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Likewise, &lt;code&gt;@unchecked Sendable&lt;/code&gt; does not unlock an ARC optimization.&lt;/p&gt;

&lt;p&gt;It tells the compiler that you are taking responsibility for a guarantee it cannot verify itself.&lt;/p&gt;

&lt;p&gt;In my tested cases, neither changed the ARC runtime calls I was looking at.&lt;/p&gt;

&lt;p&gt;That separation is important because otherwise it becomes very easy to assign performance properties to concurrency annotations that the language never promised.&lt;/p&gt;

&lt;h2&gt;
  
  
  Actor isolation is still doing real work
&lt;/h2&gt;

&lt;p&gt;None of this makes actor isolation less valuable.&lt;/p&gt;

&lt;p&gt;The negative control was rejected.&lt;/p&gt;

&lt;p&gt;The isolated version compiled.&lt;/p&gt;

&lt;p&gt;That is a meaningful change in the program.&lt;/p&gt;

&lt;p&gt;Swift 6 was enforcing a concurrency contract and preventing a class of unsafe access.&lt;/p&gt;

&lt;p&gt;What did not happen was a second, independent assumption:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Because this code is now accepted by the concurrency checker, ARC can use non-atomic lifetime accounting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Those are two different decisions.&lt;/p&gt;

&lt;p&gt;That distinction is probably the main thing I took away from the experiment.&lt;/p&gt;

&lt;p&gt;Swift's concurrency model can become more precise without every other subsystem automatically inheriting those proofs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why not just look at SIL?
&lt;/h2&gt;

&lt;p&gt;Because SIL can easily tempt you into making claims one level too early.&lt;/p&gt;

&lt;p&gt;Ownership operations in SIL tell you something about Swift's ownership representation and optimization pipeline.&lt;/p&gt;

&lt;p&gt;They do not necessarily tell you which reference-counting runtime function survives into the final generated program.&lt;/p&gt;

&lt;p&gt;Optimization makes this even more complicated.&lt;/p&gt;

&lt;p&gt;A retain may disappear. Two operations may be combined. A lifetime can be shortened or extended. Something visible at &lt;code&gt;-Onone&lt;/code&gt; may not exist at &lt;code&gt;-O&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For this question, I wanted to check SIL, then continue down to LLVM IR and assembly rather than infer the final runtime behaviour from one intermediate stage.&lt;/p&gt;

&lt;p&gt;That also makes the experiment easier to falsify.&lt;/p&gt;

&lt;p&gt;You do not have to trust my interpretation of a SIL ownership instruction.&lt;/p&gt;

&lt;p&gt;You can inspect the generated IR or assembly and search for the actual runtime symbols.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this experiment does not prove
&lt;/h2&gt;

&lt;p&gt;There are several conclusions I would not draw from this.&lt;/p&gt;

&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; prove:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Swift ARC is always atomic.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It does not prove:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Actor-isolated objects always use atomic refcounts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It does not prove:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;Sendable&lt;/code&gt; can never influence ARC optimization.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It does not prove:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Atomic reference counting protects your mutable program state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It does not prove:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;-assume-single-threaded&lt;/code&gt; is a production optimization you should enable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And it does not prove that the behaviour observed in this toolchain is a permanent Swift language rule.&lt;/p&gt;

&lt;p&gt;Those claims are much broader than the evidence.&lt;/p&gt;

&lt;p&gt;The result is specifically about the compiler configurations and source cases I tested.&lt;/p&gt;

&lt;p&gt;The defensible claim is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In these Apple Swift 6.3.3 experiments, actor isolation, &lt;code&gt;Sendable&lt;/code&gt;, and &lt;code&gt;@unchecked Sendable&lt;/code&gt; changed the concurrency validity of the program but did not change the observed ARC runtime entry points. Explicitly compiling with &lt;code&gt;-assume-single-threaded&lt;/code&gt; did.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is the claim I am comfortable publishing.&lt;/p&gt;

&lt;h2&gt;
  
  
  The practical lesson
&lt;/h2&gt;

&lt;p&gt;Most application developers will never choose ARC entry points manually.&lt;/p&gt;

&lt;p&gt;That is not really the point.&lt;/p&gt;

&lt;p&gt;The useful lesson is about how to reason about compiler guarantees.&lt;/p&gt;

&lt;p&gt;Swift 6 rejecting a data race does not mean every subsystem now gets to assume that the entire lifetime of every involved object is single-threaded.&lt;/p&gt;

&lt;p&gt;Concurrency safety is one contract.&lt;/p&gt;

&lt;p&gt;Object lifetime accounting is another.&lt;/p&gt;

&lt;p&gt;Sometimes a compiler can connect proofs across those boundaries.&lt;/p&gt;

&lt;p&gt;Sometimes it cannot, or deliberately does not.&lt;/p&gt;

&lt;p&gt;And sometimes you need to stop looking at the language feature and inspect the generated program.&lt;/p&gt;

&lt;p&gt;That is what happened here.&lt;/p&gt;

&lt;p&gt;Swift 6 rejected the race.&lt;/p&gt;

&lt;p&gt;ARC stayed on the ordinary path.&lt;/p&gt;

&lt;p&gt;Only when I explicitly told the compiler to assume a single-threaded world did the reference-counting path change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproduce it
&lt;/h2&gt;

&lt;p&gt;The full experiment is public:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/salari-dev/swift6-arc-atomicity-lab" rel="noopener noreferrer"&gt;https://github.com/salari-dev/swift6-arc-atomicity-lab&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The repository includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the Swift test cases;&lt;/li&gt;
&lt;li&gt;the negative concurrency control;&lt;/li&gt;
&lt;li&gt;generated SIL, LLVM IR and assembly evidence;&lt;/li&gt;
&lt;li&gt;the atomic versus non-atomic A/B;&lt;/li&gt;
&lt;li&gt;compiler-selection tracing;&lt;/li&gt;
&lt;li&gt;runtime-source mapping;&lt;/li&gt;
&lt;li&gt;claim auditing;&lt;/li&gt;
&lt;li&gt;simulator validation;&lt;/li&gt;
&lt;li&gt;reproduction scripts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To regenerate the compiler experiment:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./run-experiment.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For simulator validation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./run-simulator-validation.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The repository preserves the frozen evidence under &lt;code&gt;Evidence/&lt;/code&gt;, while regenerated output is written separately.&lt;/p&gt;

&lt;p&gt;The experiment currently uses one official Apple Swift toolchain. No second official Swift toolchain was installed and tested, so cross-toolchain reproduction remains unresolved.&lt;/p&gt;

&lt;p&gt;I would encourage you to run it rather than take this article on faith.&lt;/p&gt;

&lt;p&gt;Compiler behaviour is exactly the kind of thing where the interesting answer may change with a future toolchain.&lt;/p&gt;

&lt;p&gt;And if a newer Swift compiler starts lowering one of these cases differently, that would make the experiment more interesting, not less.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Swift, &lt;a href="https://docs.swift.org/main/documentation/swift/actor/" rel="noopener noreferrer"&gt;Actor&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Swift, &lt;a href="https://docs.swift.org/latest/documentation/swift/sendable/" rel="noopener noreferrer"&gt;Sendable&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Reproduction repository: &lt;a href="https://github.com/salari-dev/swift6-arc-atomicity-lab" rel="noopener noreferrer"&gt;salari-dev/swift6-arc-atomicity-lab&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;First published on &lt;a href="https://salari.dev/writing/swift-6-arc-atomicity/?utm_source=devto&amp;amp;utm_medium=syndication&amp;amp;utm_campaign=swift-6-arc-atomicity" rel="noopener noreferrer"&gt;salari.dev&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>concurrency</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
