OperationQueue添加, 自定义继承自NSOperation的子类,
若开启并发,
重复执行5次左右,会卡在
NSOperation类:
- (void)finish { //结束线程用到
[selfwillChangeValueForKey:@"isExecuting"];
[selfwillChangeValueForKey:@"isFinished"];
executing = NO;
finished = YES;
[selfdidChangeValueForKey:@"isExecuting"];
[selfdidChangeValueForKey:@"isFinished"];
}
- (id)initWithURL:(NSString*)url delegate:(id<downloadFinishDelegate>)delegate//delegate是资料下载结束的回调
{
if(self=[superinit])
{
executing = NO;
finished = NO;
_webReachable=[WebConnectionWebConnect];
self.delegate=delegate;
_enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
_request=[[NSURLRequestrequestWithURL:[NSURLURLWithString:[url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]retain];
self.XMLData=[NSMutableData data];
}
return self;
}
- (void)start 在线程队列中重写start,线程会叠加,卡死
{
[selfwillChangeValueForKey:@"isExecuting"];
executing = YES;
[selfdidChangeValueForKey:@"isExecuting"];
//if(![self isCancelled])
if([NSURLConnection canHandleRequest:_request]&&_webReachable)
{
_connection=[[NSURLConnection connectionWithRequest:_requestdelegate:self] retain];
while (_connection!=nil) {
[[NSRunLoop currentRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDatedistantFuture]];
}
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"无网络连接1" message:@"请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
}
- (BOOL)isFinished //自定义operation时候用到
{
return finished;
}
- (BOOL)isConcurrent
{
returnYES;
}
- (BOOL)isExecuting
{
return executing;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[self.XMLData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.XMLData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self.connection release];
self.connection = nil;
self.lastLoadXMLData=[[_XMLDatacopy]autorelease];
//if([_delegate respondsToSelector:@selector(downloadFinish)])
if([_delegate conformsToProtocol:(@protocol(downloadFinishDelegate))])
{
[_delegatedownloadFinish:self.lastLoadXMLData];
}
[self finish];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error:%@",[error localizedDescription]);
[self finish];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.tableView.UserInteractionEnabled=NO;
DownloadOperation *downloadOperation=[[DownloadOperationalloc]initWithURL:@"http://www.bart.gov/dev/eta/bart_eta.xml"delegate:self];
//[queue cancelAllOperations];
[queue addOperation:downloadOperation];
//添加到queue中,并重写start方法,线程会叠加,卡死。
//暂时解决方法:1 不重写start(),finish() 和executing()方法,只重写main方法。
2 需重写start、finish和 executing方法
一般加入线程队列,不需开启并发concurrent. 开启并发则不加入队列,用 [operation start]
[downloadOperation release];
NSLog(@"%i",[queueoperationCount]);
[selftransitionView];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.tableView.UserInteractionEnabled=NO;
DownloadOperation *downloadOperation=[[DownloadOperationalloc]initWithURL:@"http://www.bart.gov/dev/eta/bart_eta.xml"delegate:self];
[downloadOperation start];
[downloadOperation release];
/*需重载
- (BOOL)isFinished;
- (BOOL)isConcurrent;
- (BOOL)isExecuting;
并设定开关条件
执行:
[self willChangeValueForKey:@"isExecuting"];
executing = YES;
[self didChangeValueForKey:@"isExecuting"];
结束:
[self finish];
*/
[self transitionView];
}