0
\$\begingroup\$

Multiple AFNetworking calls In a view controller. The code is very wield.

I think , to negotiate with the server , and combine those into one url, instead of many urls.

@interface HomeViewController () 

#pragma mark - init user info 
- (void) initUserData
{
    [self get_userInfo];// user login info about
    [self getRule];// get ship rule 
    [self getAppSystem];
    [self getPunchCardInfo]; // get work register stauts
    [CardTypePickerView updateCardTypes:nil]; //  get id type
    [self getProblemTypes];
    [self getFetch_ad];/ get ad 
    [self updateSMSCount];// update small messages 
    [CGAreaData updateAddressList:NO failure:^(NSString *failure) {}];// update province , city , district
}


#pragma mark -- user login info about
- (void)get_userInfo
{
    __block UserInfoModel *userInfo = [UserInfoModel getUserInfoModel];
    [NetWorkManager info_userSuccess:^(id result, NSString *message) {
        userInfo = [UserInfoModel getUserInfoModel];
        if ([UiUtil checkIsNull:result] == NO) {
            [userInfo setValuesForKeysWithDictionary:result];
            [userInfo saveUserInfoModel];
            [self.headerTaskView setName:userInfo.realName];
        }
    } failure:nil];
    [self.headerTaskView setName:userInfo.realName];
}



#pragma mark --  get ship rule 
- (void)getRule
{
    [NetWorkManager getBillRuleWithSuccess:^(NSDictionary *dict) {
        if (![UiUtil checkIsNull:dict]) {
            [[EGOCache globalCache] setObject:dict forKey:EGO_BAR_RULE];
        }

    } failure:^(NSString *errmsg) {
    }];
}

#pragma mark -- get app version
- (void)getAppSystem
{
    [NetWorkManager app_systemWithSuccess:^(NSDictionary *result, NSString *message) {
        if (![UiUtil checkIsNull:result]) {
            [[EGOCache globalCache] setObject:result forKey:EGO_APP_SYSTEM_RULE];
            // check if new version exists
            [VersionCheck versionCheckComplete:result[@"VERSION_IOS"]];

        }

    } failure:^(NSString *errmsg) {

       // check if new version exists
    }];
}

......
// network request info etc

Under the hood, those functions all use AFNetworking

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];

Any suggestion to optimize these batch requests?

What is the harm of code like this? I think it is the battery consuming.

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

From my point of view, there's nothing wrong with doing the network requests like you are doing it IF you are using MVC. You are just asking the model for the target request and using blocks to manage the data returned. Maybe I would only put in the view controller the requests that after being executed, need to update some UI (not the case for your getRule and getAppSystem methods), and put the other ones in an model layer that manages basic data for the app.

Talking about battery consumption, I think there is no problem at all with your requests if those requests are justified and you need to call them anyway. Of course, you should avoid as much as possible to call them continuously because the battery drain.

Maybe I would group some of them in one single request.

If you find a problem with AFNetworking to perform batch requests for the lack of a completion handler, you can use dispatch groups:

// create the group
dispatch_group_t group = dispatch_group_create();
// before calling each request
dispatch_group_enter(group);
// on the completion of each request
dispatch_group_leave(group);
// to use it as a completion handler
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    NSLog(@"all requests finished!");
});
\$\endgroup\$
1
\$\begingroup\$

There is nothing wrong with multiple network requests, it will not have any impact on battery that you need to worry about.

Your code however needs a lot of work. None of these network requests should be happening inside a ViewController. It's purpose is to configure the View and handle user actions, nothing else.

\$\endgroup\$
1
  • \$\begingroup\$ battery , Please check WWDC 2017 writing energy efficient apps \$\endgroup\$
    – dengApro
    Commented May 29, 2018 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.