@@ -199,17 +199,16 @@ - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange
199199# pragma mark - NSNetServiceBrowserDelegate Methods
200200
201201- (void )netServiceBrowserWillSearch : (NSNetServiceBrowser *)browser {
202- searching = YES ;
203- [self updateUI ];
202+ discoveryIsSearching = YES ;
204203}
205204
206205- (void )netServiceBrowserDidStopSearch : (NSNetServiceBrowser *)browser {
207- searching = NO ;
206+ discoveryIsSearching = NO ;
208207 [self updateUI ];
209208}
210209
211210- (void )netServiceBrowser : (NSNetServiceBrowser *)browser didNotSearch : (NSDictionary *)errorDict {
212- searching = NO ;
211+ discoveryIsSearching = NO ;
213212 [self handleError: errorDict[NSNetServicesErrorCode ]];
214213 [activityIndicatorView stopAnimating ];
215214 startDiscover.enabled = YES ;
@@ -224,7 +223,7 @@ - (void)netServiceBrowser:(NSNetServiceBrowser*)browser
224223 if ([type containsString: SERVICE_TYPE_HTTP]) {
225224 [services addObject: aNetService];
226225 if (!moreComing) {
227- [self stopDiscovery ];
226+ [self stopDiscover ];
228227 }
229228 }
230229 else if ([name isEqualToString: descriptionUI.text]) {
@@ -250,20 +249,22 @@ - (void)handleError:(NSNumber*)error {
250249}
251250
252251- (void )updateUI {
253- if (!searching) {
254- NSInteger j = services.count ;
255- if (j == 1 ) {
252+ if (discoveryIsSearching || (discoveryDidFail && services.count )) {
253+ return ;
254+ }
255+ switch (services.count ) {
256+ case 0 :
257+ [Utilities AnimView: noInstances AnimDuration: 0.3 Alpha: 1.0 XPos: 0 ];
258+ break ;
259+
260+ case 1 :
256261 [self resolveIPAddress: services[0 ]];
257- }
258- else {
259- if (j == 0 ) {
260- [Utilities AnimView: noInstances AnimDuration: 0.3 Alpha: 1.0 XPos: 0 ];
261- }
262- else {
263- [discoveredInstancesTableView reloadData ];
264- [Utilities AnimView: discoveredInstancesView AnimDuration: 0.3 Alpha: 1.0 XPos: 0 ];
265- }
266- }
262+ break ;
263+
264+ default :
265+ [discoveredInstancesTableView reloadData ];
266+ [Utilities AnimView: discoveredInstancesView AnimDuration: 0.3 Alpha: 1.0 XPos: 0 ];
267+ break ;
267268 }
268269}
269270
@@ -444,7 +445,13 @@ - (void)netServiceDidResolveAddress:(NSNetService*)service {
444445 [self fillServerDetailsForSegment: segmentServerType.selectedSegmentIndex];
445446}
446447
447- - (void )stopDiscovery {
448+ - (void )handleDiscoverTimeout {
449+ NSLog (@" Discovery timed out." );
450+ discoveryDidFail = YES ;
451+ [self stopDiscover ];
452+ }
453+
454+ - (void )stopDiscover {
448455 [discoveryTimeoutTimer invalidate ];
449456 [netServiceBrowser stop ];
450457 [activityIndicatorView stopAnimating ];
@@ -460,18 +467,20 @@ - (IBAction)startDiscover:(id)sender {
460467 [Utilities AnimView: noInstances AnimDuration: 0.3 Alpha: 0.0 XPos: self .view.frame.size.width];
461468 [Utilities AnimView: discoveredInstancesView AnimDuration: 0.3 Alpha: 1.0 XPos: self .view.frame.size.width];
462469
463- searching = NO ;
470+ discoveryIsSearching = NO ;
464471 netServiceBrowser.delegate = self;
465472 [netServiceBrowser searchForServicesOfType: SERVICE_TYPE_HTTP inDomain: DOMAIN_NAME];
466473 [self startDiscoveryTimeoutTimer ];
467474}
468475
469476- (void )startDiscoveryTimeoutTimer {
477+ [discoveryTimeoutTimer invalidate ];
470478 discoveryTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval: DISCOVER_TIMEOUT
471479 target: self
472- selector: @selector (stopDiscovery )
480+ selector: @selector (handleDiscoverTimeout )
473481 userInfo: nil
474482 repeats: NO ];
483+ discoveryDidFail = NO ;
475484}
476485
477486#pragma mark - Segment control
0 commit comments