2

I have an app that is freezing frequently and permanently. When this happens, I click pause in Xcode and see that on the main thread it's always stopping at a line of code that executes a fetch request on the MOC. I also see the output __psynch_mutexwait + 17 in the thread list on the left. This is making me assume that the app is hitting deadlock or for some reason the MOC is blocked.

My first instinct was that I might be executing a fetch request on a non-main thread so I put in logs to check, but this isn't the case. All fetches are happening on the main thread.

How can I go about tracking down what might be blocking here? Is there something more I should be looking for in the stack traces?

Is it a problem that I am setting properties of objects fetched on the main thread on other threads? ie, fetch objectA on main but then pass it to another thread and do something like objectA.someNumber = [NSNumber numberWithInt:2] ?

2 Answers 2

0

Is it a problem that I am setting properties of objects fetched on the main thread on other threads? ie, fetch objectA on main but then pass it to another thread and do something like objectA.someNumber = [NSNumber numberWithInt:2] ?

Yes! I've tried this.

When you fetch ObjA in ThreadA, and then pass it to ThreadB for some operations, it will fall into deadlock.

1
  • Yep since posting this I realised that managed objects are not thread safe. Using the MOC's performBlock method is a good way to deal with this. Jul 4, 2013 at 19:35
0

It it precicely because your fetch requests are running on the main thread that your app is blocking. Remember that the main thread is a serial queue and that no other block (or event) will run until your fetch request is done (even if in theory it could because you block is in a waiting state). This explains why when you break you always hit a _psanch_mutexwait.

You should run your fetch requests on another queue and if necessary use the result on the main queue. One way to achieve this is with the following pattern:

- (void) fetchRequest1
{
     dispatch_async(not_the_main_queue, ^(void) {

         // Code the request here.

         // Then use the result on the main if necessary.
         dispatch_async(dispatch_get_main_queue(), ^(void) {
             // Use the result on the main queue.
         });
     });
}

Also note that its often not necessary to run anything on the main queue. In fact your app will usually run more smoothly if you run as little as possible on that thread. Of course there are some things that must be done there and in those case you could use the following pattern to ensure that it is:

- (void) runBlockOnMainThread:(void(^)(void))block
{
    dispatch_queue_t thisQ = dispatch_get_current_queue();
    dispatch_queue_t mainQ = dispatch_get_main_queue();

    if (thisQ != mainQ)
        dispatch_sync(mainQ, block);
    else
        block();
}
5
  • Hi, just to be clear I don't mean the app hangs momentarily – I mean it freezes and will stay frozen forever. Unless I'm missing something it seems like this change alone wouldn't fix the deadlock problem, it would simply free up the main thread a bit? Apr 21, 2013 at 16:03
  • From what you mentioned, I made the assumption that your fetch request was the cause of the deadlock. I don't know what the fetch is waiting on but if whatever it's waiting on also runs on the main thread then it will wait forever. One way to get around this is to run your fetch request on another thread or queue. Another is to identify what the fetch request is waiting on and act on that. Apr 21, 2013 at 17:11
  • Yep I'm pretty sure it's the cause of the deadlock. Really I was hoping for some advice in tracking down what it is that it's waiting on so I can fix it that way. I'm concerned that if I start creating more threads I'll end up needing to managed multiple MOCs and it will get complicated quickly. Is that a fair assumption? Apr 21, 2013 at 18:28
  • You might consider creating your MOC in another thread than the main thread. See this answer for more details. Apr 21, 2013 at 19:51
  • Thanks for trying to help out, but this doesn't really seem to answer my original question of trying to track down what is causing deadlock. Apr 21, 2013 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.