Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Threading issue acts differently on ios 7

Tags:

ios

iphone

I am using this Project from github, it is an image picker. I have had to make a very small change since ios7 to make the preview images from your albums show again but with that change now when you leave the picker and come back into it the photos selected (2/5) resets to 0/5 even though I have photos selected. How can I fix this?

The dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0) seems to be taking forever to update the ui even with dispatch_async(dispatch_get_main_queue() to reload the ui inside of it. When I comment out the dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0) the pictures load instantly but other things get broken that depend on the queue.

here is the code snippet with the dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0) I changed with the code i changed commented out

AGIPCAssetsController.m:

- (void)loadAssets
{
    [self.assets removeAllObjects];

    __ag_weak AGIPCAssetsController *weakSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

        __strong AGIPCAssetsController *strongSelf = weakSelf;

        @autoreleasepool {
            [strongSelf.assetsGroup enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {

                if (result == nil) 
                {
                    return;
                }

                AGIPCGridItem *gridItem = [[AGIPCGridItem alloc] initWithImagePickerController:strongSelf.imagePickerController asset:result andDelegate:strongSelf];
                if ( strongSelf.imagePickerController.selection != nil && 
                    [strongSelf.imagePickerController.selection containsObject:result])
                {
                    gridItem.selected = YES;
                }
                [strongSelf.assets addObject:gridItem];
            }];
        }

        dispatch_async(dispatch_get_main_queue(), ^{

            [strongSelf reloadData];

        });

    });
    [strongSelf reloadData];

}
like image 341
BluGeni Avatar asked Sep 23 '13 19:09

BluGeni


1 Answers

AGIPCGridItem is subclass of UIView. Don't work with UIKit objects on background thread.

Make sure you need the background thread and if you do, put only heavy tasks to background. Creating an UIView should not be that case.

Also, it's not recommended to use PRIORITY_LOW use simple PRIORITY_DEFAULT.

Edit: If you are curious why it did work on iOS 6: That's implementation detail of UIKit. It still was wrong, but somehow did what you expected.

like image 77
Tricertops Avatar answered Oct 29 '22 21:10

Tricertops