我有一个tcp流连接,可以将一个巨大的NSDictionary联系人一个接一个地从手机发送到服务器。如果NSDictionary有50个条目,它就可以了,但是在发送大约150个155个联系人后,大约有200个应用程序崩溃。我认为这可能是一个内存问题,或者流连接有一些限制?如果存在内存问题,例如我使用ARC,我该如何解决?
响应处理(我认为这可能是问题,特别是因为流已经关闭了很多次):
(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
NSLog(@“stream event%i”,streamEvent); recebeuResposta = YES; switch(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];*/
}
}
答案 0 :(得分:0)
我使用了robbiehanson的CocoaAsyncSocket库,它更快更稳定:
https://github.com/robbiehanson/CocoaAsyncSocket
如果你必须建立套接字连接我强烈建议使用它,它会更快更简单(至少对我而言)。