用户交互导致for循环移动到下一个对象

时间:2016-10-26 12:08:42

标签: ios objective-c arrays for-loop grand-central-dispatch

我有一个对象数组,其中for循环遍历。每个对象都是我测试的IP地址。但是,一旦for循环到达失败的特定对象,for循环大约需要15秒到一分钟才能移动到下一个对象。但是,如果我通过点击与设备上的显示交互,则for循环移动到下一个对象。我发现这很奇怪,因为我打电话给[[UIApplication sharedApplication] beginIgnoringInteractionEvents];。所以A)为什么应用程序仍然检测影响for循环的触摸事件。并且B)我确认[[UIApplication sharedApplication] beginIgnoringInteractionEvents];确实有效,因为如果我尝试点击barButtonItem没有任何反应。所以这是我的问题,暂时忽略的用户交互如何仍然导致for循环移动到下一个对象?我已经确认线程/ GCD没有问题。我知道如果没有代码示例,这可能很难理解,但是我想知道影响for循环的UI交互事件背后的原理。

更新<!/强>

代码:

    -(void)beginConnectivitySetupAndTests {
for (int i = 0; i<[extractedDataArray count]; i++) {
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
                    NSString *inStoreObject = [[extractedDataArray objectAtIndex:i] valueForKey:@"ConnectabilityObjectType"];
                    NSString *objectTitle = [[extractedDataArray objectAtIndex:i] valueForKey:@"ConnectabilityObjectTechnicalTitle"];
                    NSString *objectLastOctet = [[extractedDataArray objectAtIndex:i] valueForKey:@"ConnectabilityObjectLastOctet"];
                    NSString *isInfrastructureObject = [[extractedDataArray objectAtIndex:i] valueForKey:@"ConnectabilityObjectIsInfrastructureObject"];
                    NSString *needsAlternateSecondOctet = [[extractedDataArray objectAtIndex:i] valueForKey:@"ConnectablityObjectNeedsAlternateSecondOctet"];
                    NSLog(@"Working with: %@",objectTitle);
                    if ([inStoreObject isEqualToString:@"InStoreObject"]) {

                        if ([needsAlternateSecondOctet isEqualToString:@"YES"]) {
//                            self.deviceIPAddress = nil;
                            NSLog(@"%@", self.deviceIPAddress);
                            NSMutableArray <NSString *> *octets = [self.deviceIPAddress componentsSeparatedByString:@"."];
                            NSString *firstOctetString   = [octets objectAtIndex:0];
                            NSString *secondOctetString  = [octets objectAtIndex:1];
                            NSString *thirdOctetString   = [octets objectAtIndex:2];


                            int secondInt = secondOctetString.intValue;

                            if (secondInt >= 40 && secondInt <= 47) {
                                [octets removeObjectAtIndex:1];
                                secondOctetString = [NSString stringWithFormat:@"%d", secondInt +40];
                            }

                            else if (secondInt >= 52 && secondInt <= 53) {
                                [octets removeObjectAtIndex:1];
                                secondOctetString = [NSString stringWithFormat:@"%d", secondInt +100];
                            }

                            else if (secondInt >= 120 && secondInt <= 121) {
                                [octets removeObjectAtIndex:1];
                                secondOctetString = [NSString stringWithFormat:@"%d", secondInt +100];
                            }

                            else if (secondInt >= 122 && secondInt <= 123) {
                                [octets removeObjectAtIndex:1];
                                secondOctetString = [NSString stringWithFormat:@"%d", secondInt +40];
                            }

                            else if (secondInt >= 124 && secondInt <= 125) {
                                [octets removeObjectAtIndex:1];
                                secondOctetString = [NSString stringWithFormat:@"%d", secondInt +40];
                            }

                            else if (secondInt >= 126 && secondInt <= 127) {
                                [octets removeObjectAtIndex:1];
                                secondOctetString = [NSString stringWithFormat:@"%d", secondInt +40];
                            } else {

                            }
                            NSString *finalString = [NSString stringWithFormat:@"%@.%@.%@.%@", firstOctetString, secondOctetString, thirdOctetString, objectLastOctet];

                            [[extractedDataArray objectAtIndex:i] setValue:[NSString stringWithFormat:@"%@",finalString]forKey:@"ConnectabilityObjectFullIPAddress"];
                            finalIPAddress = finalString;

                            octets = nil;

                        } else {

                            NSMutableArray <NSString*> *octets = [self.deviceIPAddress componentsSeparatedByString:@"."];
                            NSString *firstOctetString = [octets objectAtIndex:0];
                            NSString *secondOctetString = [octets objectAtIndex:1];
                            NSString *thirdOctetString = [octets objectAtIndex:2];
                            [octets removeObjectAtIndex:3];
                            NSString *lastOctet = [NSString stringWithFormat:@"%@", objectLastOctet];

                            NSString *constructedIPAddress = [NSString stringWithFormat:@"%@.%@.%@.%@", firstOctetString, secondOctetString, thirdOctetString, lastOctet];

                            finalIPAddress = constructedIPAddress;
                            [[extractedDataArray objectAtIndex:i] setValue:[NSString stringWithFormat:@"%@",constructedIPAddress]forKey:@"ConnectabilityObjectFullIPAddress"];
                            NSLog(@"%@ IP: %@", objectTitle, constructedIPAddress);
                    }

                    } else {
                        finalIPAddress = [[extractedDataArray objectAtIndex:i] valueForKey:@"ConnectabilityObjectFullIPAddress"];
                    }



                self.ping = [[SimplePing alloc] initWithHostName:finalIPAddress];
                NSLog(@"pinging: %@",finalIPAddress);
                self.ping.delegate = self;

                [self.ping start];
                do {
                    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
                } while (self.ping != nil);


                if (pingableObjectAddress) {
                    if (_deviceLatencyString == NULL) {
                        [[extractedDataArray objectAtIndex:i] setValue:[NSString stringWithFormat:@"Successful"] forKey:@"ConnectivityPingTestResultValue"];
                    } else {
                        [[extractedDataArray  objectAtIndex:i] setValue:[NSString stringWithFormat:@"Successful: latency: %@ ms",_deviceLatencyString] forKey:@"ConnectivityPingTestResultValue"];
                    }
                        pingableObjectAddress = nil;
                        CFReadStreamRef readStream;
                        CFWriteStreamRef writeStream;

                        portNumberValue = [[[extractedDataArray objectAtIndex:i] valueForKey:@"ConnectabilityObjectPorts"] intValue];
                        CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)finalIPAddress, portNumberValue, &readStream, &writeStream);

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

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

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

                        [inputStream open];
                        [outputStream open];

                    NSLog(@"Opening Port for %lu at IP Address %@", (unsigned long)portNumberValue, finalIPAddress);

                        NSString *response  = @"Fly with me!";
                        NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];

                        [outputStream write:[data bytes] maxLength:[data length]];

                    if ([[extractedDataArray objectAtIndex:i] valueForKey:@"ConnectabilityObjectPorts"] != nil ) {

                        if (_connectedPortNumber != 0) {
                            NSLog(@"Port Connection %lu succeeded for %@", (unsigned long)portNumberValue, finalIPAddress);
                            [[extractedDataArray objectAtIndex:i] setValue:@"Successful" forKey:@"ConnectivityPortConnectResultValue"];
                            if (!isInfrastructureObject) {
                                _portConnectionSucceeded = _portConnectionSucceeded + 1;
                                NSLog(@"port connect is not for infrastructure object");
                            }
                            self.connectedPortNumber = 0;
                        } else {
                            if (!isInfrastructureObject) {
                                _portConnectionFailed = _portConnectionFailed + 1;
                            }
                        }
                    }
                    } else {
                        if (!isInfrastructureObject) {
                             pingFailed = pingFailed + 1;
                        }
                    }
            NSLog(@"%lu",(unsigned long)i);
        }
 }

        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];


}
// does have a valid IP address
-(void)simplePing:(SimplePing *)pinger didStartWithAddress:(NSData *)address {
    [self.ping sendPingWithData:nil];
}


// device did send packet
-(void)simplePing:(SimplePing *)pinger didSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber address:(nonnull NSString *)address {
//    NSLog(@"Sending 64 bytes to %@: icmp_seq=%hu ttl=64", address, sequenceNumber);
    pingTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:PING_TIMEOUT target:self selector:@selector(timerFired) userInfo:nil repeats:NO];
    _dateRef = [NSDate date];
    NSLog(@"Packet Sent to: %@", address);
}


// timeout timer, fires when a ping back is never recieved
-(void)timerFired {
    NSLog(@"timeout timer fired");
    [pingTimeoutTimer invalidate];
    //self.pingableObjectAddress = nil;
    self.pingTimeoutTimer = nil;
    self.ping = nil;
}

// failed to send packet error handler
-(void)simplePing:(SimplePing *)pinger didFailToSendPacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber error:(NSError *)error {
    [self.pingTimeoutTimer invalidate];
    pingTimeoutTimer = nil;

    self.ping = nil;

    if (error) {
        NSLog(@"Ping Error: %@", error);
    }
}

// device did recieve unexpected ping packet, handle this when this happens
-(void)simplePing:(SimplePing *)pinger didReceiveUnexpectedPacket:(NSData *)packet {}

// did recieve response packet
-(void)simplePing:(SimplePing *)pinger didReceivePingResponsePacket:(NSData *)packet sequenceNumber:(uint16_t)sequenceNumber address:(NSString *)address {
    [pingTimeoutTimer invalidate];
    NSDate *end = [NSDate date];
    double latency = [end timeIntervalSinceDate:_dateRef] *1000;
    self.deviceLatencyString = [NSString stringWithFormat:@"%.f", latency];
    self.pingTimeoutTimer = nil;
//    NSLog(@"64 bytes sent to %@: icmp_seq=%hu ttl=64 time = %f ms", address, sequenceNumber, _latency);
    self.pingableObjectAddress = address;
//    NSLog(@"%@", pingableObjectAddress);
    NSLog(@"did recieve packet from: %@", address);
    self.ping = nil;

}


// device failed to ping for failed reason
-(void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error {
    NSLog(@"Failed With Error %@", error);
    assert(pinger == self.ping);
    [self.ping stop];

    self.ping = nil;
}

0 个答案:

没有答案