在继续使用Func之前等待网络连接完成?

时间:2017-07-11 10:00:06

标签: ios objective-c networking

我有一个恢复功能,可以在收集应用的更新参数之前重新建立网络连接。

问题是它似乎试图在重新建立网络链接之前获取一些参数。

有没有办法可以暂停直到建立连接?

getParameters func位于后台线程上,使用:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void) { //code }

initConnection是主线程

这是func:

- (void)screenUnlocked {
    [self initConnection];

    CDCChannelCollectionView *scrollView;

    if ([self channelView]) {
        scrollView = [[self channelView] scrollView];
        for (CDCChannelStrip* strip in [scrollView visibleCells]) {
            [self getParameters:[NSNumber numberWithInteger:strip.channelNumber]];
        }
    }
}

编辑:

这样的东西?

- (void)initConnection: completion:(void (^)(void))completionBlock {
    if (![self isDemo]) {
        [self setTcpControl:[[CDCControl alloc] initWithAddress:[self IPAddress] usingProtocol:TCP]];
        [[self tcpControl] setDelegate:self];
        [self setUdpControl:[[CDCControl alloc] initWithAddress:[self IPAddress] usingProtocol:UDP]];
        [[self udpControl] setDelegate:self];

        if (successful) {
            completionBlock();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

对于块语法,您可以按照:

//Block funtion with void block return type.
- (void)initConnection:(void(^)(void))completionBlock {

    if (![self isDemo]) {
        [self setTcpControl:[[CDCControl alloc] initWithAddress:[self IPAddress] usingProtocol:TCP]];
        [[self tcpControl] setDelegate:self];
        [self setUdpControl:[[CDCControl alloc] initWithAddress:[self IPAddress] usingProtocol:UDP]];
        [[self udpControl] setDelegate:self];

        if (successful) {
            //call completion when connection is made. 
            completionBlock();
        }
    }
}

调用函数:

//Calling initConnection funtion.
    [self initConnection:^{

        NSLog(@"complition success");
    }];