title: Finding NSManagedObject inside of NSOperation
body: <p>I thought that <code>[moc existingObjectWithID:self.objectID error:&amp;error]</code> was the right way to find an existing object inside of a thread, given its <code>objectID</code>, but I can't get it to work, why?</p>
  
  <h2>Background / Code</h2>
  
  <p>I've got an <code>NSManagedObject</code> which is already saved. I can see all its properties on the main thread. I'm passing the object to my <code>NSOperation</code> in a custom <code>init</code> method and in that method, I'm saving its <code>objectID</code>:</p>
  
  <pre><code>- (instancetype)initWithEntity:(NSManagedObject *)object
  {
      self = [super init];
      if (self) {
          self.objectID = object.objectID;
          ...
      }
      return self;
  }
  </code></pre>
  
  <p>Then in the <code>main</code> function of my operation I'm looking up my object so that I can use it.</p>
  
  <pre><code>- (void)main
  {
      if (self.isCancelled) { return; }
  
      NSManagedObjectContext *moc = [NSManagedObjectContext MR_context];
  
      NSError *error = nil;
      self.object = [moc existingObjectWithID:self.objectID error:&amp;error];
  
      if (error) {
          DDLogError(@"error finding object: %@", error);
      }
  
      ...
  }
  </code></pre>
  
  <p><code>MR_context</code> is a MagicalRecord method which simply sets up a new context with concurrency type <code>NSPrivateQueueConcurrencyType</code> with its parent set to the root saving context. All of that looks correct to me.</p>
  
  <h2>Errors</h2>
  
  <p>When no debugger is set, I simply get a <code>fault</code> for the object. If I try to look up any property on it, I get <code>nil</code>.</p>
  
  <p>With the debugger turned on and <code>-com.apple.CoreData.ConcurrencyDebug 1</code> set I get some errors:</p>
  
  <p>In the simulator I get <code>EXC_BAD_INSTRUCTION (code=EXC_i386_INVOP, subcode=0x0)</code> on the <code>[moc existingObjectWithID:self.objectID error:&amp;error]</code> line.</p>
  
  <p>On the device, I get the following on that same line:</p>
  
  <pre><code>CoreData`+[NSManagedObjectContext __Multithreading_Violation_AllThatIsLeftToUsIsHonor__]:
  -&gt;  0x185946614 &lt;+0&gt;: brk    #0x1
  </code></pre>
  
  <p>So obviously something is wrong, but the <code>objectID</code> isn't temporary, it is the same <code>objectID</code> I see when inspecting the real object on the main thread. So I'm not sure why it isn't working.</p>
  
  <p>I've also tried:</p>
  
  <pre><code>self.object = [moc objectWithID:self.objectID];
  </code></pre>
  
  <p>But that doesn't work either. Any ideas?</p>
  
  <h2>Updates</h2>
  
  <p>2015-07-10 - Ok, if I use a block with the <code>moc</code> then I can properly access the object from within that block.</p>
  
  <pre><code>[moc performBlock:^{
      NSError *error = nil;
      NSManagedObject *o = [moc existingObjectWithID:self.objectID error:&amp;error];
  }];
  </code></pre>
  
  <p>How do I properly extract that object so I can have access to it in the thread that the operation is running? Doing a <code>__block</code> property and then setting it in the block and using it out of the block failed.</p>
  
  <p>Is this an issue with the way the <a href="https://github.com/magicalpanda/MagicalRecord/blob/master/Docs/Threads.md#creating-custom-contexts" rel="nofollow">context</a> is setup?</p>