2013-04-22 57 views
0

奇怪的標題,我知道,但這裏是解釋我的問題的最快方法。 Parse服務有一個預先建立的註冊控制器,讓新用戶註冊您可能擁有的任何服務。沒有辦法編輯它的實現,你只能在它的委託方法中處理事件。所以我不能把它放在「註冊」按鈕的IBAction中。我想要做的是,當用戶觸摸「註冊」時,我需要調用某個API來檢查是否存在某個事件,如果它已經存在,則不要讓用戶簽名向上。這裏是指處理驗證時,按下按鈕時的委託方法:是否可以在返回BOOL的委託方法中執行API調用?

// Sent to the delegate to determine whether the sign up request should be submitted to the server. 
- (BOOL)signUpViewController:(PFSignUpViewController *)signUpController shouldBeginSignUp:(NSDictionary *)info { 

這裏就是我想在它的地方:

[self processJSONDataWithURLString:[NSString stringWithFormat:@"https://www.someapi.com/api/profile.json?username=%@",username] andBlock:^(NSData *jsonData) { 

    NSDictionary *attributes = [jsonData objectFromJSONData]; 

    // Check to see if username has data key, if so, that means it already exists 
    if ([attributes objectForKey:@"Data"]) { 
     return NO; // Do not continue, username already exists 
    // I've also tried: 
     dispatch_sync(dispatch_get_main_queue(), ^{ return NO; }); 
    } 
else 
     return YES; //Continue with sign up 
     dispatch_sync(dispatch_get_main_queue(), ^{ return YES; }); 
}]; 

我得到的錯誤,當我嘗試返回任何東西,但是。當我直接返回YES時,「^(NSData * jsonData)」以黃色下劃線,並且我得到「不兼容塊指針類型向BOOL(^)NSData * _ 強發送void參數類型(^)NSData * _強大」。

基本上,有什麼辦法可以在這個方法中進行API調用來檢查某些內容,然後根據結果返回YES或NO?

謝謝!

+0

爲什麼你需要dispatch_sync(dispatch_get_main_queue()^兩次??? – satheeshwaran 2013-04-22 18:16:43

回答

2

要調用正在使用該塊作爲回調異步方法。 processJSON…方法被調用並立即從調用中返回。在後臺運行後,塊將被調用。你不能從塊內「返回」。該方法從堆棧彈出並返回一段時間。

您需要重新設計此邏輯。在主隊列上調用刷新是正確的。

+0

+1,但是另一種看待它的方法是:*是的,你可以進行所有你想要的API調用,只要它們是同步的。你不能做的是在等待異步操作回覆給你的同時保持同步呼叫。* – Caleb 2013-04-22 18:43:43

+0

非常感謝,非常感謝。 – JMarsh 2013-04-22 18:51:28

1

試試這個:

[self processJSONDataWithURLString:[NSString stringWithFormat:@"https://www.someapi.com/api/profile.json?username=%@",username] andBlock:^(NSData *jsonData) { 

    NSDictionary *attributes = [jsonData objectFromJSONData]; 
    BOOL status=YES; 
    // Check to see if username has data key, if so, that means it already exists 
    if ([attributes objectForKey:@"Data"]) { 
     status=NO; // Do not continue, username already exists 

    [self performSelectorOnMainThread:@selector(callDelegate:) withObject:[NSNumber numberWithBool:status] waitUntilDone:YES]; 
}]; 

-(void)callDelegate:(NSNumber*) status 
{ 
    BOOL returnStatus = [status boolValue]; 

    //now retutn returnStatus to your delegate. 
} 

但是,這不是做正確的方式,你必須改變你寫來支持異步通信的邏輯。你可以考慮我的,只有當你想按你的方式去做。

相關問題