如何在Android上

时间:2017-10-22 06:21:28

标签: android firebase google-cloud-firestore

当我浏览适用于Android的Firebase存储和数据库教程时,我遇到了一个问题,无法确定查询的优先顺序。

我需要将访问者数据保存到Firestore Database,并通过单击按钮将Firebase Storage中的访问者图像保存到public void saveNewVisitor() { Uri file = Uri.fromFile(new File(image_path)); StorageReference imageRef = storageRef.child("images/"+file.getLastPathSegment()); UploadTask uploadTask = imageRef.putFile(file); uploadTask.addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle unsuccessful uploads } }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { downloadURL = taskSnapshot.getDownloadUrl(); } }); //----------------------------------- Map<String, Object> dataToSave = new HashMap<String, Object>(); dataToSave.put(NAME, visitorName); dataToSave.put(AGE, visitorAge); dataToSave.put(GENDER, visitorGender); dataToSave.put(IMAGE_URL, String.valueOf(downloadURL)); CollectionReference mColRef = FirebaseFirestore.getInstance().collection("visitors"); mColRef.add(dataToSave).addOnSuccessListener(new OnSuccessListener<DocumentReference>() { @Override public void onSuccess(DocumentReference documentReference) { Log.d(TAG, "Visitor has been saved!"); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { Log.w(TAG, "Error adding a visitor", e); } }); } 。所以,我在Android中有以下java方法:

OnSuccessListener

在上面的代码中,有两个动作正在发生。在第一部分中,图片正在Firebase存储中保存,URL正在生成URL = null以下载该图片以供将来使用。在第二部分中,文本数据(包括上面生成的URL)将保存在数据库中。

所以,问题是,Android和Firebase首先执行文本数据查询,然后执行图像保存查询。这导致数据库中的downloadURL = null与保存数据DROP USER SYSMAN CASCADE; DROP USER SYSMAN_OPSS CASCADE; DROP USER SYSMAN_MDS CASCADE; DROP USER SYSMAN_APM CASCADE; DROP USER SYSMAN_RO CASCADE; DECLARE CURSOR l_syn_csr IS SELECT 'DROP ' || CASE owner WHEN 'PUBLIC' THEN 'PUBLIC SYNONYM ' ELSE 'SYNONYM ' || owner || '.' END || synonym_name AS cmd FROM dba_synonyms WHERE table_owner IN ( 'SYSMAN', 'SYSMAN_MDS', 'MGMT_VIEW', 'SYSMAN_BIP', 'SYSMAN_APM', 'BIP', 'SYSMAN_OPSS', 'SYSMAN_RO' ); BEGIN FOR l_syn_rec IN l_syn_csr LOOP BEGIN EXECUTE IMMEDIATE l_syn_rec.cmd; EXCEPTION WHEN OTHERS THEN dbms_output.put_line( '===> ' || l_syn_rec.cmd ); dbms_output.put_line( sqlerrm ); END; END LOOP; END; / DROP USER mgmt_view CASCADE; DROP TABLESPACE mgmt_ecm_depot_ts INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS; DROP TABLESPACE mgmt_tablespace INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS; DROP TABLESPACE mgmt_ad4j_ts INCLUDING CONTENTS AND DATAFILES CASCADE CONSTRAINTS; DELETE FROM schema_version_registry WHERE (comp_name,owner) IN ( ('Authorization Policy Manager','SYSMAN_APM'), ('Metadata Services','SYSMAN_MDS'), ('Oracle Platform Security Services','SYSMAN_OPSS') ); commit; 时相同。

我的问题是如何将图像保存查询优先级设置为HIGH,以确保在文本数据保存查询之前执行它。 Firebase文档在此主题上并不清楚。任何有助于解决问题的帮助或建议都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

使用UploadTask将文件上传到Firebase asynchronously,因此您的上传任务仍在运行,但在您调用mColRef.add(dataToSave)之前尚未完成,onSuccess()也是异步执行的。

为了链接这两种异步方法,您需要将方法逻辑的后半部分移到UploadTask的{​​{1}}块内。这样,只有在上传任务完成后才能执行数据库操作,并且您可以保证能够访问下载URL。

有关完整示例,您可以将方法拆分为两部分,以便更容易看到发生了什么:

public void saveNewVisitor() {
    Uri file = Uri.fromFile(new File(image_path));
    StorageReference imageRef = storageRef.child("images/"+file.getLastPathSegment());
    UploadTask uploadTask = imageRef.putFile(file);

    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            downloadURL = taskSnapshot.getDownloadUrl();
            saveNewVisitorToDatabase(String.valueOf(downloadURL));
        }
    });
}

private void saveNewVisitorToDatabase(String downloadURL) {
    Map<String, Object> dataToSave = new HashMap<String, Object>();
    dataToSave.put(NAME, visitorName);
    dataToSave.put(AGE, visitorAge);
    dataToSave.put(GENDER, visitorGender);
    dataToSave.put(IMAGE_URL, downloadURL);

    CollectionReference mColRef = FirebaseFirestore.getInstance().collection("visitors");
    mColRef.add(dataToSave).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
        @Override
        public void onSuccess(DocumentReference documentReference) {
            Log.d(TAG, "Visitor has been saved!");
            // Once we reach here, we can guarantee that the upload task
            // and this database operation were both successful
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.w(TAG, "Error adding a visitor", e);
        }
    });
}