FireBase Storage MetaData-Updated Time returning 0?

后端 未结 1 1244
一生所求
一生所求 2021-01-28 23:44

I am attempting to use Firebase to store and sync several CSV files. Copies of these CSV files are stored in internal storage.

To update them locally, I am looping thro

1条回答
  •  不思量自难忘°
    2021-01-29 00:38

    Retrieving the metadata is an asynchronous operation, which happens in the background, while your main code continues. It's easiest to see what that means by adding a few simple log statements:

    reference.listAll().addOnSuccessListener(new OnSuccessListener() {
        @Override
        public void onSuccess(ListResult listResult) {
            for(final StorageReference item : listResult.getItems()) {
                Log.i(TAG, "Before calling getMetadata()");
    
                item.getMetadata().addOnSuccessListener(new OnSuccessListener() {
                    @Override
                    public void onSuccess(StorageMetadata storageMetadata) {
                        Log.i(TAG, "Got metadata");
                    }
                });
                Log.i(TAG, "After calling getMetadata()");
            }
        }
    })
    

    When run, this code prints:

    Before calling getMetadata()

    After calling getMetadata()

    Got metadata

    This is probably not what you expected, but it is working as designed, and it explains exactly why your code doesn't work: by the time your if (localFile.lastModified() > onlineFileChangeDate[0]) runs, the onlineFileChangeDate[0] = storageMetadata.getUpdatedTimeMillis() hasn't set it to a value yet.


    The solution for his (and any other problems with asynchronous method calls) is that the code that needs the metadata needs to be inside the onSuccess that gets called with that metadata, or be called from there.

    The simplest fix:

    reference.listAll().addOnSuccessListener(new OnSuccessListener() {
        @Override
        public void onSuccess(ListResult listResult) {
            for(final StorageReference item : listResult.getItems()) {
                final File localFile = new File(rootpath, item.getName());
                item.getMetadata().addOnSuccessListener(new OnSuccessListener() {
                    @Override
                    public void onSuccess(StorageMetadata storageMetadata) {
                        if (localFile.lastModified() > storageMetadata.getUpdatedTimeMillis()) {
                            Log.i(TAG, "File deleted " + localFile.delete());
                            item.getFile(localFile).addOnSuccessListener(new OnSuccessListener() {
                                @Override
                                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                                    Toast.makeText(settings.this, "Downloaded: " + localFile, Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
    
                    }
                });
    
            }
            Toast.makeText(settings.this, "Update Complete", Toast.LENGTH_SHORT).show();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e(TAG,"Firebase Update Error");
        }
    });
    

    Also see:

    • getContactsFromFirebase() method return an empty list

    0 讨论(0)
提交回复
热议问题