<?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: Sadra Isapanah Amlashi</title>
    <description>The latest articles on DEV Community by Sadra Isapanah Amlashi (@sadra).</description>
    <link>https://dev.to/sadra</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F30885%2F792b335d-862b-477b-8e64-79039b8714da.jpeg</url>
      <title>DEV Community: Sadra Isapanah Amlashi</title>
      <link>https://dev.to/sadra</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sadra"/>
    <language>en</language>
    <item>
      <title>Mastering in Javascript Closures, Grandma Style + Tips &amp; Tricks</title>
      <dc:creator>Sadra Isapanah Amlashi</dc:creator>
      <pubDate>Fri, 14 Apr 2023 07:52:01 +0000</pubDate>
      <link>https://dev.to/sadra/mastering-in-javascript-closures-grandma-style-tips-tricks-2iam</link>
      <guid>https://dev.to/sadra/mastering-in-javascript-closures-grandma-style-tips-tricks-2iam</guid>
      <description>&lt;p&gt;Closures are a powerful feature in JavaScript that allows a function to retain access to its lexical environment, even after the function has returned. They are used extensively in modern JavaScript programming and can be a bit tricky to understand and use effectively.&lt;/p&gt;

&lt;p&gt;In this post, we’ll dive deep into closures and explore some tips, tricks, and best practices for mastering them in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Explain it to Grandma
&lt;/h2&gt;

&lt;p&gt;Imagine you have a box with some things inside. This box represents a function in JavaScript. When you call that function, you can use the things inside the box.&lt;/p&gt;

&lt;p&gt;But what if there’s something outside the box that the function needs to use? Usually, the function wouldn’t be able to see it.&lt;/p&gt;

&lt;p&gt;This is where closures come in. A closure is like a little bubble that forms around the function, including anything outside the box that the function needs to use. So even though the function can’t see it directly, the closure allows it to access those things and use them as if they were inside the box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuz9q8nb2seu8ba8edb05.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuz9q8nb2seu8ba8edb05.png" alt="Box in the Bubble"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In short, a Closure is a way for a function to remember and access its outer scope even after it has been called and executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Let’s say we have a function called greet that takes a name as an argument and returns a function that will greet that person. The returned function will also remember the original name even after greet has finished running. Here's what the code might look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&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="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Grandma&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// logs "Hello, Grandma!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, greet is a function that takes an name argument and returns a function that logs a greeting to the console. When we call greet('Grandma'), it returns a new function that greets "Grandma". We store this returned function in a variable called greeting.&lt;/p&gt;

&lt;p&gt;When we call greeting() later on, it logs "Hello, Grandma!" to the console. But how does it remember the name "Grandma" even though greet has already finished running?&lt;/p&gt;

&lt;p&gt;This is where the closure comes in. The returned function has a closure that includes the name variable from the outer greet function. So even though greet has finished running and the name variable should no longer be accessible, the closure allows the returned function to remember and use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Closures Effectively
&lt;/h2&gt;

&lt;p&gt;Now that we have a basic understanding of closures, let’s look at some tips and tricks for using them effectively in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tip 1: Avoid Creating Unnecessary Closures
&lt;/h3&gt;

&lt;p&gt;Creating a closure can have performance implications, especially if it is done unnecessarily. In some cases, you can avoid creating closures by passing variables as function arguments, rather than accessing them through a Closure.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tip 2: Use Closures for Data Privacy
&lt;/h3&gt;

&lt;p&gt;One of the most powerful uses of closures is for data privacy. By creating a closure around a function, you can ensure that the variables and parameters within the closure are only accessible to that function. This can be useful for creating modules and other encapsulated components.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tip 3: Be Careful with Shared Variables
&lt;/h3&gt;

&lt;p&gt;When using closures, it’s important to be careful with shared variables. If multiple closures reference the same variable, changes made to that variable in one closure will be visible in all other closures that reference it. This can lead to unexpected behaviour if you’re not careful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tip 4: Use Closures for Memoization
&lt;/h3&gt;

&lt;p&gt;Another powerful use of closures is for memoization. Memoization is a technique for caching the results of expensive function calls so that they can be returned quickly on subsequent calls. By using a closure to store the cached results, you can avoid the need to recalculate the results each time the function is called.&lt;/p&gt;

&lt;h2&gt;
  
  
  Best Practices for Using Closures
&lt;/h2&gt;

&lt;p&gt;Finally, let’s look at some best practices for using closures in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best Practice 1: Keep Closures Simple
&lt;/h3&gt;

&lt;p&gt;Closures can be complex and difficult to understand, especially if they are nested or used in complex ways. As a best practice, it’s a good idea to keep closures as simple as possible, with a clear and understandable purpose.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best Practice 2: Use Descriptive Variable Names
&lt;/h3&gt;

&lt;p&gt;When using closures, it’s important to use descriptive variable names. This can make it easier to understand what the closure is doing and how it’s being used.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best Practice 3: Avoid Memory Leaks
&lt;/h3&gt;

&lt;p&gt;Finally, it’s important to be careful with closures to avoid memory leaks. Because closures can retain references to variables and objects, they can also prevent those variables and objects from being garbage collected. To avoid this, make sure to clean up any unnecessary closures when they are no longer needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Closures are a powerful feature in JavaScript that can be used in a variety of ways. By understanding how they work and following some best practices, you can master closures and use them effectively in your JavaScript programs. Remember to keep closures simple, use descriptive variable names, and be careful with shared variables to avoid unexpected behaviour. With these tips, tricks, and best practices, you’ll be well on your way to mastering closures in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s Connect
&lt;/h2&gt;

&lt;p&gt;Thanks for reading my article. If you like my content, please consider &lt;a href="https://www.buymeacoffee.com/sadraa" rel="noopener noreferrer"&gt;buying me a slice of Pizza 🍕&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;Connect to me on &lt;a href="https://www.linkedin.com/in/amlashisadra/" rel="noopener noreferrer"&gt;Linkedin&lt;/a&gt; and &lt;a href="https://twitter.com/sadra_me" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is the best comment service for a hosted website?</title>
      <dc:creator>Sadra Isapanah Amlashi</dc:creator>
      <pubDate>Wed, 02 May 2018 22:42:11 +0000</pubDate>
      <link>https://dev.to/sadra/what-is-the-best-comment-service-for-a-hosted-website-35h9</link>
      <guid>https://dev.to/sadra/what-is-the-best-comment-service-for-a-hosted-website-35h9</guid>
      <description>&lt;p&gt;These days I'm looking for a suitable online comment service. I've tried out many services like &lt;a href="https://disqus.com/"&gt;Disqus&lt;/a&gt; &amp;amp; &lt;a href="https://just-comments.com/"&gt;just-comments&lt;/a&gt;. But both of theme has their own problems &amp;amp; I can't feel comfortable with them!&lt;br&gt;
I've made a blog on GitHub pages with Jekyll, so I need an online comment service for my static website. Can you tell me what you use? or do you know any other free online comment service?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>blog</category>
      <category>blogging</category>
    </item>
    <item>
      <title>How to see top posts in a category?</title>
      <dc:creator>Sadra Isapanah Amlashi</dc:creator>
      <pubDate>Fri, 19 Jan 2018 19:08:37 +0000</pubDate>
      <link>https://dev.to/sadra/how-to-see-top-posts-in-a-category-3eho</link>
      <guid>https://dev.to/sadra/how-to-see-top-posts-in-a-category-3eho</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjs3vlgbtrl0zrfgns7aq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjs3vlgbtrl0zrfgns7aq.jpg" alt="TopTen" width="600" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I want to see the top post (measured by viewed and liked) of day, week, month &amp;amp; year in a specific category (e.g. Nodejs or Android). but I can't find any tool for that.&lt;/p&gt;

&lt;p&gt;How can I do it in dev.to ?&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to detect when app moves to background or foreground</title>
      <dc:creator>Sadra Isapanah Amlashi</dc:creator>
      <pubDate>Thu, 24 Aug 2017 10:07:55 +0000</pubDate>
      <link>https://dev.to/sadra/how-to-detect-when-app-moves-to-background-or-foreground</link>
      <guid>https://dev.to/sadra/how-to-detect-when-app-moves-to-background-or-foreground</guid>
      <description>&lt;p&gt;As we know, there is implemented some methods for triggering an application's lifecycle:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8fhf1hxfnou6cti51o9m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8fhf1hxfnou6cti51o9m.png" alt="iOS Application Lifecycle" width="800" height="758"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is an issue. These methods didn't help us when the app moves to background or foreground! it just shows us the one-time creation of UIViewControl or process of destroying.&lt;/p&gt;

&lt;p&gt;But as the guides in &lt;a href="https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforHandlingAppStateTransitions/StrategiesforHandlingAppStateTransitions.html" rel="noopener noreferrer"&gt;docs&lt;/a&gt; there is more information about application lifecycle.&lt;/p&gt;

&lt;p&gt;When your app goes to background, The Cocoa broadcasts a notification with the message of the app is moving to the background. So, if your app or your UIViewController registered for this notification, you can be aware when your app moves to background:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1trvw6sxme3kkrtiqzy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1trvw6sxme3kkrtiqzy.png" alt="iOS Application Moves to Background" width="800" height="839"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As the same, for Moving to Foreground, we have a notification too:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferebzrmb6s0hdao8636a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ferebzrmb6s0hdao8636a.png" alt="iOS Application Moves to Foreground" width="800" height="528"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, you have two ways to detect when your app moves to background or foreground:&lt;/p&gt;

&lt;h3&gt;
  
  
  In AppDelegate
&lt;/h3&gt;

&lt;p&gt;That implemented by default in app delegation, and you can use these default methods for the issue:&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;applicationDidEnterBackground&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;application&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIApplication&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"applicationDidEnterBackground"&lt;/span&gt;&lt;span class="p"&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;applicationWillEnterForeground&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;application&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIApplication&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"applicationWillEnterForeground"&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;h3&gt;
  
  
  In UIViewController
&lt;/h3&gt;

&lt;p&gt;If you want to detect it in a UIViewController manually, you should register for the &lt;code&gt;UIApplicationWillEnterForeground&lt;/code&gt; or &lt;code&gt;UIApplicationDidEnterBackground&lt;/code&gt; notification anywhere in your app. That it's triggered when the user pressed the home button and it moves to the background, and then pressed double and choose app so it moves to the foreground.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;foreground
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&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;notificationCenter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;NotificationCenter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;
    &lt;span class="n"&gt;notificationCenter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;#selector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;appMovedToBackground&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Notification&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;UIApplicationWillEnterForeground&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;object&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&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;appMovedToForeground&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"App moved to ForeGround!"&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;ul&gt;
&lt;li&gt;Background
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;viewDidLoad&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;viewDidLoad&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;notificationCenter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;NotificationCenter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;
    &lt;span class="n"&gt;notificationCenter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;#selector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;appMovedToBackground&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Notification&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;UIApplicationDidEnterBackground&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;object&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&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;appMovedToBackground&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"App moved to Background!"&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;Did this solution work for you? Please pass it on! &lt;strong&gt;&lt;a href="https://twitter.com/share" rel="noopener noreferrer"&gt;Tweet&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

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