Firestore Retrieve Timestamp Issue

Viewed 32

I keep getting a null returned when I retrieve a timestamp in Firestore:

I have a collection appData with a document appDataRefreshTimestamp and field lastRefreshTimestamp (the app looks at this and compares to stored value in SharedPreferences in order to force refresh on specific AppData) as below:

enter image description here

I am trying to retrieve this here:

    fun listenForAppDataRefreshTimestamp() : LiveData<Boolean> {
        val result = MutableLiveData<Boolean>()
        result.value = false
        val lastTimeDataCached = Utils.getTimestampFromSharedPreferences(PREFERENCE_TIMESTAMP_APP_DATA_REFRESH)
        Log.d(TAG, "listenForAppDataRefreshTimestamp() - last saved time stamp: $lastTimeDataCached")
        appDataTimestamp
            .addSnapshotListener{ docSnapshot, e ->
                if (e != null) {
                    Log.w(TAG, "Listen failed.", e)
                    return@addSnapshotListener
                }
                if (docSnapshot != null) {
                    val newTimestamp = docSnapshot.getTimestamp("lastRefreshTimestamp")?.toDate()
                    if (lastTimeDataCached != null) {
                        if (lastTimeDataCached < newTimestamp){  //newTimestamp is null!!
                            Log.d(TAG, "listenForAppDataRefreshTimestamp() - New Timestamp found ($newTimestamp)")
                            newTimestamp?.let { Utils.saveTimestamp(APP_DATA_DATA_REFRESH_TIMESTAMP,it) }
                            result.postValue(true)
                        }
                    }
                    Log.d(TAG, "listenForAppDataRefreshTimestamp() - Saved date is..................  ${Utils.getTimestampFromSharedPreferences(PREFERENCE_TIMESTAMP_APP_DATA_REFRESH)}")
                }
            }
        return result
    }

But as shown above, it is null and I can't see why..

Edit: I have now added the SharedPreferences element - it seems it is this that may be causing the issue (not saving the timestamp for some reason).

I have the following two functions in my Utils class:

    fun saveTimestamp(timestampKey: String, timestamp: Date) {
        val sharedPref = getSharedPreferences()
        val editor = sharedPref.edit()
        editor.putLong(timestampKey, timestamp.time)
        editor.apply()
    }

And

    fun getTimestampFromSharedPreferences(timestampKey: String): Date? {
        val currentTimestamp = System.currentTimeMillis()
        val sharedPref = getSharedPreferences()
        val timestamp = sharedPref.getLong(timestampKey, currentTimestamp)
        Log.d(TAG, "getTimestampFromSharedPreferences($timestampKey) = $currentTimestamp")

        return if (timestamp.equals(0)) Date(currentTimestamp)
        else Date(timestamp)
    }

But in listenForAppDataRefreshTimestamp() it shows the timestamp didn't get updated in SharedPreferences when it is checked here:

Log.d(TAG, "listenForAppDataRefreshTimestamp() - Saved date is..................  ${Utils.getTimestampFromSharedPreferences(PREFERENCE_TIMESTAMP_APP_DATA_REFRESH)}") // Still the old timestamp..
0 Answers
Related