2013-06-02 37 views
1

我有以下問題。 我用我所有的函數做了一個API類,它們在url請求完成時返回結果。MBProgressHUD和NSURLConnection

我想在做出請求之前創建一個加載器,並在完成之後將其移除。

我的請求代碼是這樣的:

-(NSDictionary *)makeApiCall:(NSString *)function params:(NSDictionary *)params view:(UIView *)displayView{ 

__block NSDictionary *json = nil; 

[self makeRequestTo:function parameters:params successCallback:^(id jsonResponse) { 
    json = jsonResponse; 
} errorCallback:^(NSError *error, NSString *errorMsg) { 
    NSLog(@"Error: %@", errorMsg); 
}]; 


return json; 

} 


- (void) makeRequestTo:(NSString *) function 
     parameters:(NSDictionary *) params 
    successCallback:(void (^)(id jsonResponse)) successCallback 
    errorCallback:(void (^)(NSError * error, NSString *errorMsg)) errorCallback { 

NSURLResponse *response = nil; 
NSError *error = nil; 

AppDelegate* appDelegate = [UIApplication sharedApplication].delegate; 
[appDelegate setLoadingEnabled:true] 
NSURL *url = [NSURL URLWithString:kBaseUrl]; 

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 

httpClient.parameterEncoding = AFFormURLParameterEncoding; 

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:[NSString stringWithFormat:@"%@/%@",kApiBaseUrl,function] parameters:params]; 

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

//NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    if(error) { 
     errorCallback(error, nil); 
    } else { 
     id JSON = AFJSONDecode(data, &error); 
     successCallback(JSON); 
    } 
}]; 

} 

但..這是行不通的。 加載程序僅在請求完成後纔會顯示,並在一秒鐘內刪除。

有趣的事情,如果我從這個Api類叫出來,它的工作原理.. 任何人都可以幫我嗎?

感謝

回答

2

更新用戶界面只完成當程序控制返回到主 runloop。但同步呼叫

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

阻塞主線程,並且在請求完成之前不會返回。因此沒有UI更新完成,特別是您的加載程序不顯示。

你可以使用sendAsynchronousRequest,而不是和處理響應(和隱藏裝載機)在完成塊,所以代碼將大致是這樣的:

- (void) makeRequestTo:(NSString *) function 
      parameters:(NSDictionary *) params 
     successCallback:(void (^)(id jsonResponse)) successCallback 
     errorCallback:(void (^)(NSError * error, NSString *errorMsg)) errorCallback { 

    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate; 
    [appDelegate showLoader]; 

    // ... create request ... 

    NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
     [appDelegate hideLoader]; 

     // ... handle response ... 

    }]; 
} 

但是還有一個問題,您的makeApiCall:方法:由於makeRequestTo:異步工作,它幾乎立即返回。完成 與json = jsonResponse;塊被稱爲,當請求已完成。 因此json變量將始終爲nil

而且沒有辦法解決這個問題。您無法將異步調用封裝到同步方法 中,而不會阻塞該線程。特別是,makeApiCall:不能返回 響應字典。 你將不得不改變你的程序邏輯異步工作 。

+0

結果是一樣的。 更多提示? :/ –

+0

@TiagoAlmeida:也許你可以將新代碼添加到你的問題中,否則很難猜測問題出在哪裏。 –

+0

立即查看。 –