小编Kir*_*row的帖子

任何使dispatch_queue_t在单线程中工作的方法?

这是我的代码:

@interface MyObject ()
@property(nonatomic) dispatch_queue_t queue;
@end

@implementation MyObject {
    NSThread *_check;
}

- (id)init {
    self = [super init];
    if (self) {
        _queue = dispatch_queue_create("com.Thread.queue", NULL);
        dispatch_async(_queue, ^{
            _check = [NSThread currentThread]; //for ex. thread number = 3
            //some code here...
        });
    }

    return self;
}

- (void)someMethod:(MyObjClass *)obj {
    dispatch_async(_queue, ^{
        //need th
        if (_check != [NSThread currentThread]) { // it is sometimes number 3, but sometimes it changes
            NSLog(@"Thread changed.");
        }
        [obj doSmth]; //got crash if …
Run Code Online (Sandbox Code Playgroud)

nsthread nsoperationqueue grand-central-dispatch ios

3
推荐指数
1
解决办法
1847
查看次数