dispatch_queue_t still blocking main thread

前端 未结 3 937
借酒劲吻你
借酒劲吻你 2021-02-06 08:10

I want to fire off a method and have it run in the background - I do not care what really happens to it after it is started.

So in my main viewDidLoadMethod I have all o

3条回答
  •  不知归路
    2021-02-06 08:56

    To my understanding, ASIHTTPRequest startSynchronous requires NSRunLoop, and GCD serial queue thread doesn't have NSRunLoop. Thus, it doesn't work.

    And your code seems that GCD serial queue is not needed at all. I think GCD global queue works fine with your code. For example,

    - (void)getNewImages
    {
        dispatch_queue_t queue =
            dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
    
        /* snip */
        for (Manufacturer *m in self.manufacturers)
        {
            /* snip */
            ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
            [request setCompletionBlock:^{
                /* snip */
                dispatch_async(queue, ^{
                    [responseString writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:nil];
                });
    
                for (NSDictionary* dict in array) {
                    /* snip */
                    ASIHTTPRequest *imageRequest = [ASIHTTPRequest requestWithURL:url];
                    [imageRequest setCompletionBlock:^{
                        /* snip */
                        dispatch_async(queue, ^{
                            [responseData writeToFile:[savePath stringByReplacingOccurrencesOfString:@"%20" withString:@" "] atomically:YES];
                        });
                    }];
                    /* snip */
                    [imageRequest startAsynchronous];
                }
                /* snip */
            }];
            /* snip */
            [request startAsynchronous];
        }
    }
    

    getNewImages method should be executed on the main thread, it requires the NSRunLoop of the main thread.

提交回复
热议问题