美文网首页
GCD的快速迭代

GCD的快速迭代

作者: 大风天上来 | 来源:发表于2019-05-24 20:28 被阅读0次

    GCD的快速迭代

    通常我们进行遍历, 都是使用的for循环或者while循环

    但是, 普通的循环, 是在主线程中执行的
    如果循环次数过多的话, 就会影响主线程的执行, 会导致UI界面的卡顿, 从而降低用户的体验

    GCD快速迭代(遍历)

    格式:

    dispatch_apply(<#size_t iterations#>, <#dispatch_queue_t queue#>, <#^(size_t)block#>)
    + <#size_t iterations#>: 进行迭代的次数
    + <#dispatch_queue_t queue#>: 迭代执行的队列
    + <#^(size_t)block#>: 进行迭代的代码, 注意要在block中设置一个索引i, 用来控制迭代
    

    迭代的注意点

    • 虽然快速迭代会使用子线程来进行主要任务, 但是主线程也会参与到迭代任务当中, 但是调用的频率不足以影响主线程执行他的任务

    • 注意不能使用主队列, 由于主线程也会参与迭代, 这样会造成主队列与主线程之间的死锁

    • 执行的顺序不是固定的

    • 如果使用了串行队列, 那么所有的任务都将会在主线程中进行, 这样快速迭代就没有任何的意义了

    使用场景

    - (void)testMoveFile {
        NSString *fromPath = @"/Users/hufeng/Desktop/222";
        NSString *toPath = @"/Users/hufeng/Desktop/111";
        
        NSArray *fileArray = [[NSFileManager defaultManager] subpathsAtPath:fromPath];
        NSLog(@"%@", fileArray);
        
        dispatch_queue_t queue = dispatch_queue_create("123", DISPATCH_QUEUE_CONCURRENT);
        
        dispatch_apply(fileArray.count, queue, ^(size_t i) {
            NSString *fullFromPath = [fromPath stringByAppendingPathComponent:fileArray[i]];
            NSString *fullToPath = [toPath stringByAppendingPathComponent:fileArray[i]];
            
            [[NSFileManager defaultManager] moveItemAtPath:fullFromPath toPath:fullToPath error:nil];
            
            NSLog(@"%@ --- %@ --- %@", fullFromPath, fullToPath, [NSThread currentThread]);
            
        });
    }
    
    

    链接:https://www.jianshu.com/p/bbf03a158c37

    相关文章

      网友评论

          本文标题:GCD的快速迭代

          本文链接:https://www.haomeiwen.com/subject/bfnnzqtx.html