试图理解异步调用

时间:2016-04-02 14:04:45

标签: ios objective-c asynchronous nsurlsession

我有这个方法

- (NSString*) createUserWithName:(NSString*)TheName
{
    NSURL *URL =someUrlthatIncludesTheName
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"GET"];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *URL = [NSURL URLWithString:url];
    NSURLSessionTask *task = [session dataTaskWithURL:URL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (response) {
            NSError* error = nil;
            NSArray *output = [NSJSONSerialization JSONObjectWithData:data
                                                              options:NSJSONReadingMutableContainers
                                                                error:&error];
            myID = [[output objectAtIndex:0] objectForKey:@"UserID"];
        }
    }];
    [task resume];
    return myID;
}

和另一种方法

-(void)doSomethingWith: (NSString*) anID

在我的代码中的某处,我随后称这些方法为:

[self createUserWithName:@"John"];
[self doSomethingWith:myID];

但是,由于createUserWithName:中的 NSURLSession 是异步的,因此使用 myID =(null)触发doSomethingWith:。< / p>

解决此问题的最佳方法是什么,而不必回到已弃用的同步NSURLConnection?

提前致谢

1 个答案:

答案 0 :(得分:4)

工作流程应该是

- (void)createUserWithName:(NSString*)TheName
{
    NSURL *URL =someUrlthatIncludesTheName
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"GET"];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *URL = [NSURL URLWithString:url];
    NSURLSessionTask *task = [session dataTaskWithURL:URL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (response) {
            NSError* error = nil;
            NSArray *output = [NSJSONSerialization JSONObjectWithData:data
                                                              options:NSJSONReadingMutableContainers
                                                                error:&error];
            myID = [[output objectAtIndex:0] objectForKey:@"UserID"];
            [self doSomethingWith:myID];
        }
    }];
    [task resume];
}

呼叫只是

[self createUserWithName:@"John"];

方法doSomethingWith:在完成块中异步执行。

或者使用自定义完成块

- (void)createUserWithName:(NSString *)theName completion:^(NSString *identifier)completion
{
    NSURL *URL =someUrlthatIncludesTheName
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"GET"];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *URL = [NSURL URLWithString:url];
    NSURLSessionTask *task = [session dataTaskWithURL:URL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (response) {
            NSError* error = nil;
            NSArray *output = [NSJSONSerialization JSONObjectWithData:data
                                                            options:NSJSONReadingMutableContainers
                                                             error:&error];
            myID = [[output objectAtIndex:0] objectForKey:@"UserID"];
            completion(myID);
        }
    }];
    [task resume];
}

并用

调用它
[self createUserWithName:@"John" completion:^(NSString *identifier) {
    [self doSomethingWith:identifier];
}];