Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 738 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check whether or not the current thread is the main thread

#11
void ensureOnMainQueue(void (^block)(void)) {

if ([[NSOperationQueue currentQueue] isEqual:[NSOperationQueue mainQueue]]) {

block();

} else {

[[NSOperationQueue mainQueue] addOperationWithBlock:^{

block();

}];

}

}

note that i check the operation queue, not the thread, as this is a more safer approach

Reply

#12
Have a look at the [`NSThread` API documentation][1].

There are methods like

`- (BOOL)isMainThread`

`+ (BOOL)isMainThread`

and `+ (NSThread *)mainThread`


[1]:

[To see links please register here]

Reply

#13
## Details ##

- Swift 5.1, Xcode 11.3.1

## Solution 1. Detect any queue ##

[Get current DispatchQueue?][1]

## Solution 2. Detect only main queue ##

import Foundation

extension DispatchQueue {

private struct QueueReference { weak var queue: DispatchQueue? }

private static let key: DispatchSpecificKey<QueueReference> = {
let key = DispatchSpecificKey<QueueReference>()
let queue = DispatchQueue.main
queue.setSpecific(key: key, value: QueueReference(queue: queue))
return key
}()

static var isRunningOnMainQueue: Bool { getSpecific(key: key)?.queue == .main }
}

## Usage ##

if DispatchQueue.isRunningOnMainQueue { ... }

## Sample ##

func test(queue: DispatchQueue) {
queue.async {
print("--------------------------------------------------------")
print("queue label: \(queue.label)")
print("is running on main queue: \(DispatchQueue.isRunningOnMainQueue)")
}
}

test(queue: DispatchQueue.main)
sleep(1)
test(queue: DispatchQueue.global(qos: .background))
sleep(1)
test(queue: DispatchQueue.global(qos: .unspecified))

## Result (log) ##

--------------------------------------------------------
queue label: com.apple.root.background-qos
is running on main queue: false
--------------------------------------------------------
queue label: com.apple.root.default-qos
is running on main queue: false
--------------------------------------------------------
queue label: com.apple.main-thread
is running on main queue: true

[1]:

[To see links please register here]


Reply

#14
Here is a way to detect what the current queue is
extension DispatchQueue {
//Label of the current dispatch queue.
static var currentQueueLabel: String { String(cString: __dispatch_queue_get_label(nil)) }

/// Whether the current queue is a `NSBackgroundActivityScheduler` task.
static var isCurrentQueueNSBackgroundActivitySchedulerQueue: Bool { currentQueueLabel.hasPrefix("com.apple.xpc.activity.") }

/// Whether the current queue is a `Main` task.
static var isCurrentQueueMainQueue: Bool { currentQueueLabel.hasPrefix("com.apple.main-thread") }
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through