简体   繁体   English

后台线程上的调用函数

[英]call function on background thread

I am newbie in ios development, I am doing an application using tcp socket, and followed several tutorials and everything related to the connection works great. 我是ios开发的新手,正在使用tcp套接字做一个应用程序,并且遵循了一些教程,并且与连接有关的所有内容都很好用。 but the ui freezes until my authentication is completed. 但ui冻结,直到我的身份验证完成。 for this use "dispatch_queue" follows 为此使用“ dispatch_queue”如下

-(BOOL)Connect {

       queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
       dispatch_async(queue, ^ {
            CFReadStreamRef readStream;
            CFWriteStreamRef writeStream;
            CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)IP,PORT, &readStream, &writeStream);

            inputStream = (__bridge NSInputStream *)readStream;
            outputStream =(__bridge NSOutputStream *)writeStream;

           [inputStream setDelegate:self];
           [outputStream setDelegate:self];

           [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
           [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
           [inputStream open];
           [outputStream open];
           [[NSRunLoop currentRunLoop] run];

       });

    return true;

}

up here, the connection works fine and does not lock. 到这里,连接工作正常并且没有锁定。 now my problem is that when I write something on the outputstream not send anything. 现在我的问题是当我在输出流上写东西时不发送任何东西。 from another class by pressing a button I need to send something to the server, the function is called but does not send nothing, because it is in another thread I think. 从另一个类按下按钮,我需要向服务器发送一些内容,该函数被调用但不发送任何内容,因为它在我认为的另一个线程中。

-(void)Send:(NSMutableData *)_output{
   [outputStream write:[_output  bytes] maxLength:[_output length]];
 }

How I can send something in outputstream if it is in another thread? 如果它在另一个线程中,如何在outputstream中发送东西?

excuse my spelling, I do not speak much English 对不起,我的英文不好


reading some forums, I found that NSThreads can pass data between them with: 阅读一些论坛,我发现NSThreads可以通过以下方式在它们之间传递数据:

performSelector:onThread:withObject:waitUntilDone:

but i don't know how I can create an instance of thread that is already running. 但是我不知道如何创建一个已经在运行的线程实例。 Someone can help me? 有人可以帮我吗?

for NS Thread just add all the above code in a function and call it like this: 对于NS Thread,只需将以上所有代码添加到函数中,然后像这样调用它:

[self performSelectorOnMainThread:@selector(TcpSocketFunction) withObject:nil waitUntilDone:NO];

Where TcpSocketFunction is the function name with a defination like. 其中TcpSocketFunction是带有类似定义的函数名称。

- (void) TcpSocketFunction {
 CFReadStreamRef readStream;
            CFWriteStreamRef writeStream;
            CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)IP,PORT, &readStream, &writeStream);

            inputStream = (__bridge NSInputStream *)readStream;
            outputStream =(__bridge NSOutputStream *)writeStream;

           [inputStream setDelegate:self];
           [outputStream setDelegate:self];

           [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
           [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
           [inputStream open];
           [outputStream open];
           [[NSRunLoop currentRunLoop] run];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM