ios - Bagaimana untuk menentukan apabila operasi tak segerak (gelung) telah tamat sepenuhnya?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-16 13:19:19
0
1
843

Keperluan: Selepas mendapat maklumat album, dapatkan gambar pertama dan fungsi self.editImageView赋值显示.
问题 : 但是我现在想在getImageForCollectionView完全结束后来为self.editImageView赋值,那么问题来了,我如何才能判断getImageForCollectionView telah selesai?

 - (void)getImageForCollectionView{
    _library = [[ALAssetsLibrary alloc] init];
    self.photos = [NSMutableDictionary dictionary];
    [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group) {
            NSMutableArray *array = [NSMutableArray array];
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                if (result) {
                    [array addObject:result];
                }
            }];
            [self.photos setValue:array forKey:[group valueForProperty:@"ALAssetsGroupPropertyName"]];
        }
    } failureBlock:^(NSError *error) {
       
    }];
}
曾经蜡笔没有小新
曾经蜡笔没有小新

membalas semua(1)
黄舟

Letakkan operasi yang memakan masa dalam urutan bukan utama, dan operasi yang memerlukan kemas kini UI dalam utas utama.

__weak typeof(self) weakSelf = self;
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // not in main block task
    [weakSelf getImageForCollectionView];
    dispatch_async(dispatch_get_main_queue(), ^{
        // main block. change ui
        NSLog(@"%@", weakSelf.photos);
    });
});

EDIT:

- (void)getImageForCollectionView:(void(^)(void))callback {
    _library = [[ALAssetsLibrary alloc] init];
    self.photos = [NSMutableDictionary dictionary];
    
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [_library enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            if (group) {
                NSMutableArray *array = [NSMutableArray array];
                [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                    if (result) {
                        [array addObject:result];
                    }
                }];
                [self.photos setValue:array forKey:[group valueForProperty:@"ALAssetsGroupPropertyName"]];
            }
        } failureBlock:^(NSError *error) {
            
        }];
        dispatch_async(dispatch_get_main_queue(), ^{
            callback();
        });
    });
}
[self getImageForCollectionView:^{
    // ...
    NSLog(@"%@", self.photos);
}];
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan