English 中文(简体)
如何在从 NSURLConnection 收到数据后返回数据
原标题:How to return data after receveing data from NSURLConnection

我有一个登录方法。 在使用 NSURLonconnection 登录的方法中, 我想要返回 NSData 响应。 问题是, 在连接实际获得数据之前, 我返回 NSData 。

- (NSData*)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responseData appendData:data]; //responseData is a global variable
NSLog(@"
Data is: %@", [[[NSString alloc] initWithData:responseData     
encoding:NSUTF8StringEncoding]autorelease]);//this works
isLoaded = YES; //isLoaded is a BOOL
}

- (NSData*)login:(NSString*)username withPwd:(NSString*)password{  
isLoaded = NO;
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request   
delegate:self];

if(connection){
    NSLog(@"Connected");
}
 while(isLoaded = NO){
[NSThread NSSleepForTimeInterval: 1]; 
}
isLoaded = NO;
return responseData;
}

程序在循环时被卡住, 但是如果没有循环, 程序可以从服务器上检索数据, 只是方法似乎返回响应数据, 在代表方法改变它之前 。

所以,我的问题是,我怎样才能做到, 才能让这个方法在服务器完成后才会返回响应数据?

问题回答

除非另有说明, NSURLoncomnction 将一个 URL 自动装入 URL 。 它使用 deleegate 回调 来向代表更新 URL 下载进度 。

具体来说, 使用 NSURLOLONONE 代表的连接已结束 : 方法 。 您的 NSURLONE 对象将在所有数据装入后调用此功能。 您可以在此方法范围内返回您的数据 。

您可以同步装入数据, 但最终可能会阻碍 UI 。

祝你好运!

你应该重构你的代码

您正在使用一个非同步的呼叫( 良好), 但是您试图同步处理它( 不太好, 如果不是使用一条单独的线的话 ) 。

要使用非同步行为, 您需要回调, 在可可时装中, 这通常是一种代表方法( 或可以是新代码的块块) 。 实际上, 它是你的 < code> 连接: didRecepiveData 。 此方法将使用返回的数据, 而不是您开始请求的数据。 因此通常的方法是启动一个不同步请求, 不返回任何东西, 并且肯定不是, 预计将返回的请求形式 。

- (void)login:(NSString*)username withPwd:(NSString*)password
{  
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}


//Note: you cannot change the delegate method signatures, as you did (your s returns an NSData object)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    [self.responseData appendData:data]
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{ 
    //Now that the connection was successfully terminated, do the real work.
}

看看这个 补充示例代码

您可以使用同步请求方法

- (NSData*)login:(NSString*)username withPwd:(NSString*)password
{  
   NSError *error = nil;
   NSURLResponse *response = nil;

   NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]

   return responseDate;
}

Documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURLConnection/sendSynchronousRequest:returningResponse:error:





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签