2
votes

There is an issue in my app that connect to firebase real time database ..

when i try to insert data to firebase during offline... when internet available the data automaticaly send to firebase database.. when my app close on offline then the insert will not happen...

i want to prevent the data insert to firebase which the after effect of insert try during offline...

//------------------------------------------------
    private void onStarClicked(final DatabaseReference postRef) {
        postRef.runTransaction(new Transaction.Handler() {
            @Override
            public Transaction.Result doTransaction(MutableData mutableData) {

                if (mutableData.hasChildren()) {
                    int inv = 0;
                    for (MutableData postData : mutableData.getChildren()) {
                        inv = postData.child("inv").getValue(Integer.class);
                    }

                    SalesModel salesModel = new SalesModel("samsung",inv+1,987.90);
                    String newKey = postRef.push().getKey();

                    // Set value and report transaction success
                    mutableData.child(newKey).setValue(salesModel);

                }
                // Set value and report transaction success
//                mutableData.setValue(p);
                return Transaction.success(mutableData);
            }

            @Override
            public void onComplete(DatabaseError databaseError, boolean b,
                                   DataSnapshot dataSnapshot) {
                // Transaction completed
//                Log.d(TAG, "postTransaction:onComplete:" + databaseError);
            }
        });
    }
    //------------------------------------------------------/
1

1 Answers

1
votes

The Realtime Database SDK cannot be configured to ignore pending writes when the app process is killed. The SDK will always try to synchronize all writes. If you don't want this behavior, you'll have to find another way to buffer writes in memory and try to only send them when online. But I suspect you will have a lot of trouble with this, as you never have a guarantee that any write will succeed at the moment you try it.