整个Demo中有LazyTableAppDelegate,ParseOperation,AppRecord,IconDownload,RootViewController
LazyTableAppDelegate
发起一个远程请求,获取App列表的xml的数据,保存到成员变量appListData中,用ParseOperation来进行解析,解析完毕,通知LazyTableAppDelegate的RootViewController的table进行reloaddata,在刷新数据的过程中每个cell将根据自己的AppRecord的imageurl值进行IconDownload下载,总体是这个流程,以下再详解下我的理解:
LazyTableAppDelegate 的成员变量
UIWindow *window;
UINavigationController *navigationController;
RootViewController *rootViewController; //拥有一个TableView,
NSMutableArray *appRecords; //appRecords与rootViewController中的entries共享
NSOperationQueue *queue; //执行xml的解析类ParseOperation,获取每个appRecord的属性值,并且ParseOperation的delegate也是self
NSURLConnection *appListFeedConnection; // 1.发起一个远程请求,获取一个xml内容
NSMutableData *appListData; // 从appListFeedConnection中获取到的原始数据
appListFeedConnection的delegate=self;所以要实现以下方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
2.最重要的是在didFinishLoading方法中,当数据已经收到,需要传给ParseOperation类去解析,把ParseOperation放入queue中执行
在queue中执行的ParseOperation的delegate=self;所以要实现以下方法:- (void)didFinishParsing:(NSArray *)appList;
3.当收到ParseOperation类解析完毕的通知,就会执行这个方法,这里执行一个方法是
[self performSelectorOnMainThread:@selector(handleLoadedApps:) withObject:appList waitUntilDone:NO];
这个方式是在主线程上调用handleLoadedApps进行UI更新方法reloaddata(UI更新将对Icon进行下载),传递的参数是ParseOperation解析完毕的workingArrays
- (void)parseErrorOccurred:(NSError *)error;
RootViewController有两种情况
a)还在获取xml阶段,tableview中还没数据,用一个placehold占位,
b)当收到reloaddata方法时,就要对enties进行读取,在读取到icon的url时,利用icondownload进行下载