gpt4 book ai didi

ios - 如何在这种情况下管理 block

转载 作者:行者123 更新时间:2023-11-29 00:58:15 25 4
gpt4 key购买 nike

我正在为 Instagram API 实现辅助类。

我有一个获取访问码的方法:

-(void) makeAuthenticateRequestWithBlock: (completionBlock) block {
if (![self.viewController.view.subviews containsObject: self.webView]) {
[self.viewController.view addSubview: self.webView];
}
if (!self.isUserAuthinticated && self.isAvailableAllRequredData) {
NSString * fullURL = [NSString stringWithFormat: @ "%@?client_id=%@&redirect_uri=%@&response_type=token&scope=likes+comments+basic+public_content+follower_list", KAUTHURL, self.clientId, self.redirectURL];
NSURL * url = [NSURL URLWithString: fullURL];
NSURLRequest * request = [NSURLRequest requestWithURL: url];
[self.webView loadRequest: request];
self.block = block;
}
}

typedef void(^completionBlock) (BOOL success); 是一个 block ,我有:

@property (copy, nonatomic) completionBlock block ;

在 webview 委托(delegate)方法中。

-(BOOL) webView: (UIWebView * ) webView shouldStartLoadWithRequest: (NSURLRequest * ) request navigationType: (UIWebViewNavigationType) navigationType {
NSString * urlString = [
[request URL] absoluteString
];
NSLog(@ "URL STRING : %@ ", urlString);
NSArray * UrlParts = [urlString componentsSeparatedByString: [NSString stringWithFormat: @ "%@/", self.redirectURL]];
if ([UrlParts count] > 1) {
// do any of the following here
urlString = [UrlParts objectAtIndex: 1];
NSRange accessToken = [urlString rangeOfString: @ "#access_token="];
if (accessToken.location != NSNotFound) {
NSString * strAccessToken = [urlString substringFromIndex: NSMaxRange(accessToken)];
NSLog(@ "access token = %@ ", strAccessToken);
[self.webView removeFromSuperview];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[
[NSUserDefaults standardUserDefaults] setValue: strAccessToken forKey: kAccessToken
];
[
[NSUserDefaults standardUserDefaults] synchronize
];
self.isUserAuthinticated = true;
self.block(true);
}
return NO;
}
return YES;
}

一切正常。

现在,当我向 Instagram API 发送请求时,我会检查用户是否已通过身份验证。

-(void) getInformationOfOwnWithCompletionBlock: (dataTaskBlock) block {
if (self.isUserAuthinticated) {
NSString * urlString = [NSString stringWithFormat: @ "%@self/?access_token=%@", kUSERURL, [
[NSUserDefaults standardUserDefaults] valueForKey: kAccessToken
]];
[self makeRequestToServerWithUrl: urlString completionBlock: ^ (NSDictionary * data, NSError * error) {
if (!error) {
block(data, error);
} else {
block(nil, error);
}
}];
} else {
[self makeAuthenticateRequestWithBlock: ^ (BOOL success) {
[self getinformationofOwnFollowsWithCompletionBlock: block];
}];
// block(nil,errorMsg);
}
}

它将发送一个请求并使用返回 block 构建 webview。所有这些都工作正常。但是当我两次调用相同的方法时,我收到以下错误消息:

[self.instagramVC getInformationOfOwnWithCompletionBlock: ^ (NSDictionary * data, NSError * error) {
NSLog(@ "%@", data);
}];
[self.instagramVC getInformationOfOwnWithCompletionBlock: ^ (NSDictionary * data, NSError * error) {
NSLog(@ "DATA 2%@", data);
}];

它返回最后调用的方法的 block ,如输出将是 DATA 2。我知道原因,这是因为 self.block。所以请给我一个建议,这样我就不必为每个请求都使用全局 block 。

最佳答案

正如您所说,您的问题是您对所有请求使用全局 block 。您需要设置一个数据结构来保存有关每个待处理请求的信息。也许是一个可变的字典数组,其中每个字典包含请求和该请求的完成 block 。然后,当请求完成时,将该请求与您的请求数组相匹配,并执行适当的完成 block 。

关于ios - 如何在这种情况下管理 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37362009/

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