<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Tewha.net</title>
    <link>https://tewha.net/tags/nsthread/</link>
    <description>Recent content on Tewha.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 06 Mar 2013 16:00:00 +0000</lastBuildDate>
    <atom:link href="https://tewha.net/tags/nsthread/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Check your thread</title>
      <link>https://tewha.net/2013/03/check-your-thread/</link>
      <pubDate>Wed, 06 Mar 2013 16:00:00 +0000</pubDate>
      <guid>https://tewha.net/2013/03/check-your-thread/</guid>
      <description>&lt;p&gt;When you start using Grand Central Dispatch or &lt;code&gt;NSOperation&lt;/code&gt;, you’ll want to perform some actions on the main thread and some intentionally off the main thread.&lt;/p&gt;
&lt;p&gt;This is a simple and obvious technique, but it took me a while to adopt it: You can do by asserting with &lt;code&gt;NSAssert&lt;/code&gt; or &lt;code&gt;NSCAssert&lt;/code&gt; for &lt;code&gt;[NSThread isMainThread]&lt;/code&gt;, just as you would assert any other condition.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>When you start using Grand Central Dispatch or <code>NSOperation</code>, you’ll want to perform some actions on the main thread and some intentionally off the main thread.</p>
<p>This is a simple and obvious technique, but it took me a while to adopt it: You can do by asserting with <code>NSAssert</code> or <code>NSCAssert</code> for <code>[NSThread isMainThread]</code>, just as you would assert any other condition.</p>
<p>Like this:</p>
<pre><code>- (void)updateUI {
    NSAssert([NSThread isMainThread], @&quot;Only update UI from main thread&quot;);
    // update UI
}

- (void)backgroundWork {
    NSAssert(![NSThread isMainThread], @&quot;Only do work off main thread&quot;);
    // do work
}
</code></pre>
<p>With assertions like this in place, you’ll sort out what executes where in short order.</p>
<p>I wish I had done this to my code right away; it’s much simpler to get it right than to go back and fix it all later.</p>]]></content:encoded>
    </item>
  </channel>
</rss>
