警告-[UIApplication委托]必须仅在主线程中使用

时间:2019-10-23 07:11:03

标签: ios objective-c

我收到警告,仅在下面的代码行中才能从主线程使用[UIApplication委托]

  

(((AppDelegate *)[[UIApplication sharedApplication]   委托])。loginProfile.accessToken;

以下是我的代码。

+ (NSString *)accessTokenHashForDate:(NSDate *)date withParameters:(NSArray *)params{

    NSString *accessToken = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).loginProfile.accessToken;

    NSString *paramsStr = [params componentsJoinedByString:@""];
    NSString *hashStr = [NSString stringWithFormat:@"%@%@%@%@", [CommonUtil IMEI], [date agileHashFormattedString], (!paramsStr) ? @"" : paramsStr, accessToken];
    return hashStr
}

谁能告诉我如何删除此警告消息?

1 个答案:

答案 0 :(得分:0)

+ (NSString *)accessTokenHashForDate:(NSDate *)date withParameters:(NSArray *)params{
__block NSString *accessToken = NULL;
if ([NSThread isMainThread]) {
        accessToken = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).loginProfile.accessToken;


} else {
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    dispatch_async(dispatch_get_main_queue(), ^{
        accessToken = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).loginProfile.accessToken;
        dispatch_semaphore_signal(semaphore);
    });
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    dispatch_semaphore_signal(semaphore);
}
NSString *paramsStr = [params componentsJoinedByString:@""];
           NSString *hashStr = [NSString stringWithFormat:@"%@%@%@%@", [CommonUtil IMEI], [date agileHashFormattedString], (!paramsStr) ? @"" : paramsStr, accessToken];
return hashStr;

}