Question

I'm writing an iOS app which listen to a socket in a NSOperation. When something has been read on the socket, I change the content of the view (just change hidden parameter of some label) by calling a method of the UIViewController. The problem is that the view does not refresh instantly. Why that and how can I fix the problem ?

in viewDidLoad of my UIViewController :

_broadcastQueue = [[NSOperationQueue alloc] init];
_broadcastQueue.name = @"broadcastPairing";
PairingDevice *broadcast = [[PairingDevice alloc] initWithCode:_code andDelegate:self];
[_broadcastQueue addOperation:broadcast];

when the listener receive data, I call :

[self.delegate setClientConnectedWithSocket:sockTCP andIP:[NSString stringWithUTF8String:inet_ntoa(sin.sin_addr)] andHostname:[NSString stringWithUTF8String:data.machineName]];

in that method of my UIViewController I do (for now):

    _codeLabel.hidden = YES;
    _infoText.hidden = YES;

    _hostLabel.hidden = NO;
    _ipLabel.hidden = NO;
    _ipLabel.text = ip;
    _hostLabel.text = hostname;
Was it helpful?

Solution 2

Probably you the socket listen not in the main thread.
Try to warp the delegate call with operatio main queue, as following:

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
   [self.delegate setClientConnectedWithSocket:sockTCP andIP:[NSString stringWithUTF8String:inet_ntoa(sin.sin_addr)] andHostname:[NSString stringWithUTF8String:data.machineName]];
}];

In addition as check if the delegate respond to @selectore before calling him.

OTHER TIPS

Make sure that you are performing that code on the main thread of your application.
That's just an assumption, but I guess that your network call is performed in a background thread (as it is intended), so you need to do all yout UI stuff inside the main thread:

dispatch_async(dispatch_get_main_queue(), ^{
// Your UI code
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top