gpt4 book ai didi

ios - NSOperationQueue 中类的对象 - 使用委托(delegate)的异步方法

转载 作者:搜寻专家 更新时间:2023-10-30 20:17:15 25 4
gpt4 key购买 nike

我创建了一个 NSOperation 的子类,名为 DownloadQueue。我将所有对象一一添加到 NSOperationQueue,即 appDelegate.queueDownload

问题是当 DownloadOperation 的对象被执行时,一旦 DownloadOperationdownload 方法执行,我的 DownloadOperation 从队列中删除。

但我必须将它保存在内存中,因为当我在 DownloadOperation.m 中进行异步调用 [using downloadFromPath] 并且服务器响应文件数据时,当时DownloadOperation 对象被释放并且 liveOperationSucceeded 从未被调用。

可能是我觉得这个问题有点复杂,所以在标记它之前请先阅读它两遍!!这对我很重要。感谢理解。

    // Adding to queue

DownloadOperation *operation = [[DownloadOperation alloc] init];

operation.delegate = self;

[operation operationWithFileOrFolderID:dictDocument[@"id"] withParentPath:self.strParentPath download:NO withFileName:dictDocument[@"name"] withDictionary:dictDocument];
[operation download];

[operation setQueuePriority:NSOperationQueuePriorityVeryHigh];

[appDelegate.queueDownload addOperation:operation];

DownloadOperation.m (which is Subclass of NSOperation)

@implementation DownloadOperation

-(void)main
{
[self download];
}

- (void)operationWithFileOrFolderID: (NSString *)strID withParentPath: (NSString *)strParentPath download: (BOOL)isDownload withFileName: (NSString *)strFileName withDictionary: (NSMutableDictionary *)dictInfoLocal
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
self.documentsDirectory = paths[0];

self.strFileName = strFileName;
self.strID = strID;
self.strParentPath = strParentPath;
self.dictInfo = dictInfoLocal;

if (self)
{

}
}

- (void)download
{
self.liveClient = [[LiveConnectClient alloc] initWithClientId:CLIENT_ID delegate:self];

[self.liveClient downloadFromPath:[self.strID stringByAppendingString:@"/content"] delegate:self];

NSLog(@"DownloadFilesVC : Downloading : %@",self.strFileName);
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{

}

- (void) liveOperationFailed:(NSError *)error
operation:(LiveOperation *)operation
{

}

- (void) liveDownloadOperationProgressed:(LiveOperationProgress *)progress data:(NSData *)receivedData operation:(LiveDownloadOperation *)operation
{

}

@end

最佳答案

您在一个操作中有一个异步操作。

操作队列正在做它应该做的事情,并且在方法 download 完成后弹出完成的 DownloadOperation。它不关心操作是否有回调。

您需要保留 DownloadOperation 操作的所有权,方法是将它添加到强引用集合(例如 NSArrayNSSet)并在指定位置丢弃它LiveConnectClient 完成后的点。您可能希望在 DownloadOperation 中添加一个 BOOL 属性。

@property BOOL imReallyReallyFinishedAndCanBeDiscardedOK;

编辑

API 是基于异步/委托(delegate)的。一种简单的方法是遍历文件夹内容并计算返回响应。一旦您收到与请求数量相匹配的回复数量,您就知道您已经完成了。

作为一个非常简化的版本,不关心递归或错误。

@interface DownloadManager  : NSObject

@property NSArray *fileRefList;
@property NSUInteger count;
@property (strong) void (^completionBlock)();

@end

@implementation DownloadManager

-(void)downloadAllTheFiles {

self.count = 0;
for(NSString *strID in self.fileRefList) {

LiveConnectClient *liveClient = [[LiveConnectClient alloc] initWithClientId:CLIENT_ID delegate:self];
[liveClient downloadFromPath:[strID stringByAppendingString:@"/content"] delegate:self];

}

}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
self.count++;
[self haveIFinished];
}

- (void) liveOperationFailed:(NSError *)error
operation:(LiveOperation *)operation
{
self.count++;
[self haveIFinished];
}

-(void)haveIFinished {

if (self.count == self.fileRefList.count) {
if (self.completionBlock) {
self.completionBlock();
}
}

}

让 API 为您执行队列操作和后台操作。完成后触发任意 block 。

关于ios - NSOperationQueue 中类的对象 - 使用委托(delegate)的异步方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25500423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com