从同步更改为异步

时间:2015-11-16 15:15:50

标签: ios objective-c asynchronous

在这里,我使用GET方法从一个Url获取数据并在表格视图中显示数据。我使用synchronous来显示数据。

需要:

我需要将此代码更改为Asynchronous以在表视图中显示相同的数据。我只是使用我的控制台来显示数据。

这是我的代码:

#import "ViewController.h"
#import <CommonCrypto/CommonDigest.h>
@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    [self getdata];
}

-(NSString*)sha256HashFor:(NSString*)input
{
    const char* str = [input UTF8String];
    unsigned char result[CC_SHA256_DIGEST_LENGTH];
    CC_SHA256(str, strlen(str), result);

    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
    for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
    {
        [ret appendFormat:@"%02x",result[i]];
    }
    return ret;
}

-(void)getdata {

    NSString *userName = @"user@gmail.com";
    NSString *password = @"passer";
    //encoding
    NSData *Data = [password dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [Data base64EncodedStringWithOptions:0];

    base64String=[self sha256HashFor: base64String];

    NSString *urlString = @"https://api.exampleurl/user23/doc";

    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"GET"];

    NSString *authStr = [NSString stringWithFormat:@"%@:%@", userName, base64String];
    NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];

    NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", str);

  }  
  • 在此代码中,我使用了base64和shaa256编码。

  • 任何人都可以帮我处理异步。我不知道该怎么做 那。请帮我解释代码。 提前致谢!

1 个答案:

答案 0 :(得分:4)

要制作异步http请求,您可以使用:

[NSURLConnection sendAsynchronousRequest:httpRequest queue:[AppDelegate connectionQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
    ....
}];

或者您可以实施NSURLConnectionDataDelegate协议

更新答案:

  1. 首先在app delegate file

    中创建一个连接队列

    + (NSOperationQueue *)connectionQueue { dispatch_once(&once, ^{ connectionQueue = [[NSOperationQueue alloc] init]; [connectionQueue setMaxConcurrentOperationCount:2]; [connectionQueue setName:@"com.XYZ.connectionqueue"]; }); return connectionQueue; }

  2. 这里更新了getData方法:

    -(void)getdata {
    
    NSString *userName = @"user@gmail.com";
    NSString *password = @"passer";
    //encoding
    NSData *Data = [password dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64String = [Data base64EncodedStringWithOptions:0];
    
    base64String=[self sha256HashFor: base64String];
    
    NSString *urlString = @"https://api.exampleurl/user23/doc";
    
    NSMutableURLRequest *request= [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"GET"];
    
    NSString *authStr = [NSString stringWithFormat:@"%@:%@", userName, base64String];
    NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]];
    [request setValue:authValue forHTTPHeaderField:@"Authorization"];
    

    __阻止NSString * str;

     [NSURLConnection sendAsynchronousRequest:httpRequest queue:[AppDelegate connectionQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
    str = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];}];
    NSLog(@"%@", str);
    

    }