0
votes

I want to call following code with return statement in main thread but I got below error.

Incompatible block pointer types passing 'NSString *(^)(void)' to parameter of type 'dispatch_block_t _Nonnull' (aka 'void (^)(void)')

Following is my code.

NSString *hash = [AGMobileService accessTokenHashForDate:date withParameters:[NSArray new]];


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

    dispatch_async(dispatch_get_main_queue(), ^{
      NSString *accessToken = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).loginProfile.accessToken;

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

+ (NSString *)computeHash:(NSString *)text usingHash:(NSString*)hashSalt {

    NSString *textToEncode = [NSString stringWithFormat:@"%@%@", hashSalt, text];
    NSData *data = [textToEncode dataUsingEncoding:NSUTF8StringEncoding];
    unsigned char hash[CC_SHA256_DIGEST_LENGTH];
    if ( CC_SHA256([data bytes], (int) [data length], hash) ) {
        NSData *sha256 = [NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH];
        return [sha256 base64EncodedString];
    }
    return nil;
}
1
and which exactly line is it? what is computeHash?Lu_
And most most importantly why are you returning asynchronously from synchronous function, how do you want it to work properly?Lu_
@Lu_ comuteHash is the class method which returns string. please check edited answer.iPhone
@Lu_ I got warning message like [[UIApplication sharedApplication] delegate] must be in main thread. So, I am trying to call that part in main thread.iPhone
remove dispatching to make it work, you have to decide are you doing it synchronously or notLu_

1 Answers

0
votes

I have encountered things like this, I just remove the return code in the dispatch block, but I do not know the reason.So I also want to know the reason.