0

我有一个 tcp 流连接,可以将一个巨大的 NSDictionary 联系人一一从手机发送到服务器。如果 NSDictionary 有 50 个条目,则一切正常,但在发送大约 150 155 个联系人后,应用程序崩溃了大约 200 个。我认为这可能是内存问题,或者流连接有一些限制?如果存在内存问题,比如我开启了 ARC,我该如何解决?

响应处理(我认为这可能是问题所在,特别是因为流被关闭了很多次):

  • (void)stream:(NSStream *)theStream 句柄事件:(NSStreamEvent)streamEvent {

    NSLog(@"流事件 %i", streamEvent); recebeuResposta=是;开关(流事件){

    case NSStreamEventOpenCompleted:
        NSLog(@"Stream opened");
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"serverResponseArrived"
         object:nil];
        break;
    case NSStreamEventHasBytesAvailable:
    
        if (theStream == inputStream) {
    
            uint8_t buffer[10240];
            int len;
    
            while ([inputStream hasBytesAvailable]) {
                len = [inputStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0) {
    
                    NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                        //NSLog(@"server said: %@", output);
                    NSArray *firstSplit = [output componentsSeparatedByString:@"=end="];
                        // NSLog(@"firstSplit, %@",[firstSplit objectAtIndex:0]);
                    NSError *parseError = nil;
                    NSDictionary *outputDictionary =[[NSDictionary alloc]init];
                    outputDictionary = 
                    [NSJSONSerialization JSONObjectWithData: [[firstSplit objectAtIndex:0] dataUsingEncoding:NSASCIIStringEncoding] 
                                                    options: NSJSONReadingAllowFragments
                                                      error: &parseError];                        
                        //    NSLog(@"server said outputDictionary: %@", outputDictionary);
    
                    if (nil != output) {
                        if([(NSString*)[outputDictionary objectForKey:@"action"]isEqualToString:@"connect"])
                        {
                                //  NSLog(@"stream with server is opened. ready to send contacts.");
                                //   NSLog(@"action: %@",(NSString*)[outputDictionary objectForKey:@"action"]);
                            [[NSNotificationCenter defaultCenter]
                             postNotificationName:@"beginSyncronization"
                             object:nil];
                        }else if([(NSString*)[outputDictionary objectForKey:@"action"]isEqualToString:@"add"]&&[(NSString*)[outputDictionary objectForKey:@"type"]isEqualToString:@"response"]&&[(NSString*)[outputDictionary objectForKey:@"result"]isEqualToString:@"done"]&&[(NSString*)[outputDictionary objectForKey:@"element"]isEqualToString:@"contact"])
                        {
                                //    NSLog(@"enviar data");
                            [[NSNotificationCenter defaultCenter]
                             postNotificationName:@"sendNextContact"
                             object:nil];
                                //[self prepareDetails];
                        }else if([(NSString*)[outputDictionary objectForKey:@"action"]isEqualToString:@"add"]&&[(NSString*)[outputDictionary objectForKey:@"type"]isEqualToString:@"response"]&&[(NSString*)[outputDictionary objectForKey:@"result"]isEqualToString:@"error"]&&[(NSString*)[outputDictionary objectForKey:@"element"]isEqualToString:@"contact"])
                        {
                                //     NSLog(@"action: %@",(NSString*)[outputDictionary objectForKey:@"action"]);
                            [[NSNotificationCenter defaultCenter]
                             postNotificationName:@"sendSameContactTCP"
                             object:nil];
                        }else if([(NSString*)[outputDictionary objectForKey:@"action"]isEqualToString:@"SyncMobile"]&&[(NSString*)[outputDictionary objectForKey:@"type"]isEqualToString:@"response"]&&[(NSString*)[outputDictionary objectForKey:@"result"]isEqualToString:@"error"])
                        {
                                //   NSLog(@"action: %@",(NSString*)[outputDictionary objectForKey:@"action"]);
                            [[NSNotificationCenter defaultCenter]
                             postNotificationName:@"sendSameContactTCP"
                             object:nil];
                        }
                    }
                }
            }
        }else{
            NSLog(@"STREAM HAS NO BYTES! %@:",theStream);
        }
        break;
    
    
    case NSStreamEventErrorOccurred:
    
        NSLog(@"Can not connect to the host!");
        break;
    
    case NSStreamEventEndEncountered:
    
        NSLog(@"Stream closed");
        [theStream close];
        [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        theStream = nil;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"sendSameContactTCP" object:self];
        break;
    default:
        NSLog(@"Unknown event");
     /*   [[NSNotificationCenter defaultCenter]
         postNotificationName:@"sendSameContactTCP"
         object:nil];*/
    

    }

}

4

1 回答 1

0

我改用了 robbiehanson 的 CocoaAsyncSocket 库,它更快更稳定:

https://github.com/robbiehanson/CocoaAsyncSocket

如果您必须进行套接字连接,我强烈建议您使用它,它会更快更简单(至少对我而言)。

于 2012-12-12T12:00:21.260 回答