Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 处理网络连接的正确方法是什么?_Objective C_Ios_Xcode - Fatal编程技术网

Objective c 处理网络连接的正确方法是什么?

Objective c 处理网络连接的正确方法是什么?,objective-c,ios,xcode,Objective C,Ios,Xcode,我的应用正在使用网络连接,如下所示: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ . . . receivedData = [[NSMutableData alloc] init]; } 。应用程序中的以下代码片段(onButtonPressed): 我想了解的是: 如果我想

我的应用正在使用网络连接,如下所示:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 
    .
    .
    .
    receivedData = [[NSMutableData alloc] init];
}

。应用程序中的以下代码片段(onButtonPressed):

我想了解的是:

  • 如果我想创建另一个同步的URLRequest,我是否需要使用不同的连接,以便在检索时不会混淆来自web的数据

  • 在这段代码中,有时代码会在函数didReceiveResponse()
    setLength:0
    的行上崩溃,当应用程序崩溃时,我看到
    receivedData=nil
    我应该将行更改为

    if(receivedData!=nil)
       [receivedData setLength:0];
    else
       receivedData = [[NSMutableData data] retain];
    
  • 我不太确定这行正在做什么receivedData=[[NSMutableData]retain]


我认为处理两个连接的最简单方法是复制
NSMutableData
。我就是这样使用的,它对我来说非常适合

首先,在需要第二个连接的点执行此操作:

receivedData2 = [[NSMutableData alloc] init];

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  
{  
    if (receivedData2) {
        [receivedData2 setLength:0];  
    }
    else 
    {
        [receivedData setLength:0];  
    }
}  

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  
{  
    if (receivedData2) {
        [receivedData2 appendData:data];  
    }
    else {    
        [receivedData appendData:data]; 
    }
} 
然后,在您请求receivedData2或receivedData的方法中

当你使用它时,别忘了做:

receivedData2=nil;
[receivedData2 setLength:0];

我认为处理2个连接的最简单方法是复制
NSMutableData
。我就是这样使用的,它对我来说非常适合

首先,在需要第二个连接的点执行此操作:

receivedData2 = [[NSMutableData alloc] init];

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  
{  
    if (receivedData2) {
        [receivedData2 setLength:0];  
    }
    else 
    {
        [receivedData setLength:0];  
    }
}  

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  
{  
    if (receivedData2) {
        [receivedData2 appendData:data];  
    }
    else {    
        [receivedData appendData:data]; 
    }
} 
然后,在您请求receivedData2或receivedData的方法中

当你使用它时,别忘了做:

receivedData2=nil;
[receivedData2 setLength:0];
  • 当然,您需要一个不同的
    NSURLConnection
  • 你可以这样做,但最好找出为什么它是零,因为它不应该
  • 它分配一个空的
    NSMutableData
    对象。但对我来说,这似乎是一段难看的代码,因为您创建了一个自动删除的对象并保留了它。最好编写
    [NSMutableData new]
    [[NSMutableData alloc]init]
  • 当然,您需要一个不同的
    NSURLConnection
  • 你可以这样做,但最好找出为什么它是零,因为它不应该
  • 它分配一个空的
    NSMutableData
    对象。但对我来说,这似乎是一段难看的代码,因为您创建了一个自动删除的对象并保留了它。最好编写
    [NSMutableData new]
    [[NSMutableData alloc]init]
  • if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        receivedData = [[NSMutableData data] retain];
    } else {
        NSLog(@"Connection failed");
    }
    
    if(receivedData!=nil)
       [receivedData setLength:0];
    else
       receivedData = [[NSMutableData data] retain];
    
    receivedData2 = [[NSMutableData alloc] init];
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  
    {  
        if (receivedData2) {
            [receivedData2 setLength:0];  
        }
        else 
        {
            [receivedData setLength:0];  
        }
    }  
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data  
    {  
        if (receivedData2) {
            [receivedData2 appendData:data];  
        }
        else {    
            [receivedData appendData:data]; 
        }
    } 
    
    receivedData2=nil;
    [receivedData2 setLength:0];