Dawn17
Dawn17

Reputation: 8297

Is it possible to set a timer in a Firebase database?

I am creating a mobile app that displays a score of an ongoing soccer game (amateur game, not professional). So, what I am basically doing is, I am creating a data of a soccer game in the Firebase real-time DB which might contain information like:

Team A: String
Team B: String
Score A: int
Score B: int
isLive: true/false
id: int
currentTime: ???

So, when I press the Score Update in my app, it will update the score, and will apply this change to a ListView in my app.

Everything seems clear up to this point, but I am having a trouble of displaying the current game time.

I wanted to work something like:

enter image description here

This is from the web app (http://livescore.com) that I was motivated by. When a game begins, the game time is displayed from 1min to 90min as shown in the image and turns in to FT when it is over.

My question is,

is it possible to make like an automatically incrementing timer in Firebase database, or is it possible to make it work like it is incrementing 1 to its value every 1 minute up to a certain point?

Any kind of advice would be appreciated.

Upvotes: 1

Views: 1916

Answers (3)

Never World
Never World

Reputation: 1

Most likely, you need another client that will always work and update the global time in FireBase, and based on the global time, you can make your calculations

Upvotes: 0

BHargav D Patel
BHargav D Patel

Reputation: 1

//fetch test time in minutes
    //and convert that into milli seconds
    mTimerRef = FirebaseDatabase.getInstance().getReference("eTest").child(key).child("TestMinutes");
    mTimerRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            long millisecs = (Integer.parseInt(dataSnapshot.getValue(String.class))) * 6000;
            mTimeLeftInMillis = millisecs;
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            throw databaseError.toException();
        }
    });

add minutes in firebase and read that using this function and convert into long, milli seconds. hope this help.

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 138899

There is no way to achieve what you want using Firebase Realtime Database because it doesn't provide something like this. To solve this, you need to create a custom scheduling mechanism made by yourself that provides you that behaviour.

So unfortunately, in this case you do not get rid of coding.

Upvotes: 2

Related Questions