0

這是我的工作代碼NSURLConnection sendSynchronousRequest在Objective-C面朝大海,NSURLSessionDataTask問題與SynchronousRequest

+ (Inc*)getData:(NSString*)inUUID { 
    NSString* urlString = [NSString stringWithFormat:@"/inc/%@", incUUID]; 
    NSURLRequest* request = [[HttpRequest requestWithRelativePath:urlString] toNSMutableURLRequest]; 
    NSDictionary* json = [self getJSONForRequest:request]; 
    return [Inc incFromDictionary:json]; 
} 
+ (NSDictionary*)getJSONForRequest:(NSURLRequest*)request { 
    NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    return [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil]; 
} 

但是,sendSynchronousRequest:request已被棄用。

因此,我用NSURLSessionDataTask而不是sendSynchronousRequest。在這裏,是我的實現代碼:

+ (Inc*)getData1:(NSString*)inUUID { 
    NSString* urlString = [NSString stringWithFormat:@"/in/%@", inUUID]; 
    NSURLRequest* request = [[HttpRequest requestWithRelativePath:urlString] toNSMutableURLRequest]; 
    //NSDictionary* json = [self getJSONForRequest1:request]; 
    __block NSDictionary* json; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self getJsonResponse1:request success:^(NSDictionary *responseDict) { 
      json = [responseDict valueForKeyPath:@"detail"];; 
      //return [Inc incFromDictionary:json]; 

     } failure:^(NSError *error) { 
      // error handling here ... 
     }]; 
    }); 
    return [Inc incFromDictionary:json]; 
} 
+ (void)getJsonResponse1:(NSURLRequest *)urlStr success:(void (^)(NSDictionary *responseDict))success failure:(void(^)(NSError* error))failure 
{ 
    NSURLSessionDataTask *dataTask1 = [[NSURLSession sharedSession] dataTaskWithRequest:urlStr completionHandler:^(NSData *data, NSURLResponse *response, 
                                NSError *error) { 
     NSLog(@"%@",data); 
     if (error) 
      failure(error); 
     else { 
      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
      NSLog(@"%@",json); 
      success(json); 
     } 
    }]; 
    [dataTask1 resume]; // Executed First 
} 

問題是return語句呼叫getData1之前完成API調用。

在此先感謝。

+0

'dataTaskWithRequest'異步工作,您必須像'getJsonResponse1'中一樣實現完成處理程序。實際上,您可以爲這兩個處理程序傳遞相同的塊對象。 – vadian

+0

不明白。你能簡單解釋一下嗎? –

+0

即使我在getJsonResponse1中使用完成處理程序,它不起作用。 –

回答

1

正如評論中所提到的,您需要一個完成處理程序。

像這樣(未經):

+ (void)getData1:(NSString*)inUUID success:(void (^)(NSDictionary *responseDict))success failure:(void(^)(NSError* error))failure { 
    NSString* urlString = [NSString stringWithFormat:@"/in/%@", inUUID]; 
    NSURLRequest* request = [[HttpRequest requestWithRelativePath:urlString] toNSMutableURLRequest]; 

    [self getJsonResponse1:request success:^(NSDictionary *responseDict) { 
     NSDictionary* json = [responseDict valueForKeyPath:@"detail"]; 
     success(json); 

    } failure:^(NSError *error) { 
     failure(error); 
    }]; 
} 


+ (void)getJsonResponse1:(NSURLRequest *)urlStr success:(void (^)(NSDictionary *responseDict))success failure:(void(^)(NSError* error))failure 
{ 
    NSURLSessionDataTask *dataTask1 = [[NSURLSession sharedSession] dataTaskWithRequest:urlStr completionHandler:^(NSData *data, NSURLResponse *response, 
                                NSError *error) { 
     NSLog(@"%@",data); 
     if (error) 
      failure(error); 
     else { 
      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 
      NSLog(@"%@",json); 
      success(json); 
     } 
    }]; 
    [dataTask1 resume]; // Executed First 
} 

,並呼籲

[MyClass getData1:@"asdf" success:^(NSDictionary *responseDict) { 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     NSDictionary *json = [responseDict valueForKeyPath:@"detail"]; 
     Inc *inc = [Inc incFromDictionary:json]; 
     // do something witrh `inc` 

    }); 
} failure:^(NSError *error) { 
    // error handling here ... 
}]; 

考慮使用實例和類(ES)而不是僅僅類方法的實例方法。

+0

好吧,現在我明白了。讓我嘗試。 –

+0

感謝很多小小的變化,它的作用就像一個魅力。 –