2010-09-30 49 views
1

可以在任何一个你发布的代码片段,例如教您如何使用可可触摸框架从相同的viewController处理多个多NSURLCoonections使用iPhone SDK的

NSURLConnections ....

感谢所有你未来的帮助.....

回答

0

看到这个链接的答案。它用asihttp包装nsurlconnection,这会让你的生活变得更容易。

Does ASIHTTP support multi threads?

+0

约旦,我无法找到它的解决方案。你可以使用ASIHTTPRequest发布一个相关的例子。 如果你在这个问题上发现任何使用NSURLConnection的代码片段,让我试试这个,对我的应用程序会更有帮助。感谢你的帮助。 – Ramya 2010-10-01 13:51:20

+0

这是ASI文档。如果你不能按照文档,你可能需要做一些背景阅读之前尝试NSURLConnection。网络队列的使用以简明的语言和代码示例进行了阐述。 http://allseeing-i.com/ASIHTTPRequest/How-to-use – Jordan 2010-10-01 15:42:12

3

我经手使用的NSMutableDictionary其跟踪哪些NSMutableData实例的特定NSURLConnection的应该保存它的结果多NSUrlConnections。

在我上课的时候,我定义:

NSMutableDictionary *dataDictionary; 
我loadData方法

然后,我有:

 // Create the request 
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:currentCam] 
               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
              timeoutInterval:30]; 

    // create the connection with the request 
    // and start loading the data 
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
    if (theConnection) { 

     NSMutableData *receivedData = receivedData = [[NSMutableData alloc] init]; 

     //keep track of this connection by adding it to subViewDictionary and dataDictionary with appropriate objects. 
     [dataDictionary setObject:receivedData forKey:[theConnection description]]; 
    } 

    else { 

     NSLog(@"ERROR DOWNLOADING WITH NSURLCONNECTION"); 
    } 

    [theConnection release]; 

我用[theConnection说明】如我的钥匙和MSMutableData的一个实例作为我的字典中的对象,所以稍后我可以查找哪个实例与特定连接一起使用。如果你不这样做,你可能会遇到数据损坏的问题(多个连接都可以将他们的数据保存到同一个变量中)。

然后,我定义如下NSURLConnection的委托方法:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSMutableData *)data 
{ 
    //look up in dictionary to find out which recievedData instance to use. 
    NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; 

    // Append the new data to receivedData. 
    [theReceivedData appendData:data]; 
} 


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
     //Lookup in the dictionary to find the correct instance of recievedData to put image into. 
    NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; 
    [theReceivedData setLength:0]; 
    [activityIndicator stopAnimating]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    // inform the user that there was an error 
    NSLog(@"Connection failed! Error - localizedDescription:%@ NSURLErrorFailingURLStringErrorKey:%@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 


    NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; 
    [theReceivedData release]; 

    //remove keys for this connection since it did not load. 
    [dataDictionary removeObjectForKey:[connection description]]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 

    //look up correct instance of recievedData in teh dictionary for this connection 
    NSMutableData *theReceivedData = [dataDictionary objectForKey:[connection description]]; 

    NSLog(@"Succeeded! Received %d bytes of data",[theReceivedData length]); 

    //---do stuff with data here// 

    [theReceivedData release]; 
} 

有上NSURLConnection的here一个很好的教程为一个单独的连接。我的代码是基于此,增加了NSMutableDictionary来跟踪每个NSUrlConnection和每个NSMutableData实例。

我希望这是有道理的,是有帮助的!

+0

杰克逊,谢谢你的回应。我将在我的应用程序中尝试使用此功能。 – Ramya 2011-01-19 03:43:21

+0

不客气。我希望我能帮忙。 – Jackson 2011-01-23 18:10:17