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

标签 objective-c ios xcode

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

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

.

-(void) dataFromWeb{
request = [[NSURLRequest alloc] initWithURL: url];
theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

if (theConnection) {
    receivedData = [[NSMutableData data] retain];
    NSLog(@"** NSURL request sent !");
} else {

    NSLog(@"Connection failed");
}

.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    .
    .
    .
    [theConnection release];
    [request release];

// Here - using receivedData
[receivedData release];
}

.

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

.

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    [theConnection release];
    [request release];
    [receivedData release];
}

.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
}

.在应用程序的后面是这个片段 (onButtonPressed):

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");
}

我想理解的是:

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

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

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

最佳答案

我认为处理 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];

关于objective-c - 处理网络连接的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13510571/

相关文章:

objective-c - 多态性和 Cocoa NSViews + 绑定(bind)

ios - 使用带有 AutoLayout 的 UICollectionView 实现聊天布局

ios - 如何按日期排序数组?

ios - React-Native navigation.addListener 不是函数

ios - 获取当前行的正确编号(iOS)

iPhone map 套件 : How can I get a real business name/address from a longitude/latitude?

ios - 使用 Xcode 的 OpenFire

ios - 关闭键盘 - iOS 7 中的多个 UITextField

xcode - 本地化 - 向 localizable.strings 文件添加其他语言

ios - 在单元测试代码中访问部署目标