我是新的目标C和iOS的发展在总体上。我正在尝试创建一个应用程序,它可以发出http请求并在标签上显示内容。
当我开始测试时,我注意到标签是空白的,尽管我的日志显示我有数据返回。显然,发生这种情况是因为标签文本更新时响应还没有准备好。
我在上面加了一个循环来解决这个问题,但是我几乎肯定会有一个更好的方法来解决这个问题。
ViewController.m
- (IBAction)buttonSearch:(id)sender {
HttpRequest *http = [[HttpRequest alloc] init];
[http sendRequestFromURL: @"https://en.wiktionary.org/wiki/incredible"];
//I put this here to give some time for the url session to comeback.
int count;
while (http.responseText ==nil) {
self.outputLabel.text = [NSString stringWithFormat: @"Getting data %i ", count];
}
self.outputLabel.text = http.responseText;
}HttpRequest.h
#import <Foundation/Foundation.h>
@interface HttpRequest : NSObject
@property (strong, nonatomic) NSString *responseText;
- (void) sendRequestFromURL: (NSString *) url;
- (NSString *) getElementBetweenText: (NSString *) start andText: (NSString *) end;
@endHttpRequest.m
@implementation HttpRequest
- (void) sendRequestFromURL: (NSString *) url {
NSURL *myURL = [NSURL URLWithString: url];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: myURL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest: request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
self.responseText = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
}];
[task resume];
}非常感谢您的帮助:)
更新
在阅读了大量关于这些非常有用的评论之后,我意识到我忽略了整个观点。因此,从技术上讲,NSURLSessionDataTask将向队列中添加任务,该队列将异步进行调用,然后我必须为该调用提供一个代码块,以便在任务生成的线程完成后执行。
Duncan非常感谢代码中的响应和评论。这对我有很大帮助。
所以我用所提供的信息重写了我的程序。请注意,他们是有点冗长,但,我希望它像这样理解整个概念,目前。(我正在声明一个代码块,而不是嵌套它们)
HttpRequest.m
- (void) sendRequestFromURL: (NSString *) url
completion:(void (^)(NSString *, NSError *))completionBlock {
NSURL *myURL = [NSURL URLWithString: url];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: myURL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest: request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//Create a block to handle the background thread in the dispatch method.
void (^runAfterCompletion)(void) = ^void (void) {
if (error) {
completionBlock (nil, error);
} else {
NSString *dataText = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
completionBlock(dataText, error);
}
};
//Dispatch the queue
dispatch_async(dispatch_get_main_queue(), runAfterCompletion);
}];
[task resume];
} ViewController.m
- (IBAction)buttonSearch:(id)sender {
NSString *const myURL = @"https://en.wiktionary.org/wiki/incredible";
HttpRequest *http = [[HttpRequest alloc] init];
[http sendRequestFromURL: myURL
completion: ^(NSString *str, NSError *error) {
if (error) {
self.outputText.text = [error localizedDescription];
} else {
self.outputText.text = str;
}
}];
}请随时评论我的新代码。风格,不正确的用法,不正确的流程;反馈在这个学习阶段非常重要,所以我可以成为一个更好的开发人员:)
再次感谢您的回复。
发布于 2016-06-04 12:31:57
重写sendRequestFromURL函数以获得一个完成块:
- (void) sendRequestFromURL: (NSString *) url
completion: (void (^)(void)) completion
{
NSURL *myURL = [NSURL URLWithString: url];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: myURL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest: request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
self.responseText = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
if (completion != nil)
{
//The data task's completion block runs on a background thread
//by default, so invoke the completion handler on the main thread
//for safety
dispatch_async(dispatch_get_main_queue(), completion);
}
}];
[task resume];
}然后,在调用sendRequestFromURL时,当请求作为完成块准备好时,传入要运行的代码:
[self.sendRequestFromURL: @"http://www.someURL.com&blahblahblah",
completion: ^
{
//The code that you want to run when the data task is complete, using
//self.responseText
}];
//Do NOT expect the result to be ready here. It won't be.上面的代码使用没有参数的完成块,因为您的代码将响应文本保存到实例变量。更典型的做法是将响应数据和NSError作为参数传递给完成块。请参阅@Yahoho的答案,以获得带有结果字符串和sendRequestFromURL参数的完成块的版本。
(注:我在SO post编辑器中编写了上述代码。它可能有一些语法错误,但它是作为指南,而不是代码,您可以复制/粘贴到位。目标-C块语法有点讨厌,我通常在第一次错误的时候,至少有一半的时间。)
发布于 2016-06-04 10:41:31
你知道吗,用AFNetworking来救你的命。
或者修改您的HttpRequest的sendRequestFromURL:
- (void)sendRequestFromURL:(NSString *)url completion:(void(^)(NSString *str, NSError *error))completionBlock {
NSURL *myURL = [NSURL URLWithString: url];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: myURL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest: request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
completionBlock(nil, error);
} else {
completionBlock([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding], error);
}
});
}];
[task resume];
}像这样调用
[http sendRequestFromURL:@"https://en.wiktionary.org/wiki/incredible" completion:^(NSString *str, NSError *error) {
if (!error) {
self.outputLabel.text = str;
}
}];发布于 2016-06-04 10:44:42
如果您想要简单的方法,那么不要为调用webservice创建单独的类。只需在viewController.m中制定方法即可。我的意思是在您的sendRequestFromURL中写入viewController.m,并在完成处理程序中更新标签的文本,
- (void) sendRequestFromURL: (NSString *) url {
NSURL *myURL = [NSURL URLWithString: url];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: myURL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest: request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
self.responseText = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
self.outputLabel.text = self.responseText;
})
}];
[task resume];
}https://stackoverflow.com/questions/37628930
复制相似问题