NSStream and Sockets, NSStreamDelegate methods not being called

后端 未结 2 1776
礼貌的吻别
礼貌的吻别 2020-12-07 18:03

I\'ve followed the guide Setting Up Socket Streams and have effectively duplicated that code in my class. No matter what I try the delegate methods just don\'t seem to get c

2条回答
  •  失恋的感觉
    2020-12-07 18:56

    That solution will work if & only if you don't have blocking work on thread 0. This is often OK, but a better solution is to create a new thread (i.e. using a class method to create the thread on demand) and then enqueue on that thread. i.e.

    + (NSThread *)networkThread {
        static NSThread *networkThread = nil;
        static dispatch_once_t oncePredicate;
    
        dispatch_once(&oncePredicate, ^{
            networkThread =
                 [[NSThread alloc] initWithTarget:self
                                         selector:@selector(networkThreadMain:)
                                           object:nil];
            [networkThread start];
        });
    
        return networkThread;
    }
    
    + (void)networkThreadMain:(id)unused {
        do {
            @autoreleasepool {
                [[NSRunLoop currentRunLoop] run];
            }
        } while (YES);
    }
    
    - (void)scheduleInCurrentThread
    {
        [inputstream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                               forMode:NSRunLoopCommonModes];
    }
    

    With this, you can schedule the input stream using:

    [self performSelector:@selector(scheduleInCurrentThread)
                 onThread:[[self class] networkThread]
               withObject:nil
            waitUntilDone:YES];
    

    This will allow you to run your network operations without worrying about deadlocks anymore.

提交回复
热议问题