0

I have the following code. What it does is performs a series of synchronizations (using AFNetworking) by calling another class/method - POSRestletManager.

Before each method, starting at getSettingsWithEnv, I am displaying a message (using SVProgressHud) to show the user where things are at. This seems to get displayed immediately for all the method calls (because they are async) but only after the method call is completed. ie. in the completion block. How can I display the message before the call ?. is it even possible ?.

The reason I ask is that the last call (getItemsWithLocation) is executed synchronously and therefore the message "Sync Items..." does not display until all the items are sync'd... which is obviously not what I want.

can anyone help me better structure this?

Here is my code.

- (IBAction)updateSettings:(id)sender {
 isUpdating = YES;

[POSRestletsManager getInstance].delegate = self;
[POSRestletsManager setHeaderWithUsername:[POSUserDefaults getRegisteringEmail] Password:[POSUserDefaults getRegisteringPassword] accountNumber:[POSUserDefaults getRegisteringAccount] andRoleNumber:[POSUserDefaults getRegisteringRole]];

[POSRestletsManager getDataCenterURL:[POSUserDefaults getEnvironmentValue] completion:^{

    [SVProgressHUD showWithStatus:@"Sync Settings..." maskType:SVProgressHUDMaskTypeGradient];
    [self.navigationController popToRootViewControllerAnimated:YES];

    [POSRestletsManager getSettingsWithEnv:[POSUserDefaults getEnvironmentValue] locationId:((NSString *)[POSUserDefaults getRegisteringTerminal]).integerValue andUpdateType:@"Full" completion:^{

        [SVProgressHUD dismiss];
        [SVProgressHUD showWithStatus:@"Sync Customers..." maskType:SVProgressHUDMaskTypeGradient];

        [POSRestletsManager getCustomersListWithUpdateType:[POSUserDefaults getEnvironmentValue] andUpdateType:_customerSync completion:^{

            [SVProgressHUD dismiss];
            [SVProgressHUD showWithStatus:@"Sync Items..." maskType:SVProgressHUDMaskTypeGradient];


            [POSRestletsManager getItemsWithLocation:[[POSCoreDataManager getInstance] getLocationId] andUpdateType:_itemSync andEnvironment:[POSUserDefaults getEnvironmentValue]];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"POSSetCustomerSettings" object:nil] ;

        }];
    }];

}];

}
ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
RayT
  • 3
  • 2
  • Networking should always be done asynchronously, off the main thread. See [this Apple Technical Q&A for more information](https://developer.apple.com/library/ios/qa/qa1693/_index.html). – mattt Oct 14 '14 at 13:26
  • In fact, blocking the main thread may prevent alerts from being displayed when expected. – mattt Oct 14 '14 at 13:27

0 Answers0