携帯電話からサーバーに連絡先の巨大なNSDictionaryを1つずつ送信するためのtcpストリーム接続があります。NSDictionaryに50のエントリがある場合は問題ありませんが、約150の155の連絡先を送信した後、約200のアプリがクラッシュします。メモリの問題である可能性があると思いますか、それともストリーム接続に制限がありますか?ARCをオンにしているときのように、メモリの問題がある場合、どうすれば解決できますか?
応答処理(特にストリームが何度も閉じられるため、これが問題になる可能性があると思います):
(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
NSLog(@ "ストリームイベント%i"、streamEvent); recebeuResposta = YES; スイッチ(streamEvent){
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];*/
}
}