Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在ASINetworkQueue中设置委托自身和使用委托。请求完成时找不到选择器_Ios_Objective C_Asihttprequest - Fatal编程技术网

Ios 在ASINetworkQueue中设置委托自身和使用委托。请求完成时找不到选择器

Ios 在ASINetworkQueue中设置委托自身和使用委托。请求完成时找不到选择器,ios,objective-c,asihttprequest,Ios,Objective C,Asihttprequest,我有以下课程: 文件\u Downloadmanager.h: #import "ASINetworkQueue.h" @interface File_Downloadmanager : NSObject { } -(void)addRequestToDownloadQueue:(NSString*)objectID :(NSString*)userID :(NSString*)filename; -(void)initDownloadQueue; // creates a new down

我有以下课程:

文件\u 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
文件\u Downloadmanager.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];
}
-(无效)下载文件{
File_Downloadmanager*downloadhandler=[[File_Downloadmanager alloc]init];
//初始化下载队列
[downloadhandler initDownloadQueue];
对于(int i=0;i<[meetingObjects count];i++){
..获取objectID、userID等的一些其他代码。
[downloadhandler addRequestToDownloadQueue:ID:[loginData stringForKey:@“userId”]:[NSString stringWithFormat:@“%@%@”,currentObject.ID,currentObject.name]]
}
[下载处理程序开始下载];
}
一切正常,下载开始。但是当下载第一个文件时,我在ASINetworkQueue类中得到一个错误,我的选择器“requestFinished”无法调用(我没有确切的消息,目前无法启动应用程序,但失败代码为exc\u bad\u access code=1)


我的文件\u Downloadmanager对象的声明/初始化时间是否是问题所在?因为调用了函数“downloadFiles”,所以创建了DownloadManager对象,添加了请求,然后是“downloadFiles”方法返回,因为队列是异步工作的?

它看起来像是ASINetworkQueue试图向其发送requestFinished消息的downloadhandler对象在向其发送消息时不再存在,因为它可能在downloadFiles方法完成执行时被解除分配。与其将downloadhandler对象设置为downloadFiles方法的本地对象,不如将其设置为包含downloadFiles方法的类中的(强、非原子)属性。这样,您就可以确保在调用requestFinished时它仍然存在。

我以前没有使用过ASI网络功能,但是在网上看到了很多关于它的引用

在我看来,ASINetworkQueue类希望它的委托符合特定的协议。如果设置正确,则当您尝试将自己指定为ASINetworkQueue对象的委托,但尚未声明您的类符合相应的协议时,应该会收到警告。如果您确实包含协议声明,那么您应该得到一个警告,说明您没有从该协议实现所需的方法

尝试清理项目和重建,然后仔细查找警告,特别是在您的线路上:

[[self downloadQueue] setDelegate:self];
编辑:我刚刚下载了一个ASIHTTPRequest项目,令我沮丧的是,ASINetworkQueue类的委托属性不必符合特定的协议。这是糟糕的编程风格。如果设置委托,则应使委托指针符合特定协议


此外,请注意,ASI网络课程已经有好几年没有维护了,而且已经严重过时。还有更好的选择,你应该考虑转移到另一个网络框架。

谢谢,这就是我的想法!我会尽快试一试。是否可以使整个File_Downloadmanager类保持静态?或者用另一种方法来提供我的整个应用程序这个类,这样我就可以随时随地访问它了?你可以在创建它们时将它们粘贴到数组或字典中,然后在你不再需要它们时立即删除它们。但是如果我的方法调用是“嵌套”的呢。例如,我有一个类A,它创建一个对象ob Class B并调用一个方法“mb”ob Class B。在方法“mb”中,创建File_Downloadmanager对象并开始下载。即使File_Downloadmanager对象是类B的属性,它也不起作用,因为类B对象也已被销毁。明白我的意思了吗?谢谢!我知道这个图书馆已经过时了,我们一有时间就会寻找替代品。谈到备选方案:你更喜欢一个具体的方案吗?我倾向于推出自己的网络代码,但正如洛特斯卡森所说,AFNetworking很受欢迎。