gpt4 book ai didi

ios - AF网络 2 : canceling batch requests

转载 作者:可可西里 更新时间:2023-11-01 06:09:07 25 4
gpt4 key购买 nike

我正在使用 AFNetworking2 代码来处理请求。我从示例代码中复制并粘贴,并将上传操作更改为下载。我需要取消 Controller 消失的下载操作。我正在尝试实现取消:

    [[self.imagesQueue.operations 
filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings)
{ return [evaluatedObject isKindOfClass:[AFHTTPRequestOperation class]]; }]] makeObjectsPerformSelector:@selector(cancel)];

批量请求(下载图片):

-(void) startDownloadImages {

NSMutableArray *mutableOperations = [NSMutableArray array];
//self.downloadOperations = [NSMutableArray array];
for (NSString *str in _project.frames) {

NSURLRequest *request =
[[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET"
URLString:str
parameters:nil];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"OK %@", str);


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"FAILS %@", str);
}];

[mutableOperations addObject:operation];
}

NSArray *operations =
[AFURLConnectionOperation batchOfRequestOperations:mutableOperations
progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"%lu of %lu complete", (unsigned long)numberOfFinishedOperations, (unsigned long)totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
NSLog(@"All operations in batch complete");

}];

self.imagesQueue = [[NSOperationQueue alloc] init];
self.imagesQueue.name = @"com.imagesqueue";
self.imagesQueue.maxConcurrentOperationCount = 1;

[self.imagesQueue addOperations:operations waitUntilFinished:NO];
//[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
}

一系列启动和取消导致 EXC_BREAKPOINT(代码=EXC_ARM_BREAKPOINT,子代码=0xdefe):

libdispatch.dylib`dispatch_group_leave:
0x37e1e7d8: dmb ishst
0x37e1e7dc: ldrex r1, [r0, #40]
0x37e1e7e0: adds r1, #1
0x37e1e7e2: strex r2, r1, [r0, #40]
0x37e1e7e6: cmp r2, #0
0x37e1e7e8: bne 0x37e1e7dc ; dispatch_group_leave + 4
0x37e1e7ea: cmp.w r1, #4294967295
0x37e1e7ee: ble 0x37e1e7fe ; dispatch_group_leave + 38
0x37e1e7f0: mvn r2, #2147483648
0x37e1e7f4: cmp r1, r2
0x37e1e7f6: it eq
0x37e1e7f8: beq.w 0x37e21df8 ; _dispatch_group_wake
0x37e1e7fc: bx lr
0x37e1e7fe: trap
0x37e1e800: nop
0x37e1e802: nop

有什么建议吗?

最佳答案

尝试将 str 声明为 block 变量(您正在 block 中访问它):

__block NSString *str;

for (str in _project.frames) {

你也可以尝试在执行取消选择器之前检查操作是否已经完成。

在 View 将消失的方法中,您可以调用图像队列上的 cancelAllOperations 来取消所有剩余的操作。

关于ios - AF网络 2 : canceling batch requests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20748977/

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