在 iPhone 上使用带有辅助线程的 AsyncSocket

发布于 2024-10-13 01:49:31 字数 1149 浏览 0 评论 0原文

我在 iPhone 上使用 AsyncSocket 与服务器通信。 AsyncSocket 基于运行循环,但我的应用程序基于线程。这意味着,我启动一个新线程来写入数据并等待,直到在同一线程上收到响应。但我无法直接从另一个线程调用 AsyncSocket 的方法,我必须使用:

[self performSelectorOnMainThread:@selector(writeSomeData:) withObject:dataToWrite waitUntilDone:YES];

它确实有效,但我无法从以这种方式调用的方法“writeSomeData:”中获得响应,因为 PerformSelectorOnMainThread 不返回任何内容。

writeSomeData: 方法的作用如下:

-(NSData *)writeData:(NSData *)dataToWrite {
    dataReceived = nil; // AsyncSocket writes data to this variable
    [asyncSocket writeData:dataToWrite withTimeout:-1 tag:0];
    [asyncSocket readDataToData:[@"<EOF" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
    int counter = 0;
    while (dataReceived == nil && counter < 5) {
        // runLoop is [NSRunLoop currentRunloop]
        [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.3]];
        ++counter;
    }

    return [dataReceived copy];
}

我可以通过访问类变量“dataReceived”来获取响应,但此时它的内容已更改。

谁能告诉我如何在单独的线程上使用 AsyncSocket (或者通常如何处理基于运行循环的类),这样如果我调用该类的方法,它就会阻塞,直到执行该方法并收到响应为止?

谢谢。

I use AsyncSocket on the iPhone to communicate with a server. AsyncSocket is based on run loops but my app is based on threads. That means, I start a new thread to write data and wait until a response is received on the same thread. But I can't call an AsyncSocket's method directly from another thread, I have to use:

[self performSelectorOnMainThread:@selector(writeSomeData:) withObject:dataToWrite waitUntilDone:YES];

It does work, but I cannot get the response from my method "writeSomeData:" called this way, because performSelectorOnMainThread returns nothing.

The method writeSomeData: does something like this:

-(NSData *)writeData:(NSData *)dataToWrite {
    dataReceived = nil; // AsyncSocket writes data to this variable
    [asyncSocket writeData:dataToWrite withTimeout:-1 tag:0];
    [asyncSocket readDataToData:[@"<EOF" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0];
    int counter = 0;
    while (dataReceived == nil && counter < 5) {
        // runLoop is [NSRunLoop currentRunloop]
        [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.3]];
        ++counter;
    }

    return [dataReceived copy];
}

I could get the response by accessing the class variable "dataReceived", but it's content is changed at this time.

Can anybody tell me how to use AsyncSocket (or generally, how to deal with run loop based classes) on separate threads, so that if I call a method of that class it blocks until the method is executed and a response is received?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨小墨 2024-10-20 01:49:31

尝试使用 GCD(Grand Central Dispatch)在单独的线程上写入数据,然后在写入数据时返回主线程。你可以这样做:

// call this on the main thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    NSData *data = [self writeData:dataToWrite];
    dispatch_async(dispatch_get_main_queue(), ^{
        // do something with the data on the main thread.
    });
});

我希望这样的事情可以帮助你......

Try using GCD(Grand Central Dispatch) to write your data on a separate thread and than come back to the main thread the moment that the data was written. You could do it like this:

// call this on the main thread
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    NSData *data = [self writeData:dataToWrite];
    dispatch_async(dispatch_get_main_queue(), ^{
        // do something with the data on the main thread.
    });
});

I hope something like this can help you...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文