I'm developing iOS framework with Objective C.
I create a dispatch_queue_t
by using dispatch_queue_create.
And call CFRunLoopRun()
for run the Runloop in the queue.
But, It looks like the dispatch_queue_t
has share the RunLoop. Some classes has add an invalid timer, and when I call the CFRunLoopRun(),
It crashed on my side.
Sample code:
- (void)viewDidLoad {
[super viewDidLoad];
self.queue1 = dispatch_queue_create("com.queue1", DISPATCH_QUEUE_CONCURRENT);
self.queue2 = dispatch_queue_create("org.queue2", DISPATCH_QUEUE_CONCURRENT);
}
- (IBAction)btnButtonAction:(id)sender {
dispatch_async(self.queue1, ^{
NSString *runloop = [NSString stringWithFormat:@"%@", CFRunLoopGetCurrent()];
runloop = [runloop substringWithRange:NSMakeRange(0, 22)];
NSLog(@"Queue1 %p run: %@", self.queue1, runloop);
//NSTimer *timer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:1 target:self selector:@selector(wrongSeletor:) userInfo:nil repeats:NO];
//[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
});
dispatch_async(self.queue2, ^{
NSString *runloop = [NSString stringWithFormat:@"%@", CFRunLoopGetCurrent()];
runloop = [runloop substringWithRange:NSMakeRange(0, 22)];
NSLog(@"Queue2 %p run: %@", self.queue2, runloop);
CFRunLoopRun();
});
}
Some time they take same RunLoop:
https://i.stack.imgur.com/wGcv3.png
=====
You can see the crash by uncomment the code of NSTimer.
The NSTimer
has been added in queue1, but it still running when call CFRunLoopRun()
in queue2.
I have read some description like: https://stackoverflow.com/questions/38000727/need-some-clarifications-about-dispatch-queue-thread-and-nsrunloop
They told that: "system creates a run loop for the thread". But, in my check, they are sharing the RunLoop.
This is sad for me, because I facing that crashes happen when calling CFRunLoopRun() on production.
Can someone take a look at this.