首页 > 解决方案 > Firebase Cloud Firestore 在插入时冻结应用

问题描述

我面临一个奇怪的问题。当我尝试将数据添加到我的数据库时,应用程序完全冻结。我已经调试了 Firestore 例程,它似乎卡在了add(Object)方法上。

这是我调用插入数据的代码:

public void addArmaOficial(Arma data) {
        if (data != null)
            db.collection(Constants.COLLECTION_BASE)
                    .document(Constants.COLLECTION_ARMAS)
                    .collection(Constants.COLLECTION_OFIARM)
                    .add(data)   ///< It get's stucked here
                    .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                        @Override
                        public void onSuccess(DocumentReference documentReference) {
                            Toast.makeText(ctx, R.string.accionexistosa, Toast.LENGTH_SHORT).show();
                            ctx.finish();
                        }
                    })
                    .addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
                        @Override
                        public void onComplete(@NonNull Task<DocumentReference> task) {
                            Toast.makeText(ctx, R.string.addarmaoficialtoast, Toast.LENGTH_SHORT).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            e.printStackTrace();
                        }
                    });
        else
            Toast.makeText(ctx, "Los datos son nulos", Toast.LENGTH_SHORT).show();
    }

我不明白为什么会这样。有一些“Arma”类的摘录:

public class Arma extends Equipo implements Parcelable {
    private String dano;
    private String tipo_dano;
    private ArrayList<Prop_Arma> propiedades;
    private String categoria_princ;
    private String categoria_secun;
    private String notas;
    private float distancaimed, distanciamax;
    private int mods;
///< Constructor, getters and setters, and parcelable implementation done automatically
}

还有“Prop_Arma”类:

public class Prop_Arma implements Parcelable {
    private String nombre;
    private boolean dano_var;
    private String descripcion;
    private boolean distancia;
    private boolean grande, muygrande, gigante, gargantuesca;
///< The same as previous, constructors, getters and setters, and the Parcelable implementation done automatically
}

问题是它被卡住并且说没有错误。就像是在运行一些东西,但我不知道是什么。另外,我已经在我的代码中完成了时间戳更新,所以我认为这不是问题所在。

更新 1

这是它被称为方法的代码addArmaOficial(Arma data)

public void Guardar(boolean flag) {
        if (arma_nueva.getPropiedades() == null && !flag) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(R.string.nopropmessage)
                    .setTitle(R.string.nopropmesstit);

            builder.setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(CrearArma.this, "Operación cancelada...", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .setPositiveButton(R.string.continuar, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Guardar(true);
                        }
                    });

            AlertDialog dialog = builder.create();
            dialog.show();
        } else {
            arma_nueva.setNombre(((EditText) findViewById(R.id.nombre)).getText().toString());
            if(!Objects.equals(((EditText) findViewById(R.id.peso)).getText().toString(), ""))
                arma_nueva.setPeso(Float.parseFloat(((EditText) findViewById(R.id.peso)).getText().toString()));
            if(!Objects.equals(((EditText) findViewById(R.id.costePC)).getText().toString(), ""))
                arma_nueva.setPc(Integer.parseInt(((EditText) findViewById(R.id.costePC)).getText().toString()));
            if(!Objects.equals(((EditText) findViewById(R.id.costePP)).getText().toString(), ""))
                arma_nueva.setPp(Integer.parseInt(((EditText) findViewById(R.id.costePP)).getText().toString()));
            if(!Objects.equals(((EditText) findViewById(R.id.costePO)).getText().toString(), ""))
                arma_nueva.setPo(Integer.parseInt(((EditText) findViewById(R.id.costePO)).getText().toString()));
            arma_nueva.setDano(((Spinner)findViewById(R.id.dano)).getSelectedItem().toString());
            arma_nueva.setTipo_dano(tip_dano_spin.getSelectedItem().toString());
            arma_nueva.setCategoria_princ(cats_spin.getSelectedItem().toString());
            arma_nueva.setCategoria_secun(tips_spin.getSelectedItem().toString());
            if(!Objects.equals(((EditText) findViewById(R.id.distmed)).getText().toString(), ""))
                arma_nueva.setDistancaimed(Float.parseFloat(((EditText)findViewById(R.id.distmed)).getText().toString()));
            if(!Objects.equals(((EditText) findViewById(R.id.distmax)).getText().toString(), ""))
                arma_nueva.setDistanciamax(Float.parseFloat(((EditText)findViewById(R.id.distmax)).getText().toString()));
            if(!Objects.equals(((EditText) findViewById(R.id.mods)).getText().toString(), ""))
                arma_nueva.setMods(Integer.parseInt(((EditText)findViewById(R.id.mods)).getText().toString()));
            DBConnector db = new DBConnector(this);
            db.addArmaOficial(arma_nueva);
        }
    }

这是在声明为的类中:

public class CrearArma extends AppCompatActivity {
///...
}

标签: androidfirebaseinsertgoogle-cloud-firestore

解决方案


好吧,我终于找到了问题所在!

它不是 firebase,也不是它的发送方式,它只是一种找不到的资源。只是我用非字符串文件字符串替换了一些字符串,它可以工作。


推荐阅读