gpt4 book ai didi

ios - 异步连接下载回调

转载 作者:行者123 更新时间:2023-11-29 10:41:45 25 4
gpt4 key购买 nike

我使用以下方法创建了一个类 customDownload:

-(NSString *) getTextFromLink: (PreliteRequest *) requestDetails
asyncConnection: (BOOL) isAsync
callbackMethod: (SEL) methodToExecute {

mainRequest = requestDetails;

NSMutableURLRequest *postRequest = [[NSMutableURLRequest alloc] init];
NSURLRequest *getRequest = [[NSURLRequest alloc] init];
NSURLConnection *connection;

NSURLResponse * response = nil;
NSError * error = nil;

if ([[requestDetails getType] isEqualToString:@"POST"]) {
[postRequest setURL:[NSURL URLWithString:[requestDetails getUrl]]];
[postRequest setHTTPMethod:[requestDetails getType]];
[postRequest setValue:[requestDetails getPostLenght] forHTTPHeaderField:@"Content-Length"];
[postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[postRequest setHTTPBody:[requestDetails getPostParameters]];

if (isAsync) {
tmpMethod = methodToExecute;
connection = [[NSURLConnection alloc] initWithRequest:postRequest delegate:self];
} else
downloadedData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
} else {
getRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[requestDetails getUrl],[requestDetails getGetParameters]]]];
if (isAsync) {
tmpMethod = methodToExecute;
connection = [[NSURLConnection alloc] initWithRequest:getRequest delegate:self];
} else
downloadedData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:getRequest returningResponse:&response error:&error];
}

NSString *result=[[NSString alloc]initWithData:downloadedData encoding:NSUTF8StringEncoding];

return result;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
downloadedData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[downloadedData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {


return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

NSString *tmpResult = [[NSString alloc]initWithData:downloadedData encoding:NSUTF8StringEncoding];

[self performSelector:tmpMethod withObject:tmpResult];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSLog(@"Connection error: %@",error);
}

在我的 View Controller 中,我声明了前一个类并调用该类的唯一方法 getTextFromLink。

download = [[customDownload alloc] init];
[download getTextFromLink:request asyncConnection:YES callbackMethod:tmpSelector];
SEL tmpSelector = @selector(printResult:);
-(void) printResult:(NSString *) resultToPrint {
NSLog(@"Risultato: %@",resultToPrint);
}

我将 tmpSelector 作为参数传递给 getTextFromLink,因为这是我想在 getTextFromDownloadLink 完成其工作后立即调用的方法。实际上 getTextFromLink 执行一个异步连接。

我想做的是在异步连接完成下载数据时执行一些操作。我想创建一个回调自定义类来执行此操作。

谁能帮帮我?

最佳答案

而不是这个选择器模型,通常人们会为此使用 block 。例如,为您的 block 定义一个 typedef:

typedef void(^PreliteRequestCompletionHandler)(NSString *string);

由于您正在处理异步模式,您可能希望定义一个属性,您可以使用该属性来保存此完成处理程序以便稍后调用:

@property (nonatomic, copy) PreliteRequestCompletionHandler completionHandler;

然后您可以将该选择器参数更改为 block 参数:

-(NSString *) getTextFromLink: (PreliteRequest *) requestDetails
asyncConnection: (BOOL) isAsync
completionHandler: (PreliteRequestCompletionHandler)completionHandler {

self.completionHandler = completionHandler;

// do stuff
}

然后,当您想调用该完成 block 时,您可以执行以下操作:

NSString *result = ...;

if (self.completionHandler) {
self.completionHandler(result);
}

然后您现在可以在您的方法中使用这个新的 block 参数:

download = [[customDownload alloc] init];
[download getTextFromLink:request asyncConnection:YES completionHandler:^(NSString *result) {
NSLog(@"Risultato: %@", result);
}];

关于ios - 异步连接下载回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24252581/

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