2013-03-06 35 views
3

您好,感謝您的幫助。我一直在尋找,但無法找到答案。我一直在爲iOS編程一週。Objective-c + RestKit - 等待下一步前的響應

到目前爲止,所有到Web服務的連接都在運行,並且我創建了一個類和方法來完成這些調用。我正在進行標準登錄,用戶輸入登錄信息,應用程序將這些值傳遞給Web服務,並且它返回一個布爾值,具體取決於信息是否匹配數據庫上的任何內容。之後,應用程序獲取返回值,並移至下一個屏幕或顯示錯誤警報。或者至少這是我的目標。

問題是,在進行其他調用或解析響應之前正在執行條件,並且找不到解決方法。我讀過關於異步和同步調用的內容,但沒有太多的運氣來實現它們。

這是調用代碼:

//Class that has the methods for calling the web services 
__restObj = [[restCalls alloc] init]; 
bool login = [__restObj restLogin:user passwd:pass]; 
if (login) { 
    [self performSegueWithIdentifier:@"adminLogin" sender:self]; 
} 
else{ 
    UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Incorrect group name or password." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
    [alert show]; 
} 

在實際POST發生之前執行的條件,並在那裏永遠是假的。

這是我的方法:提前

- (bool)restLogin:(NSString*)user passwd:(NSString*)pass{ 
// Load the object model via RestKit 
RKObjectManager *objectManager = [RKObjectManager sharedManager]; 

groupInfo *gi = [[groupInfo alloc] init]; 

gi.gName = user; 
gi.pass = pass; 


RKObjectMapping *userInfoMapping = [RKObjectMapping requestMapping]; 
[userInfoMapping addAttributeMappingsFromDictionary:@{@"gName": @"groupName",@"pass":@"pass"}]; 

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userInfoMapping 
                       objectClass:[groupInfo class] 
                       rootKeyPath:nil]; 
[objectManager addRequestDescriptor:requestDescriptor]; 
objectManager.requestSerializationMIMEType = RKMIMETypeJSON; 

[objectManager postObject:gi 
        path:@"adminLoginIos" 
       parameters:nil 
        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
         NSArray* statuses = [mappingResult array]; 
         NSLog(@"Loaded statuses: %@", statuses); 
         _result = [statuses objectAtIndex:0]; 
        } 
        failure:^(RKObjectRequestOperation *operation, NSError *error) { 
         NSLog(@"Hit error: %@", error); 
        } 
]; 

return _result; 
} 

感謝,並記住,我在這個真正的新,所以我感謝你的幫助,如果我的代碼是不是最好的,請告訴我的。

問候, ChmlGr

回答

2

你必須在一個塊傳遞,然後成功回調中,塊返回_result。

一個例子根據您的結構是這樣的:

-(void) restLogin:(NSString*)user passwd:(NSString*)pass block:(void (^)(id))block { 
    // Load the object model via RestKit 
    RKObjectManager *objectManager = [RKObjectManager sharedManager]; 

    groupInfo *gi = [[groupInfo alloc] init]; 

    gi.gName = user; 
    gi.pass = pass; 


    RKObjectMapping *userInfoMapping = [RKObjectMapping requestMapping]; 
    [userInfoMapping addAttributeMappingsFromDictionary:@{@"gName": @"groupName",@"pass":@"pass"}]; 

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userInfoMapping 
                        objectClass:[groupInfo class] 
                        rootKeyPath:nil]; 
    [objectManager addRequestDescriptor:requestDescriptor]; 
    objectManager.requestSerializationMIMEType = RKMIMETypeJSON; 

    [objectManager postObject:gi 
         path:@"adminLoginIos" 
        parameters:nil 
         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
          NSArray* statuses = [mappingResult array]; 
          NSLog(@"Loaded statuses: %@", statuses); 
          _result = [statuses objectAtIndex:0]; 
          block(_result); 
         } 
         failure:^(RKObjectRequestOperation *operation, NSError *error) { 
          NSLog(@"Hit error: %@", error); 
          block(nil); 
         } 
    ]; 

} 

你到方法的調用會是這樣的:

[__restObj restLogin:user passwd:pass block:^(id obj) { 
    // do something here to translate that object into a BOOL and check value  
}]; 

我不使用RestKit,這樣我就可以」 t驗證這正是你需要的,但它應該讓你走上正確的道路。也就是說,如果你想查看AFNetworking,我寫了一個我不介意分享的NetworkClient包裝器。

+0

非常感謝你,這絕對是訣竅! =) – user2119777 2013-03-06 17:54:56