8

NSOperationQueue creates many threads, as you'd expect, and desire. But when you pause the app and debug it in Xcode, it's unclear which threads belong to one operation queue and which belong to another.

I've tried:

[NSThread currentThread] setName: @"My amazing operation thread"]

but as threads are reused, this just means that many threads get this name and then never lose it. I've tried setting the thread name in -start and unsetting it in -finish, but the thread names never show up in the Xcode debugging thread list.

What's a good way of naming threads/operations to make them easier to debug in Xcode?

1
  • Naming threads is meaningless when working with queues. A queue may use any thread, technically, and which thread is an implementation detail. Multiple queues may share threads, again the details are left to the opaque implementation.
    – bbum
    Mar 22, 2013 at 2:32

4 Answers 4

4

To name your NSOperationQueue, you can use:

- (void)setName:(NSString *)newName

When debugging, the name of the thread appears un light gray under the thread.

Example: (thread 3 is mine)

enter image description here

From Apple's documentation:

Discussion

Names provide a way for you to identify your operation queues at run time. Tools may also use this name to provide additional context during debugging or analysis of your code.

Xcode is one of the "tools" that uses this information to provide additional context during debugging.

8
  • 1
    I have tried this and found it not to be the case. [_operationQueue setName: @"my awesome operation queue"]; Nothing shows up in the debug threadlist. Mar 21, 2013 at 23:52
  • @NickLocking Well, I don't understand. I use it all the time… When debugging, the name of the thread appears in light gray under the thread, on the left pane in Xcode. See my example.
    – Jean
    Mar 21, 2013 at 23:53
  • You can recognize the custom-named thread at the misspelled dispatch and at the jssensors in the name. What version of Xcode are you using? I am on Version 4.6.1 (4H512).
    – Jean
    Mar 22, 2013 at 0:00
  • Latest version. The only thing I can think of is that I'm using concurrent operations. Mar 22, 2013 at 0:11
  • @NickLocking That's probably the explanation. I am using it serially. What does Xcode tell you when you select "View Process by queue" instead of "by threads" (top right of the left panel, when debugging)?
    – Jean
    Mar 22, 2013 at 0:15
3

(This answer is tested only with Xcode 10.1)

To answer the question in the body of the post (not the title), you need to set the name of the OperationQueue's underlying queue. By doing so, that queue name will show in Xcode while debugging (just like the main or global DispatchQueues).

For example:

var queue = OperationQueue()
queue.maxConcurrentOperationCount = 1
queue.qualityOfService = .background
queue.underlyingQueue = DispatchQueue(
    label: "my-queue-dispatch-queue", qos: .background)

This example will yield the following in Xcode 10.1: Example Xcode 10.1 Debug View

Reference to the underlyingQueue property: https://developer.apple.com/documentation/foundation/operationqueue/1415344-underlyingqueue

Sidenote: From experience, the "queue.qualityOfService = .SOMETHING" is necessary. See: https://bugs.swift.org/browse/SR-9742

1

Fixed the problem by doing this:

[[NSThread currentThread] setName:@"ScreenSharingProcessorThread"];

instead of:

[self setName: @"ScreenSharingProcessorThread"];

Hope this helps

0

I also found that naming the NSOperationQueue will not name the thread in Xcode during debugging.

Solution: Add an operation that sets the thread's name, and add it to the queue once after creating the queue.

NameThreadOperation.h

#import <Foundation/Foundation.h>

@interface NameThreadOperation : NSOperation

@end

NameThreadOperation.m

#import "NameThreadOperation.h"

@implementation NameThreadOperation

- (void)main
{
    @autoreleasepool
    {
        [[NSThread currentThread] setName:@"Name of the thread"];
    }
}

@end

In your ViewController.m or whatever:

operationQueue = [[NSOperationQueue alloc] init];

#if defined(DEBUG)
    [self.operationQueue addOperation:[[NameThreadOperation alloc] init]];
#endif

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.