zoukankan      html  css  js  c++  java
  • For循环执行AFNetworking请求

    屏幕快照 2017-12-19 下午1.46.25.png

    需求:如下操作打印的文档为

    NSLog(@"开始");for(NSIntegeri =0; i <5; i++) {        [RequestUtil requestFavoriteGroup:appDelegate.userInfo.uid andBlock:^(NSArray*modelArr,NSError*error) {NSLog(@"第一层的循环请求,i=%ld",i);for(NSIntegerj =0; j <3; j++) {                [RequestUtil requestFavoriteGroup:appDelegate.userInfo.uid andBlock:^(NSArray*modelArr,NSError*error) {NSLog(@"第二层的循环请求,i=%ld,j=%ld",i,j);                }];            }        }];    }NSLog(@"结束");

    开始

    第一层的循环请求,i=0

    第二层的循环请求,i=0,j=0

    第二层的循环请求,i=0,j=1

    第二层的循环请求,i=0,j=2

    第一层的循环请求,i=1

    第二层的循环请求,i=1,j=0

    第二层的循环请求,i=1,j=1

    第二层的循环请求,i=1,j=2

    第一层的循环请求,i=2

    第二层的循环请求,i=2,j=0

    第二层的循环请求,i=2,j=1

    第二层的循环请求,i=2,j=2

    第一层的循环请求,i=3

    第二层的循环请求,i=3,j=0

    第二层的循环请求,i=3,j=1

    第二层的循环请求,i=3,j=2

    第一层的循环请求,i=4

    第二层的循环请求,i=4,j=0

    第二层的循环请求,i=4,j=1

    第二层的循环请求,i=4,j=2

    结束

    一种写法:

    dispatch_async(dispatch_get_global_queue(0,0), ^{NSLog(@"开始");        dispatch_semaphore_t sema = dispatch_semaphore_create(0);for(NSIntegeri =0; i <5; i++) {            [RequestUtil requestFavoriteGroup:@"8130d4174c2353af"andBlock:^(NSArray*modelArr,NSError*error) {NSLog(@"第一层的循环请求,i=%ld",i);dispatch_async(dispatch_get_global_queue(0,0), ^{                    dispatch_semaphore_t sema2 = dispatch_semaphore_create(0);for(NSIntegerj =0; j <3; j++) {                        [RequestUtil requestFavoriteGroup:appDelegate.userInfo.uid andBlock:^(NSArray*modelArr,NSError*error) {NSLog(@"第二层的循环请求,i=%ld,j=%ld",i,j);                            dispatch_semaphore_signal(sema2);                        }];                        dispatch_semaphore_wait(sema2, DISPATCH_TIME_FOREVER);                    }                    dispatch_semaphore_signal(sema);                });                            }];            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);        }NSLog(@"结束");    });

  • 相关阅读:
    Sql题目精选练习
    SqlServer2008数据库的备份与还原
    SqlServer视图介绍以及创建方式
    Java反射详解
    for(foo('a') ; foo('b') && (i<2);foo('c'))的执行结果
    jdk与jre与jdk都是干什么的有什么区别和jvm详解:
    List去重为什么要写equals(),hashCode()方法
    String类的常用方法以及知识点总结
    o enclosing instance of type ArrayList_day02 is accessible. Must qualify the allocation with an enclosing instance of type ArrayList_day02
    JAVA整理01--面向对象基础
  • 原文地址:https://www.cnblogs.com/striveLD/p/9953734.html
Copyright © 2011-2022 走看看