我有以下类(class):
File_Downloadmanager.h:
#import "ASINetworkQueue.h"
@interface File_Downloadmanager : NSObject {
}
-(void)addRequestToDownloadQueue:(NSString*)objectID :(NSString*)userID :(NSString*)filename;
-(void)initDownloadQueue; // creates a new download queue and sets delegates
-(void)startDownload; // starts the download queue
-(void)requestFinished;
-(void)requestFailed;
-(void)queueFinished;
@property(retain) ASINetworkQueue *downloadQueue;
@end
文件下载管理器.m:
@implementation File_Downloadmanager
@synthesize downloadQueue;
-(void)initDownloadQueue{
NSLog(@"Init DownloadQueue");
// Stop anything already in the queue before removing it
[[self downloadQueue] cancelAllOperations];
[self setDownloadQueue:[ASINetworkQueue queue]];
[[self downloadQueue] setDelegate:self];
[[self downloadQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self downloadQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self downloadQueue] setQueueDidFinishSelector:@selector(queueFinished:)];
[self downloadQueue].shouldCancelAllRequestsOnFailure = NO;
}
-(void)startDownload{
NSLog(@"DownloadQueue Go");
[downloadQueue go];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// If no more elements are queued, release the queue
if ([[self downloadQueue] requestsCount] == 0) {
[self setDownloadQueue:nil];
}
NSLog(@"Request finished");
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
// You could release the queue here if you wanted
if ([[self downloadQueue] requestsCount] == 0) {
[self setDownloadQueue:nil];
}
//... Handle failure
NSLog(@"Request failed");
}
- (void)queueFinished:(ASINetworkQueue *)queue
{
// You could release the queue here if you wanted
if ([[self downloadQueue] requestsCount] == 0) {
[self setDownloadQueue:nil];
}
NSLog(@"Queue finished");
}
-(void)addRequestToDownloadQueue:(NSString*)objectID :(NSString*)userID :(NSString*)filename{
...SourceCode for creating the request...
// add operation to queue
[[self downloadQueue] addOperation:request];
}
在另一个类中,一个函数在该函数内部被调用,我正在执行以下操作:
-(void)downloadFiles{
File_Downloadmanager * downloadhandler = [[File_Downloadmanager alloc]init];
// initialize download queue
[downloadhandler initDownloadQueue];
for (int i = 0; i < [meetingObjects count]; i++) {
....some other code to get the objectID, userID, etc.
[downloadhandler addRequestToDownloadQueue:ID :[loginData stringForKey:@"userId"] :[NSString stringWithFormat:@"%@%@",currentObject.id,currentObject.name]]
}
[downloadhandler startDownload];
}
一切正常,下载开始。但是当第一个文件被下载时,我在 ASINetworkQueue 类中得到一个错误,我的选择器“requestFinished”无法被调用(我没有确切的消息,目前无法启动应用程序,但失败代码是 exc_bad_access code=1)。
我的 File_Downloadmanager 对象的声明/初始化时间有问题吗?因为调用了函数“downloadFiles”,创建了 DownloadManager 对象,添加了请求,然后“downloadFiles”方法返回,因为队列是异步工作的?
最佳答案
我以前没有使用过 ASI 网络的东西,但在网上看到了很多关于它的引用。
在我看来,ASINetworkQueue 类希望它的委托(delegate)符合特定协议(protocol)。如果设置正确,当您尝试将自己指定为 ASINetworkQueue 对象的委托(delegate)但尚未声明您的类符合适当的协议(protocol)时,您应该会收到警告。如果您确实包含协议(protocol)声明,那么您应该会收到一条警告,提示您尚未从该协议(protocol)中实现所需的方法。
尝试清理您的项目并重新构建,然后仔细查看警告,特别是在您的行上:
[[self downloadQueue] setDelegate:self];
编辑:我刚刚下载了一个 ASIHTTPRequest 项目,令我沮丧的是,ASINetworkQueue 类的委托(delegate)属性不必符合特定协议(protocol)。这是糟糕的编程风格。如果设置委托(delegate),则应使委托(delegate)指针符合特定的协议(protocol)。
另外,请注意 ASI 网络类已经好几年没有维护了,而且已经严重过时了。有更好的替代方案,您应该考虑转向不同的网络框架。
关于ios - setDelegate self 并在 ASINetworkQueue 中使用委托(delegate)。请求完成后找不到选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21171840/