<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Irem Dogan on Medium]]></title>
        <description><![CDATA[Stories by Irem Dogan on Medium]]></description>
        <link>https://medium.com/@doganirem96?source=rss-5e56aca54dbf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*4_4PITm5z_TaINIGe4WTeg@2x.jpeg</url>
            <title>Stories by Irem Dogan on Medium</title>
            <link>https://medium.com/@doganirem96?source=rss-5e56aca54dbf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 17:28:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@doganirem96/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Coding agents rediscover your codebase on every single run.]]></title>
            <link>https://medium.com/trendyol-tech/coding-agents-rediscover-your-codebase-on-every-single-run-ebc61ac061bf?source=rss-5e56aca54dbf------2</link>
            <guid isPermaLink="false">https://medium.com/p/ebc61ac061bf</guid>
            <category><![CDATA[benchmark]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[developer-tools]]></category>
            <category><![CDATA[ai-agent]]></category>
            <dc:creator><![CDATA[Irem Dogan]]></dc:creator>
            <pubDate>Fri, 11 Sep 2026 08:19:03 GMT</pubDate>
            <atom:updated>2026-09-11T08:19:03.513Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zZAXiiC7kRbDsUko5nTrqw.png" /></figure><p><em>Coding agents rediscover your codebase on every single run. We tried to fix that with READMEs written as navigation maps — and learned that more documentation is not reliably better documentation.</em></p><h3>TL;DR</h3><ul><li>In a large mobile repo, the first phase of an agent run is <strong>discovery</strong>, not reasoning — scanning modules, hunting for entry points, working out which abstractions are still current.</li><li>We grouped all checkout modules under one umbrella module, wrote <strong>agent-facing READMEs</strong> as navigation maps, and benchmarked three repository states: no guidance, an initial README pass, and a detailed README structure.</li><li>Planning runs got roughly <strong>40% faster</strong> with about <strong>14% fewer agent tokens</strong>. Code-understanding runs got <strong>41–47% faster</strong> — but not cheaper.</li><li>On one task, the detailed README made the run <strong>19% slower and 62% more expensive</strong> than shipping no documentation at all.</li><li>The rule that survived the benchmark: <strong>README first for navigation, source code last for truth.</strong></li></ul><h3>The discovery tax</h3><p>AI coding agents are only as effective as the context they can find. In a large mobile codebase, finding context is the expensive part.</p><p>When an agent starts a task in a repository with dozens of modules, feature flags, UI flows and service boundaries, the first phase of the run is usually not reasoning or implementation. It is discovery. The agent scans folder structures, searches for entry points, tries to work out which abstractions are current, and hopefully avoids the deprecated ones.</p><blockquote><em>Engineers do the same thing when they join a team. The difference is that an engineer pays that cost once, and an agent pays it on every run.</em></blockquote><p>We saw this pattern clearly in our own runs. A planning task in the checkout domain might need to know where a screen is assembled, which module owns the UI components, where configuration is read, how analytics events are emitted, and where navigation goes next. Without guidance, the agent relearns all of it from scratch, every time.</p><p>That creates three concrete problems:</p><ol><li><strong>Runtime goes up.</strong> The agent spends minutes orienting itself before doing anything useful.</li><li><strong>Token usage goes up.</strong> Repeated searches and exploratory file reads add up fast.</li><li><strong>Output becomes inconsistent.</strong> If the agent enters the repo through different files on different runs, similar tasks produce structurally different answers.</li></ol><p>So we asked a practical question: can repository-level README documentation make a domain-specific agent faster, cheaper and more consistent, without touching the model itself?</p><h3>Step zero: one roof before any map</h3><p>Before writing a single line of documentation, we made a structural change: every checkout-related module moved under one umbrella module. A module that used to live at :app:checkout-domain:impl became :app:checkout:checkout-domain:impl.</p><p><strong>Before — pattern-match by name</strong></p><pre>app/<br>├── basket/<br>├── checkout-data/<br>├── checkout-domain/<br>├── checkout-ui/<br>├── payment-methods/<br>├── order-summary/<br>└── … dozens more, flat</pre><p><strong>After — one entry point</strong></p><pre>app/<br>└── checkout/<br>    ├── README.md  ← agent enters here<br>    ├── checkout-data/<br>    ├── checkout-domain/<br>    ├── checkout-ui/<br>    ├── payment-methods/<br>    └── order-summary/</pre><p>The change earned its place quickly:</p><ul><li><strong>One entry point instead of a scavenger hunt.</strong> An agent — or a new engineer — that needs checkout context has exactly one place to start.</li><li><strong>A natural home for domain documentation.</strong> Without the umbrella, there is no single location that speaks for the whole domain.</li><li><strong>Cleaner ownership.</strong> Team-to-code mapping becomes a folder path, which simplifies code owners, review routing and boundary discussions.</li><li><strong>Scoped tooling.</strong> Build logic, lint rules and change-based CI are far easier to apply per domain when the domain is one subtree.</li></ul><p>The module hierarchy is the skeleton; the READMEs are the map drawn on top of it. Skipping the first step would have made the second one much weaker.</p><h3>How we measured</h3><p>We compared three repository states:</p><ul><li><strong>Baseline</strong> — the original repository, with no dedicated agent-facing README guidance.</li><li><strong>Initial README</strong> — a first documentation pass exposing module responsibilities and flow ownership.</li><li><strong>Detailed README</strong> — a richer, more structured version: entry points, flow boundaries, decision hints, deeper module detail.</li></ul><p>Every run used the same coding agent, the same model and the same settings. The only variable was the repository state.</p><p>Two suites ran against each state.</p><p><strong>The ASK suite</strong> covered code-understanding questions — 15 of them, across five categories: module structure and responsibilities, configuration and feature-flag inventories, cross-module data flows (for example, how a payment request travels from UI through domain to the network layer), domain knowledge, and deep-code verification questions that are deliberately <em>not</em> answerable from documentation alone: call-site discovery, endpoint usage, flag-driven UI behaviour, error-flow chains and test tooling. That last category exists to catch an agent that leans on the README and stops reading code.</p><p><strong>The PLAN suite</strong> covered implementation-planning tasks, where the agent produces a technical plan but writes no code. We ran three real task types against each repository state: a routine feature task from the board, a configuration-driven behaviour change, and a technical-debt item. They were chosen to cover different reasoning styles — UI placement, conditional behaviour, and cross-module refactoring — and they appear here as <strong>Case A</strong>, <strong>Case B</strong> and <strong>Case C</strong>.</p><p>Coding runs were excluded from this phase on purpose. Code-writing agents open branches and merge requests, which makes a benchmark much harder to control. We wanted to measure context quality first, with zero production side effects.</p><h3>Keeping the measurement honest</h3><p>Benchmarks are easy to get wrong, so we set two rules before running anything.</p><p>First, <strong>a completed run is not a valid run</strong>. Two early Plan results drifted onto the wrong task — the agent produced a confident plan for something we had not asked for. Averaging those in would have measured confusion, not README effectiveness. We discarded them and reran with cleaner prompts and explicit task summaries.</p><p>Second, <strong>each cell in the results is a single run</strong>. The numbers are directional signals, not statistics. The gaps below are large enough to act on, but we treat them as trends to re-verify rather than as precise constants.</p><h3>Results: the ASK suite</h3><pre>Repository state    Turns   Rel. cost   Duration   Agent tokens<br>─────────────────────────────────────────────────────────────────<br>Baseline               21       1.00×     8m 28s          211.5k<br>Initial README         32       1.15×     4m 27s          207.4k<br>Detailed README        35       1.26×     5m 01s          212.1k</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wckp7cq7th1m3zIPj-eX9A.png" /><figcaption><em>The initial README cut ASK duration from 8m 28s to 4m 27s — roughly 47% faster — and the detailed structure held at about 41% faster than baseline. Cost and turn count moved the other way. n = 1 run per cell; read as a directional signal, not a statistic.</em></figcaption></figure><p>The runtime result is clear, but the cost result is the surprising one: README-supported runs were faster without being cheaper, and both relative cost and turn count increased.</p><p>Our read is that the agent followed a README-to-code validation path — it navigated faster, but processed more context along the way. That is arguably the behaviour we asked for, since the ASK suite deliberately contains questions no README can answer.</p><p>Whether the trade is worth it depends on what you optimise for. In interactive developer workflows, latency and answer reliability usually matter more than raw token minimisation. A faster answer that points at the right files beats a cheaper answer you have to verify by hand.</p><h3>Results: the PLAN suite</h3><p>The Plan suite ran the same three cases across all three repository states — nine runs in total. The averages look tidy. The per-case behaviour is where the actual lesson lives.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kNeiCPUdDaEYwLHSwuDcJA.png" /></figure><p><em>Caption: Three tasks, one documentation change, three outcomes. Averaging these together would have hidden the entire finding. n = 1 run per cell.</em></p><h3>Case A — routine feature task</h3><pre>Repository state    Turns   Rel. cost   Duration   Agent tokens<br>─────────────────────────────────────────────────────────────────<br>Baseline               30       1.00×     9m 26s          384.5k<br>Initial README         17       0.79×     4m 14s          328.4k<br>Detailed README        13       0.51×     3m 18s          163.1k</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RwaUZpyFjmcN2pwqUAj5ow.png" /></figure><p><em>Caption: Runtime fell from 9m 26s to 3m 18s, turns from 30 to 13, and agent tokens by more than half — at roughly half the cost of the baseline run.</em></p><p>Case A is the ideal outcome. The agent found the right path quickly, explored less, and still produced a usable plan.</p><h3>Case B — configuration-driven behaviour change</h3><pre>Repository state    Turns   Rel. cost   Duration   Agent tokens<br>─────────────────────────────────────────────────────────────────<br>Baseline               19       1.00×     8m 50s          294.8k<br>Initial README         18       0.83×     3m 52s          256.4k<br>Detailed README        17       0.99×     5m 15s          313.4k</pre><p>Case B improved, then partially regressed. The initial README was the sweet spot; the detailed version stayed faster than baseline but handed back nearly the whole cost win. This is the first place where the extra structure stopped paying for itself.</p><h3>Case C — technical-debt item</h3><pre>Repository state    Turns   Rel. cost   Duration   Agent tokens<br>─────────────────────────────────────────────────────────────────<br>Baseline               13       1.00×     4m 11s          185.3k<br>Initial README         13       1.00×     4m 11s          185.3k<br>Detailed README        22       1.62×     4m 59s          269.3k</pre><p>Case C is the cautionary tale, and the reason this article exists in its current form. The task was already cheap at baseline. The detailed README made it <strong>19% slower, 45% heavier in agent tokens, and 62% more expensive</strong> — worse than shipping no documentation at all. Turn count nearly doubled, from 13 to 22.</p><p>That is the over-context risk in action. Documentation that lists more places worth checking makes the agent more thorough, and thoroughness is not free. On a task where the agent already knew where to go, every extra pointer was an invitation to look somewhere it did not need to look.</p><h3>Across the suite</h3><p>Average Plan duration dropped from about <strong>7m 29s</strong> at baseline to <strong>4m 06s</strong> with the initial README and <strong>4m 31s</strong> with the detailed one. Against baseline, the detailed state improved runtime by roughly <strong>40%</strong> and cut agent-token usage by about <strong>14%</strong>.</p><p>Those averages are real, and they are also the least useful numbers in this article. Case A and Case C nearly cancel each other out inside them.</p><h3>What earned its place in the README</h3><p>The mistake we deliberately avoided at the start was writing <em>longer</em> documentation, and the benchmark confirmed why. The best README for an agent is not the most complete one. It is the one that helps the agent decide where to look next.</p><p>In practice, agent-consumable meant four things.</p><p><strong>It is a navigation map, not a description.</strong> Instead of explaining what the domain does, the README answers “where should an agent start?” — relevant modules, canonical entry points, ownership boundaries. A good README replaces a broad repository scan with a narrow path.</p><p><strong>It separates stable architecture from task-specific detail.</strong> Module responsibilities, flow boundaries and common patterns stay useful across hundreds of runs. Task details are useful for exactly one. Keeping the second kind out of long-lived documentation also matters for security: a README should not expose unreleased behaviour or private business rules.</p><p><strong>It makes flow discovery explicit.</strong> Checkout work crosses UI, domain, data, configuration, analytics and navigation. Describing those layers lets the agent form a mental model <em>before</em> opening files — fewer blind searches, more targeted reads.</p><p><strong>It gives decision hints.</strong> Some parts of a codebase have several similar-looking paths: old flows, test-only helpers, deprecated components. A short “start here, not there” saves the agent from inspecting all of them.</p><p>Here is the same module documented both ways.</p><p><strong>Descriptive — useful to a human, once</strong></p><pre># Checkout</pre><pre>The checkout module handles the checkout process of the<br>application. It follows Clean Architecture with a domain,<br>data and presentation split, and is owned by the checkout<br>team.</pre><pre>The domain layer contains use cases for cart validation,<br>address selection and payment. The data layer talks to the<br>BFF and maps responses into domain models.</pre><p><strong>Navigational — useful to an agent, every run</strong></p><pre># Checkout — agent map</pre><pre>Start here: CheckoutActivity.kt</pre><pre>| Need            | Go to                 |<br>|-----------------|-----------------------|<br>| screen assembly | :checkout-ui:impl     |<br>| payment flow    | :checkout-domain:impl |<br>| feature flags   | CheckoutConfig.kt     |<br>| analytics       | CheckoutTracker.kt    |</pre><pre>Not here: LegacyCheckoutFragment<br>(deprecated, kept behind a rollback flag)</pre><pre>Docs are the map. Code is the truth.</pre><p>Roughly the same length. Very different behaviour on the other side of the prompt.</p><p>Case C is what happens when the second version keeps growing. So the practical version of the lesson is a keep-and-cut list, not a template.</p><p><strong>Earned its place</strong></p><ul><li>Which module owns which responsibility</li><li>Canonical entry points for common flows</li><li>Configuration keys that change behaviour</li><li>Files that must be checked before making a claim</li><li>Areas that are intentionally out of scope</li></ul><p><strong>Cost more than it returned</strong></p><ul><li>Prose restating what the code already says</li><li>Exhaustive file-by-file inventories</li><li>Task-specific detail with a one-run shelf life</li><li>Long lists of “you may also want to check…”</li><li>Anything that invites a scan instead of ending one</li></ul><p>The second list is not wrong information. It is correct information that made the agent slower.</p><p>And one guardrail turned out to matter more than anything else we wrote.</p><blockquote><strong><em>Use README and domain docs as the first navigation layer. Never as the final source of truth.</em></strong><em> Validate every claim and plan against source code — and if the README and the code disagree, trust the code and report the mismatch.</em></blockquote><p>Documentation becomes outdated. An agent that relies on a README without validating it against the code is worse than an agent with no README at all, because it will be confidently wrong. The behaviour we want is not “read more”. It is “waste less time in the wrong place”.</p><h3>Conclusion: a better map, not a better agent</h3><p>The benchmark supports continuing README-guided agent work, with a precision-first mindset. README support clearly improved latency in the ASK suite and produced strong gains in the PLAN suite, including one case where every metric improved at once.</p><p>It also exposed a real trade-off. Documentation reduces discovery cost, but unfocused documentation increases context consumption — and in the worst case makes runs slower and more expensive than having no guidance at all. That failure mode does not announce itself. It looks like a thorough agent.</p><p>The practical recommendation is boring and effective: keep READMEs short, structured and navigational. They should help agents — and new engineers, incidentally — reach the right code path sooner, not replace source-code validation.</p><blockquote><em>The strongest improvement we measured did not come from the agent getting smarter. It came from the agent getting a better map.</em></blockquote><h3>Coming in Part II</h3><p>A map is only useful while it matches the territory, and codebases move fast. Part II covers the harder half of the problem.</p><p><strong>Keeping READMEs alive.</strong> Tying documentation updates into the development flow so the map does not rot the day after it is drawn — and detecting staleness before the agent trusts it.</p><p><strong>Smarter maps, not bigger ones.</strong> Task-type-aware entry hints, trimming the sections that caused the Case C regression, and re-running the benchmark to see whether that case can be turned around.</p><p><strong>The next frontier: coding runs.</strong> What changes when the agent stops planning and starts writing code — and how to benchmark that safely.</p><p>If Part I was about giving the agent a map, Part II is about making sure the map never lies.</p><h3>Join us</h3><p>Do you want to join us on the journey of building the e-commerce platform that has the most positive impact?</p><p><a href="https://jobs.lever.co/trendyol?lever-via=reKICwwshF">Have a look at the roles we’re looking for!</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ebc61ac061bf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trendyol-tech/coding-agents-rediscover-your-codebase-on-every-single-run-ebc61ac061bf">Coding agents rediscover your codebase on every single run.</a> was originally published in <a href="https://medium.com/trendyol-tech">Trendyol Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction To Kotlin Flows]]></title>
            <link>https://medium.com/deliveryherotechhub/introduction-to-kotlin-flows-827f5a71ad7e?source=rss-5e56aca54dbf------2</link>
            <guid isPermaLink="false">https://medium.com/p/827f5a71ad7e</guid>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin-flow]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Irem Dogan]]></dc:creator>
            <pubDate>Fri, 11 Feb 2022 14:26:05 GMT</pubDate>
            <atom:updated>2022-02-11T14:26:05.342Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EGYSEGzlZFcvaPYfly9fMw.jpeg" /></figure><p>Android development, which I have been working on since the beginning of my college life, has always seemed like a huge world that is developing and changing very fast. I wanted to write an introductory article about Kotlin Flows, which has been used frequently lately and which I think will come up more often. Enjoy reading :)</p><blockquote><strong>Table of Contents</strong></blockquote><blockquote><a href="#e3c7">Flow Builders</a><br><a href="#af81">Flow Operators</a><br><a href="#74e2">Using Flows in UI</a></blockquote><p>To give a more detailed definition, flow is a Kotlin language feature that can emit multiple values sequentially over some time. For example, you can use a flow to fetch the latest updates from an API and modify them with flow operators. Before diving deeper into what you can do with flows, making small mentions about the hot/cold streams and flow builders will help you understand the flow logic.</p><p>Hot streams transmit data even if there is no subscriber when the data arrives. In cold streams, the incoming data is transmitted only if there is a subscriber. For example, let’s say you are fetching data from an API. You can do this by using channels. Channels are an example of hot stream. However, there is a point to be noted here; since there will always be data flow in hot streams, it is necessary to keep the subscribers under control. Because it can cause data losses (if the subscriber is forgotten) or even memory leaks (open network connection etc). On the other hand, cold streams start pushing values only when you start collecting. Kotlin flow is an implementation of cold stream, powered by Kotlin Coroutines. And because of Kotlin Coroutines, when you cancel the scope, you also release any running coroutines so the same rules apply to Kotlin flow as well. When you cancel the scope, you also dispose of the flow. You don’t have to free up memory manually.</p><h4><strong>Flow Builders</strong></h4><p>There are several different flow builders for creating flows. The flow{}<em> </em>builder is the most basic one. With this builder, you create a flow from the suspendable lambda block. Inside the block, you are able to do many operations.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FiX1mNkS4w%3Ffrom%3D4%26to%3D10&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FiX1mNkS4w%3Ffrom%3D4%26to%3D10&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6ddd2981ac3e7a26b8953957599437d/href">https://medium.com/media/a6ddd2981ac3e7a26b8953957599437d/href</a></iframe><p>You can use flowOf() to create a Flow from a fixed set of values.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FBnreYxme9%3Ffrom%3D4%26to%3D6&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FBnreYxme9%3Ffrom%3D4%26to%3D6&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/2403b401babd3095d3e1dcc1122298ae/href">https://medium.com/media/2403b401babd3095d3e1dcc1122298ae/href</a></iframe><p>Or you can convert various collections and sequences to a Flow using asFlow() builder.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2F8WCDMhxf5%3Ffrom%3D4%26to%3D6&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2F8WCDMhxf5%3Ffrom%3D4%26to%3D6&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/d253f0e13e4525152b337affc7b7d848/href">https://medium.com/media/d253f0e13e4525152b337affc7b7d848/href</a></iframe><h4><strong>Flow Operators</strong></h4><p>Flows can be transformed with operators, just like collections or sequences. There are two types of operators available inside the flow; <strong><em>intermediate</em></strong> and <strong><em>terminal</em></strong>.</p><p>Intermediate operators are used to modifying the data flow between the producer and the consumer in order to meet the requirements of the following layer.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2Fy22xeSN8t%3Ffrom%3D4%26to%3D19&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2Fy22xeSN8t%3Ffrom%3D4%26to%3D19&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/474410b4ab583d2587b1fe72dd3273a6/href">https://medium.com/media/474410b4ab583d2587b1fe72dd3273a6/href</a></iframe><p>In the example above, each operator creates a new flow according to its functionality. We use the filter() operator to get the numbers we need and the map() operator to transform the data to uiModel which is a better abstraction for this layer of the app. The catch()<strong> </strong>operator catches exceptions that could happen while processing items in the upstream flow. The upstream flow refers to the flow produced by the producer block and those operators called before the current one. Similarly, we can refer to everything that happens after the current operator as the downstream flow. catch() can also re-throw the exception if needed or emit new values.</p><p>Collecting flows usually happen from the UI layer as it is where we want to display the data on the screen. <strong>We need to use a </strong><a href="https://kotlinlang.org/docs/flow.html#terminal-flow-operators"><strong>terminal operator</strong></a><strong> </strong>(collect, reduce, launchIn etc.)<strong> to start listening for values.</strong> To get all the values in the stream as they are emitted, we use the collect() operator. It takes a function as a parameter that is called on every new value and it is a suspend function that needs to be executed within a coroutine.</p><p><strong>When you apply a terminal operator to a flow, the flow is created on-demand and starts emitting values.</strong> On the contrary, <strong>intermediate operators just set up a chain of operations that are executed lazily</strong> when an item is emitted into the flow. Every time collect() is called on user messages, a new flow or pipe will be created and its producer block starts refreshing the messages from the API at its own interval. We refer to this type of flows as cold flows as they are created on-demand and emit data only when they are being observed.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2Fhf-A7Qxq5%3Ffrom%3D6%26to%3D18&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2Fhf-A7Qxq5%3Ffrom%3D6%26to%3D18&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/e343d17b01377ef85cb38f072c475c07/href">https://medium.com/media/e343d17b01377ef85cb38f072c475c07/href</a></iframe><p>The above example uses the transform() and take() operators. We used the transform() operator to bring the flow to the desired form, and with the take() operator, we took the first two of the 5 numbers and printed them on the screen.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FTXnm_Wu1m%3Ffrom%3D4%26to%3D19&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FTXnm_Wu1m%3Ffrom%3D4%26to%3D19&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/60085dd2504e011f3dba6e66d1e0f9c5/href">https://medium.com/media/60085dd2504e011f3dba6e66d1e0f9c5/href</a></iframe><p>The zip() and combine() operators combine the corresponding values of the two streams. Let’s look at the example above; numbers variable is updated every 300ms while numberTexts variable is updated every 400ms. Compressing them using the zip() operator will still produce the same result, even if the results are printed every 400ms but combine() merges the most recently emitted values by each flow.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2Fj6jlsIOEb%3Ffrom%3D4%26to%3D14&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2Fj6jlsIOEb%3Ffrom%3D4%26to%3D14&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/edda8aac6fda424c3d67934fc4f5e0e8/href">https://medium.com/media/edda8aac6fda424c3d67934fc4f5e0e8/href</a></iframe><p>In this example, other operators that we encounter frequently are used. We return a repeating list from 1 to 10 as flow. The onStart() is where it is entered before the flow is collected and the onCompletion() is the place entered when the flow is completed or canceled. Each time a new value arrives, it is entered into onEach(). With distinctUntilChanged() we filter out all subsequent occurrences of the same values, and with takeWhile() we pass the values to the next operator until the given condition is met. You can go to the link <a href="https://flowmarbles.com/">here</a> to see it in more detail :)</p><h4><strong>Using Flows In UI</strong></h4><p>There are two major points to consider when using flows in UI. The first is avoiding wasting resources when the program is running in the background and the second is about changing the configuration.</p><p>When the flow isn’t visible on the screen, the UI should stop collecting data from it. There are several options for doing so, all of which are aware of the UI lifecycle. Live data or lifecycle coroutine specific APIs like repeatOnLifecycle() and flowWithLifecycle() can be used. The asLiveData() operator turns the flow into live data that only observes items while the UI is displayed on the screen. We can perform this conversion in the view model class like the code below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fcaad70061bb22276fbfbb291eb7e1b/href">https://medium.com/media/4fcaad70061bb22276fbfbb291eb7e1b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31d9efd95ba59683915a38cee72fa56e/href">https://medium.com/media/31d9efd95ba59683915a38cee72fa56e/href</a></iframe><p>However, it is not required. repeatOnLifecycle() is recommended way to collect flows from the UI layer. It is a suspend function that takes Lifecycle.State as a parameter and also it is lifecycle aware so it automatically launches a new coroutine with the block passed to it when that lifecycle reaches a specific given state. The ongoing coroutine is then canceled when the lifecycle falls below that state. As we are in the context of a coroutine, we can call collect() inside the block. repeatOnLifecycle() must be called in a coroutine because it is a suspending function. The best practice is to call this function when the lifecycle is initialized.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3360b1c6044ab889390931e421140c0/href">https://medium.com/media/e3360b1c6044ab889390931e421140c0/href</a></iframe><p>Something important to notice is that the coroutine that calls repeatOnLifecycle() won’t resume executing until the lifecycle is destroyed. If you need to collect numerous flows, use launch inside the repeatOnLifecycle() block to generate multiple coroutines.</p><p>When there is only one flow to collect, you can use the flowWithLifecycle() operator instead of repeatOnLifecycle(). When the lifecycle enters and exits the target state, this API emits items and cancels the underlying producer.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e8c37b6929b983cf0c49b4507bb8297/href">https://medium.com/media/3e8c37b6929b983cf0c49b4507bb8297/href</a></iframe><p>To mention an important point, if you collect directly from lifecycleScope.launch, the activity keeps receiving flow updates while in the background. To solve this issue, we can manually start collecting in onStart() and stop collecting in onStop() but using repeatOnLifecycle() already removes all that boilerplate code for us. launchWhenStarted is also better to launch since it suspends flow collecting while the app is running in the background.</p><p>This technique, on the other hand, keeps the flow producer running, potentially emitting things in the background that will load the memory with data that will not be displayed on the screen. Because the UI has no idea how the flow producer is constructed, it’s always wiser to play it safe and use repeatOnLifecycle() or flowWithLifecycle() to prevent collecting things and keeping the flow producer active while the UI is in the background.</p><p>While using flows on the UI side, we said that one of the two important things is configuration changes. For example, screen rotation or an update may cause a configuration change. In such use cases, we should be able to protect the current data with a buffer and share it with other collectors regardless of which lifecycle it is in. We can achieve this by using StateFlow. Even if there are no collectors, it stores the data. It’s safe to utilize with activities or fragments because you can collect multiple times from it. You could use the mutable version of stateflow to change the value at any time but that isn’t very responsive. Instead, any flow can be converted to a stateflow. The stateflow receives all updates from the upstream flows and saves the most recent value, and it can contain zero or more collectors. As a result, this is ideal for viewmodels.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf3d847f81921837bb2bde39bdd5b2f4/href">https://medium.com/media/bf3d847f81921837bb2bde39bdd5b2f4/href</a></iframe><p>You can use the stateIn() operator to convert a flow to a stateflow.<br>It has three parameters: initialValue since a stateflow must always have a value, scope which controls when the sharing begins, and started which takes the value of how much time is required before upstream flows are killed.</p><p>Let’s take a few examples to make it more clear. Consider the scenarios of rotation and navigation to the home screen. The activity is briefly removed and then regenerated when the screen is rotated. We don’t want to restore any flows in this scenario because we want to complete the changeover as quickly as possible. However, in order to save battery and other resources, we aim to stop all flows on the way back home screen. We set a timeout using the parameter started to determine which one is which. We don’t immediately stop all upstream flows when a stateflow stops being collected. Instead, we wait a set amount of time, such as 5 seconds. No upstream flows are canceled if the flow is collected again before the timeout. That is precisely what the WhileSubscribed() configuration achieves. When the view is turned off, it takes five seconds for the upstream flows to be turned off and if the timeout expires, the upstream flows are terminated.</p><p>We have come to the end of my article. I tried to give some detailed basic information about Kotlin Flows. I hope you had a good time reading it :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=827f5a71ad7e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/deliveryherotechhub/introduction-to-kotlin-flows-827f5a71ad7e">Introduction To Kotlin Flows</a> was originally published in <a href="https://medium.com/deliveryherotechhub">Delivery Hero Tech Hub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>