dispatch_queue_t still blocking main thread

前端 未结 3 938
借酒劲吻你
借酒劲吻你 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:47

    I went the long way around but finally got the concept in my head and here is what is working perfectly for me:

    dispatch_queue_t newImages = dispatch_queue_create("com.mydomain.app.newimagesinbackground", NULL); // create my serial queue
            dispatch_async(newImages, ^{
         [self getNewImages]; // call my function - this get added first in my serial queue
        });
    
    
        dispatch_async(newImages, ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                // add this to the main queue as the last item in my serial queue
                // when I get to this point I know everything in my queue has been run
                dispatch_release(newImages);
            });
        });
    

    My main problem was using the ASIHTTPRequest startAsynchronous method: http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_blocks

    That in essence was creating my bottleneck, 2000 images to get, 2000 asynchronous calls trying to be made. If I am already in the background the startSynchronous method works just fine and only 1 call is trying to run at a time.

提交回复
热议问题