<?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 Arash Goodarzi ( Mehrdad) on Medium]]></title>
        <description><![CDATA[Stories by Arash Goodarzi ( Mehrdad) on Medium]]></description>
        <link>https://medium.com/@arash.good93?source=rss-4e1eaa8d867a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bmJ3VeNx_e8HE3L6qVRf3g@2x.jpeg</url>
            <title>Stories by Arash Goodarzi ( Mehrdad) on Medium</title>
            <link>https://medium.com/@arash.good93?source=rss-4e1eaa8d867a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 23:43:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arash.good93/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[Swift New Concurrency; Better Task Cancellations]]></title>
            <link>https://medium.com/@arash.good93/swift-new-concurrency-better-task-cancellations-bd9808ac9a26?source=rss-4e1eaa8d867a------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd9808ac9a26</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swi̇ft]]></category>
            <category><![CDATA[asyncawait]]></category>
            <category><![CDATA[cancellations]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Arash Goodarzi ( Mehrdad)]]></dc:creator>
            <pubDate>Wed, 10 Jul 2024 10:00:14 GMT</pubDate>
            <atom:updated>2024-07-10T10:00:14.206Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OwMOh-i5XvrYvcD0.png" /></figure><p>While you are dealing with Swift new concurrency APIs you may consider cancellations. For example when you have a search field, while user is typing, we throttle the input to prevent extra network calls. But there’s another point we need to be aware of. <strong>Cancellation</strong> of <strong>previous tasks</strong> for previous inputs of user to keep the output healthy.</p><p>Unless a spawn task response may receive after the last task and make unwanted behaviors in searching functionality.</p><p>Fortunately we can easily cancel tasks by keeping a reference of a task and calling its <strong>cancel() </strong>method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I261I51FDLBA2O6aThQ8-w.png" /><figcaption>not good!</figcaption></figure><p><strong>But</strong> it is a little messy. isn’t it?</p><p>Also what if we have more tasks to cancel</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzdQh1ymsY8TsvFhgb_wug.png" /><figcaption>not good at all!</figcaption></figure><p>Ouch!</p><p>So what we can do is to make something like <strong>AnyCancellable</strong> in <strong>Publishers </strong>to make it easier to read and faster to implement by a bag.</p><p>To hide its generics first make a cancellable protocol and implement it to <strong>Task</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/1*j5RJ51H6YMDILUwjezTH9A.png" /></figure><p>Then define the Bag</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bns6ndUsu3abw6KGtnajuw.png" /></figure><p>To have a better syntax make an extension like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IgsOXv_LHqSc7FfB3Utf8Q.png" /></figure><h3>Result:</h3><p>Then we have</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zsFZnP7BSFj_FI6UGlFNkg.png" /><figcaption>:)</figcaption></figure><p>The bag overrides tasks by ids we pass to. cancels previous tasks and runs another one.</p><p>That’s it. Have no more mess :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd9808ac9a26" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Annotation Injection with compile-time safety in Swift]]></title>
            <link>https://medium.com/@arash.good93/annotation-injection-with-compile-time-safety-in-swift-d9866b91ebb7?source=rss-4e1eaa8d867a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9866b91ebb7</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[macos]]></category>
            <category><![CDATA[annotations]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <dc:creator><![CDATA[Arash Goodarzi ( Mehrdad)]]></dc:creator>
            <pubDate>Fri, 05 Aug 2022 14:31:02 GMT</pubDate>
            <atom:updated>2022-08-05T15:59:46.173Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GwuVOFh-a14dZiSxGcelCQ.png" /></figure><p>Let’s say we’re all familiar with the concept of dependency injection and why we tend to consider that. Also assume that we’re on the same page not to implement <strong>DI </strong>traditionally by initializer injection, property injection and method injection.</p><p>So here we will directly ship to the other options and we won’t talk about reasons of using <strong>DI</strong>.</p><h3>Possible Choices</h3><p>After a search, possible choices would be some third-party libraries which have their owns disadvantages. For instance, run-time based tools like <a href="https://github.com/Swinject/Swinject">Swinject</a> and <a href="https://github.com/hmlongco/Resolver">Resolver</a> seems to be handy but having run-time registration process would lead to error-prone situation. After that we decide to choose something like <a href="https://github.com/uber/needle">Needle</a>, providing compile-time safety. But the problem with needle is lots of boilerplate codes and its hierarchical structure. so we may choose something else.</p><h3>What to do?</h3><p>Now we want to have our own implementation of <strong>DI </strong>providing:</p><ul><li>Compile-time safety.</li><li>Less boilerplate code</li><li>Syntactic sugar like annotations in Resolver.</li><li>No dependency to third-parties</li></ul><h3>Get Started</h3><p>suppose we have these services to inject.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c0c8bca7e4ae9877733c77c826728bb/href">https://medium.com/media/8c0c8bca7e4ae9877733c77c826728bb/href</a></iframe><p>Note that Services should be constructed by some <strong>factories</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d03021434dfbc302e0223ced050886c5/href">https://medium.com/media/d03021434dfbc302e0223ced050886c5/href</a></iframe><p>In the factories <strong>scope</strong> of the objects would be defined. for instance, we prefer <strong>NetworkService </strong>to<strong> </strong>be app-scoped like <strong>scope(.application)</strong> in Resolver and <strong>ConverterService </strong>to be created every single time it is called like <strong>scope(.unique) </strong>in Resolver.</p><blockquote>Wrapping services into factories helps to define object scopes.</blockquote><p>Then there should be a <strong>container</strong> to hold the factories. the container would be the end point which provides our services.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5c7dfc163467bf65f708e73b2aca36f/href">https://medium.com/media/c5c7dfc163467bf65f708e73b2aca36f/href</a></iframe><p>To use container in the <strong>annotations</strong> defining by swift property wrappers, we need to have accessibility to the container in the property wrappers. So a <strong>DIManager </strong>would be defined to provide the accessibility.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/579f224edfd2bf4f8dfc9a0d7914fa0f/href">https://medium.com/media/579f224edfd2bf4f8dfc9a0d7914fa0f/href</a></iframe><h3>Almost there</h3><p>Now let’s make our <strong>annotations </strong>by the<strong> DIManager </strong>as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4699cd89f80538f77bf0626b9b41129e/href">https://medium.com/media/4699cd89f80538f77bf0626b9b41129e/href</a></iframe><h3>Usage</h3><p>In the end will inject our services by the annotations in <strong>MyViewModel</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f6e1fc1a8f08b56c3ff7ecfc726e14e/href">https://medium.com/media/6f6e1fc1a8f08b56c3ff7ecfc726e14e/href</a></iframe><p>easy-peasy!</p><h3>Testing</h3><p>After all, the injected <strong>MyViewModel</strong> should be testable. To test the viewModel by a mocked service we should mock the factory of the service and pass it to the container.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/baf1628124fb184c47139798d21b4768/href">https://medium.com/media/baf1628124fb184c47139798d21b4768/href</a></iframe><p>done!</p><p>You can find the source code in this repo:</p><p><a href="https://github.com/aarashgoodarzi/Annotation-Injection">GitHub - aarashgoodarzi/Annotation-Injection: Annotation Injection with Compile-time safety</a></p><p>hope find it useful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9866b91ebb7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>