简体   繁体   中英

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. but the ui freezes until my authentication is completed. for this use "dispatch_queue" follows

-(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?

excuse my spelling, I do not speak much English


reading some forums, I found that NSThreads can pass data between them with:

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:

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

Where TcpSocketFunction is the function name with a defination like.

- (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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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