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];