首先,问题很简单。如果你只是想看看它们是什么,请跳到这篇文章的底部,你会看到它们以粗体显示。有关更多详细信息,那么你可以阅读这篇文章的其余部分......
我只是想解决我的 NSURLConnection 问题,以便它工作顺利,我正确理解这一点。互联网上非常缺乏异步连接的示例/教程,或者我找不到任何可以解释的任何深度的例子/教程,而不是让连接启动并运行,在处理它之后看起来很简单。希望这个问题可以填补我认为其他用户的空白。
因此,在我的 .h 文件中,我导入了基础头文件并声明了接收或缺少接收数据(错误等)所需的方法。
。H
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h> //add foundations
//.. other headers can be imported here
@interface MyViewController: UITableViewController {
//Im not setting any delegates to access the methods because Is all happening in the same
//place so I just use the key word 'self' when accessing the methods declared below
//I'm not sure if this is the best thing to do but I wasn't able to get my head around declaring the delegate or how it would help me with the way I have set up my request etc.
}
- (IBAction)setRequestString:(NSString *)string; //this method sets the request and connection methods
//these methods receive the response from my async nsurlconnection
- (void)receivedData:(NSData *)data;
- (void)emptyReply;
- (void)timedOut;
- (void)downloadError:(NSError *)error;
所以这就是我的头文件.. 很简单,不需要太多解释。
.m
//call setRequestString from some other method attached to a button click or something
[self setRequestString:@"rss.xml"];
//..
- (IBAction)setRequestString:(NSString *)string
{
//Set database address
NSMutableString *databaseURL = [[NSMutableString alloc] initWithString:@"http:www.comicbookresources/feeds/"]; // address not real jsut example
//append the string coming in to the end of the databaseURL
[databaseURL appendString:string];
//prepare NSURL with newly created string
NSURL *url = [NSURL URLWithString:databaseURL];
//AsynchronousRequest to grab the data
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil){
[self receivedData:data];
}else if ([data length] == 0 && error == nil){
[self emptyReply];
}else if (error != nil && error.code == NSURLErrorTimedOut){ //used this NSURLErrorTimedOut from foundation error responses
[self timedOut];
}else if (error != nil){
[self downloadError:error];
}
}];
}
现在设置在 .h 文件中初始化并在上面的 if 语句中调用的方法
- (void)receivedData:(NSData *)data
{
NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", newStr); //logs recived data
//now you can see what data is coming in on your log
//do what you want with your data here (i.e. start parsing methods
}
- (void)emptyReply
{
//not sure what to do here yet?
}
- (void)timedOut
{
//also not sure what to do here yet?
}
- (void)downloadError:(NSError *)error
{
NSLog(@"%@", error);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"A connection failure occurred." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[errorAlert show];
}
很酷,所以我在那里所做的事情的基本知识..现在我的问题如下。
问题一: 我这样称呼 NSURLConnection
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
这里发生了什么 ^ 是为了在不同的线程或其他东西上执行整个块(包括 if 语句)?因为它看起来很像中央调度格式,但略有不同。
问题二:在emptyReply和timedOut方法 中我应该做什么?
问题三: 我如何将缓存纳入其中?我想缓存从不同请求返回的响应。即用我的 setRequestString 你会看到有一个字符串输入参数,所以我可以用相同的方法请求不同的 rss 提要。我需要弄清楚如何将这些响应缓存到单独的缓存中。但我不知道从哪里开始它。
最后 ,如果您已经做到了这一点,非常感谢您阅读我的问题。希望通过您的回复,我们可以在这里得到一个很好的解决方案。其他人可以自己使用并挑选他们需要的适用于自己的解决方案的部分和部分。
无论如何,非常感谢您的阅读,我期待您的回复.. 即使它们只是对您认为可能对我有帮助的教程或示例的参考.. 任何事情都很好,我只想完全了解发生了什么以及什么是好的解决方案.