Skip to content

Instantly share code, notes, and snippets.

@tailang
Created July 31, 2017 08:12
Show Gist options
  • Save tailang/3dfe115abf0545dee6005af9975c7155 to your computer and use it in GitHub Desktop.
Save tailang/3dfe115abf0545dee6005af9975c7155 to your computer and use it in GitHub Desktop.
//实现
/// Network thread entry point.
+ (void)_networkThreadMain:(id)object {
    @autoreleasepool {
        [[NSThread currentThread] setName:@"com.xxx.xxx"];
        NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
        [runLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
        [runLoop run];
    }
}

/// Global image request network thread, used by NSURLConnection delegate.
+ (NSThread *)_networkThread {
    static NSThread *thread = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        thread = [[NSThread alloc] initWithTarget:self selector:@selector(_networkThreadMain:) object:nil];
        if ([thread respondsToSelector:@selector(setQualityOfService:)]) {
            thread.qualityOfService = NSQualityOfServiceBackground;
        }
        [thread start];
    });
    return thread;
}

//调用
[self performSelector:@selector(_didReceiveImageFromDiskCache:) onThread:[self.class _networkThread] withObject:image waitUntilDone:NO];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment