按顺序执行代码时的奇怪行为

时间:2017-12-04 08:07:12

标签: android retrofit2

checkUpdate(last_update, SplashActivity.this);
Intent l = new Intent(SplashActivity.this, MainActivity.class);
startActivity(l);

在上面的代码中,系统在checkUpdate的所有主体完全执行之前执行startActivity,这对我来说是个大问题

checkUpdate中的代码如下

private void checkUpdate(String last_update, final Context context) {

    mydb = new DatabaseHelper(context);

    Cursor res = mydb.get_options();
    res.moveToFirst();
    if (((res != null) && (res.getCount() > 0))) {
        String dbLastUpdate = res.getString(0);
        if (!last_update.equals(dbLastUpdate)) {
            // Toast.makeText(context, "Database will be updated ", Toast.LENGTH_SHORT).show();
            db_Request(context);
        } else {
            Intent l = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(l);
        }
    } else {
        //Toast.makeText(context, "Database", Toast.LENGTH_SHORT).show();
        db_Request(context);

    }

    return;
}

private void db_Request(final Context context) {

    dialog = CustomProgressDialog.showProgressDialog(context, "Loading...");
    mydb = new DatabaseHelper(context);
    apiInterface = ApiClient.getClient().create(ApiInterface.class);
    Call<DatabaseModel> call = apiInterface.providers();
    call.enqueue(new Callback<DatabaseModel>() {
        @Override
        public void onResponse(Call<DatabaseModel> call, Response<DatabaseModel> response) {


            DatabaseModel result = response.body();
            if (result != null) {

                Boolean status = response.body().isStatus();
                String msg = response.body().getMsg();
                if (status.equals(true)) {

                    mydb.delete();
                    DatabaseModel.Data data = response.body().getData();
                    List<DatabaseModel.Service_parameters> params = data.getService_parameters();
                    for (int i = 0; i < params.size(); i++) {
                        int id = params.get(i).getId();
                        String service_type = params.get(i).getService_type();
                        int external_system_id = params.get(i).getExternal_system_id();
                        int payment_service_id = params.get(i).getPayment_service_id();
                        String name_ar = params.get(i).getName_ar();
                        String name_en = params.get(i).getName_en();
                        int position = params.get(i).getPosition();
                        String visible = params.get(i).getVisible();
                        String required = params.get(i).getRequired();
                        String type = params.get(i).getType();
                        String is_client_id = params.get(i).getIs_client_id();
                        String default_value = params.get(i).getDefault_value();
                        int min_length = params.get(i).getMin_length();
                        int max_length = params.get(i).getMax_length();

                        mydb.insert_service_parameters(context, id, service_type, external_system_id, payment_service_id, name_ar, name_en,
                                position, visible, required, type, is_client_id, default_value, min_length, max_length);

                    }



                    List<DatabaseModel.Service_provider_categories> cat = data.getService_provider_categories();
                    for (int i = 0; i < cat.size(); i++) {
                        int id = cat.get(i).getId();
                        String name_ar = cat.get(i).getName_ar();
                        String name_en = cat.get(i).getName_en();
                        String description_ar = cat.get(i).getDescription_ar();
                        String description_en = cat.get(i).getDescription_en();
                        String icon = cat.get(i).getIcon();

                        mydb.insert_service_provider_categories(context, id, name_ar, name_en, description_ar, description_en, icon);


                    }


                    List<DatabaseModel.Service_providers> prov = data.getService_providers();
                    for (int i = 0; i < prov.size(); i++) {
                        int id = prov.get(i).getId();
                        int service_provider_category_id = prov.get(i).getService_provider_category_id();
                        String name_ar = prov.get(i).getName_ar();
                        String name_en = prov.get(i).getName_en();
                        String description_ar = prov.get(i).getDescription_ar();
                        String description_en = prov.get(i).getDescription_en();
                        String logo = prov.get(i).getLogo();

                        mydb.insert_service_providers(context, id, service_provider_category_id, name_ar, name_en, description_ar,
                                description_en, logo);

                    }


                    List<DatabaseModel.Services> serv = data.getServices();
                    for (int i = 0; i < serv.size(); i++) {
                        int id = serv.get(i).getId();
                        int service_provider_id = serv.get(i).getService_provider_id();
                        String name_ar = serv.get(i).getName_ar();
                        String name_en = serv.get(i).getName_en();
                        String description_ar = serv.get(i).getDescription_ar();
                        String description_en = serv.get(i).getDescription_en();
                        String icon = serv.get(i).getIcon();

                        mydb.insert_services(context, id, service_provider_id, name_ar, name_en, description_ar, description_en,
                                icon);

                    }


                    List<DatabaseModel.Options> opts = data.getOptions();

                    String name = opts.get(0).getName();
                    String value = opts.get(0).getValue();

                    mydb.insert_options(context, name, value);

                    Intent l = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(l);

                    dialog.dismiss();
                }

            } else {

                 /*   CustomAlertDialog.getInstance().showInfoDialog(context.getString(R.string.error), context.getString(R.string.errorConnectingServer),
                        context.getString(R.string.close),
                        context, 1);*/
            }

        }

        @Override
        public void onFailure(Call<DatabaseModel> call, Throwable t) {
      /*      CustomAlertDialog.getInstance().showInfoDialog(context.getString(R.string.error), context.getString(R.string.errorConnectingServer),
                    context.getString(R.string.close),
                    context, 1);*/
            dialog.dismiss();
        }
    });

}

N.B 我无法删除startActivity并将其放在checkUpdate正文

所以,我想了解为什么会发生这种情况以及如何防止它

0 个答案:

没有答案