0

我使用塊從一個類的響應中獲取頭字段,並且我必須在另一個類中獲取它。如何立即獲得塊結果?

我實現這樣的

在第一類代碼:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UserAuthentication *auth = [[UserAuthentication alloc]init]; 
    NSDictionary *dict = [auth getUserConfiguration]; 
    NSLog(@"%@",dict); 
} 

在userAuthentication類:

-(NSDictionary *)getUserConfiguration; 
{ 
    __block NSDictionary *resultDictionary; 
    NSURLSession *session = [NSURLSession sharedSession]; 
    [[session dataTaskWithURL:[NSURL URLWithString:@"http://72.52.65.142:8083/auth"] 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *error) { 
       NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; 
       if ([response respondsToSelector:@selector(allHeaderFields)]) { 
        resultDictionary = [httpResponse allHeaderFields]; 
        NSLog(@"%@",resultDictionary); 
       } 
      }] resume]; 
    NSLog(@"%@",resultDictionary); 
    return resultDictionary; 
} 

這裏我的問題是頭等我越來越dict爲空。

即使在userAuthentication類也是我變空。

但經過一段時間回調方法正在調用,然後我可以在completionHandler正確看到響應。

那麼我如何才能在firstClass中獲得響應?

回答

2

您誤解了在後臺線程中運行的異步操作的基本原理,並且操作完成時,它將爲您提供完成塊中的數據。

要在viewDidLoad第二類的方法中獲得響應,您需要使用塊。像下面

-(void)getUserConfigurationOnCompletion:(void (^)(NSDictionary *))completion 
{ 
    __block NSDictionary *resultDictionary; 
    NSURLSession *session = [NSURLSession sharedSession]; 
    [[session dataTaskWithURL:[NSURL URLWithString:@"http://72.52.65.142:8083/auth"] 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *error) { 
       NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; 
       if ([response respondsToSelector:@selector(allHeaderFields)]) { 
        resultDictionary = [httpResponse allHeaderFields]; 
        // Call completion with parameter 
        completion(resultDictionary); 
       } 
      }] resume]; 
} 

,並使用它像這樣在viewDidLoad中

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UserAuthentication *auth = [[UserAuthentication alloc]init]; 
    [auth getUserConfigurationOnCompletion:^(NSDictionary *dict){ 
    // do necessary work with response dictionary here 
    NSLog(@"%@",dict); 

    }]; 
} 
+0

得到這個錯誤:函數'完成'的隱式聲明在C99中是無效的 – Himanth

+0

所以我只好改變完成操作完成操作 – Himanth

+0

是的,這是一個錯字錯誤,對不起。 –

1

您正在使用NSURLSession!它在後臺線程上執行任務! 只有當您從服務器獲得響應時纔會調用完成塊。當然,完成請求需要時間。您應該使用塊完成請求並在完成時返回結果。

-(void)getUserConfigurationAndOnCompletion:(void(ˆ)(NSDictionary *dict, NSError *error))completion; 
{ 
    __block NSDictionary *resultDictionary; 
    NSURLSession *session = [NSURLSession sharedSession]; 
    [[session dataTaskWithURL:[NSURL URLWithString:@"http://72.52.65.142:8083/auth"] 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *error) { 
       NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; 
       if ([response respondsToSelector:@selector(allHeaderFields)]) { 
        resultDictionary = [httpResponse allHeaderFields]; 
        NSLog(@"%@",resultDictionary); 

       //This will call the block in the first class with the result dictionary 

       dispatch_async(dispatch_get_main_queue(), ^{ 
        if(!error){ 
         completion(resultDictionary,nil); 
        }else{ 
         completion(nil,error); 
        } 
       }); 

     }] resume]; 
} 

當您從第一個類調用上述代碼時,它將在那裏創建一個塊,並且您將在塊參數中獲得所需的字典!

+0

如何當您嘗試調用函數調用頭等 – Himanth

+0

這種方法,自動填充展示函數名稱,然後autoComplete將寫入整個函數。 但是,你應該看看獅子的答案,以瞭解它。 – Rikh

1

你的方法應該是這樣的,

-(void)getUserConfigurationwithCompletionHandler : (void (^)(NSDictionary* resultDictionary))completionHandler 
{ 
__block NSDictionary *resultDictionary; 
NSURLSession *session = [NSURLSession sharedSession]; 
[[session dataTaskWithURL:[NSURL URLWithString:@"http://72.52.65.142:8083/auth"] 
     completionHandler:^(NSData *data, 
          NSURLResponse *response, 
          NSError *error) { 
      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; 
      if ([response respondsToSelector:@selector(allHeaderFields)]) { 
       resultDictionary = [httpResponse allHeaderFields]; 
       NSLog(@"%@",resultDictionary); 

       completionHandler(resultDictionary); 

      } 
     }] resume]; 
    NSLog(@"%@",resultDictionary); 

} 

,您可以訪問它像,

- (void)viewDidLoad { 
     [super viewDidLoad]; 

     [self getUserConfigurationwithCompletionHandler:^(NSDictionary *resultDictionary) { 

    // you can acess result dictionary here 

    NSLog(@"%@",resultDictionary); 

    }]; 

} 

,因爲你會在web服務(從服務器)的響應中獲取數據,因此需要一些時間來完成,所以你必須從webservice調用的完成處理程序返回數據,並且不能從完成處理程序返回數據,因此必須創建自己的完成處理程序和調用,如上所述。你可以訪問resultDictionarycompletionHandler,你可以從這個completionHandler顯示新的VC。

2

這東西你必須去習慣:這是有關互聯網接入(有些東西不是與之相關的)任何不能立即返回 - 除非您願意等待,請阻止您的用戶界面,並讓您的用戶非常非常不高興。

您必須以這樣一種方式編寫應用程序,使其可以處於四種狀態:從不詢問用戶配置,詢問用戶配置,詢問並接收用戶配置,或詢問用戶配置和失敗。在這種情況下,您的觀點必須處理所有四種可能性,並且必須在情況變化時處理。

0

你必須在completionHandler的第一個類中調用一個方法。

  1. 創建類型YOURFIRSTCLASS的屬性* myfirstclass在UserAuthentication類。

  2. 將帶有「self」的firstclass傳遞給UserAuthentication對象。

  3. 建立在你的Firstclass可見方法 「 - (空)responseCaller:(NSDictionary的)字典」

  4. 呼叫你的迴應方法,該方法

YOURFIRSTCLASS .H:

-(void)responseCaller:(NSDictionary)dict; 

YOURFIRSTCLASS .m

-(void)responseCaller:(NSDictionary)dict 
{NSLog(@"%@",dict);} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UserAuthentication *auth = [[UserAuthentication alloc]init]; 
    auth.myfirstclass = self; 
    NSDictionary *dict = [auth getUserConfiguration]; 
    NSLog(@"%@",dict); 
} 

UserAuthentication .H

#import "YOURFIRSTCLASS.h" 
@property (nonatomic) *myfirstclass; 

UserAuthentication .M

-(NSDictionary *)getUserConfiguration; 
{ 
    __block NSDictionary *resultDictionary; 
    NSURLSession *session = [NSURLSession sharedSession]; 
    __weak myfirstclassSave = myfirstclass; 
    [[session dataTaskWithURL:[NSURL URLWithString:@"http://72.52.65.142:8083/auth"] 
      completionHandler:^(NSData *data, 
           NSURLResponse *response, 
           NSError *error) { 
       NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; 
       if ([response respondsToSelector:@selector(allHeaderFields)]) { 
        resultDictionary = [httpResponse allHeaderFields]; 
        [myfirstclassSave responseCaller:resultDictionary ]; 

       } 
      }] resume]; 

    return resultDictionary; 
} 

類似的東西