My Issue is that when A stores a java object using the setValue function, it will be reflected in the firebase console with no duplications, but unfortunatly onChildAdded will be executed multiple times according to the number of duplications it returns.
Important note: when my application is fired and I have added no value at this time, onChildAdded will return the exact & non-duplicated values that have been stored in the firebase console. But as mentioned above, the issue happens when I store a new data object, where it will be stored in the firebase console without duplication, but the onChildAdded will create multiple duplications for this new stored object!
The code of the Data Model class that has been stored in the Firebase's console:
package drawingModelData;
import java.util.ArrayList;
public class Pen {
private ArrayList<Float> moveToCoordinates;
private ArrayList <Float> lineToCoordinates;
private ArrayList<Float> quadToCoordinates;
private int strokeColor;
private int strokeWidth;
public Pen (){
moveToCoordinates = new ArrayList<>(2);
lineToCoordinates = new ArrayList<>(2);
quadToCoordinates = new ArrayList<Float>();
}
public void setMoveToCoordinates(float x, float y) {
moveToCoordinates.add(0f);
moveToCoordinates.add(0f);
moveToCoordinates.set(0, x);
moveToCoordinates.set(1, y);
}
public void setLineToCoordinates(float x, float y) {
lineToCoordinates.add(0f);
lineToCoordinates.add(0f);
lineToCoordinates.set(0, x);
lineToCoordinates.set(1, y);
}
public void setQuadToCoordinates(float x1, float y1, float x2, float y2) {
quadToCoordinates.add(x1);
quadToCoordinates.add(y1);
quadToCoordinates.add(x2);
quadToCoordinates.add(y2);
}
public void setStrokeColor(int strokeColor) {
this.strokeColor = strokeColor;
}
public void setStrokeWidth(int strokeWidth) {
this.strokeWidth = strokeWidth;
}
public int getStrokeColor() {
return strokeColor;
}
public int getStrokeWidth() {
return strokeWidth;
}
public ArrayList<Float> getMoveToCoordinates() {
return moveToCoordinates;
}
public ArrayList<Float> getLineToCoordinates() {
return lineToCoordinates;
}
public ArrayList<Float> getQuadToCoordinates() {
return quadToCoordinates;
}
}
The Firebases's Codes that been used to store and return the data:
board0DBRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
System.out.println("Key: " + snapshot.getKey() + ", Value " + snapshot.getValue());
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
String boardItemForDB = board0DBRef.push().getKey();
Map<String, Object> DATA = new HashMap<String, Object>();
DATA.put( "a", pen.getQuadToCoordinates());
DATA.put("b" ,pen.getLineToCoordinates());
DATA.put("c", pen.getMoveToCoordinates());
board0DBRef.child(boardItemForDB).setValue(DATA);
With:
FirebaseDatabase firebaseDatabase;
DatabaseReference board0DBRef;
firebaseDatabase = FirebaseDatabase.getInstance();
board0DBRef = firebaseDatabase.getReference().child("Board0");
And this is the JSON file exported from the real-time database console
{
"Board0": {
"-NBaQ1hsWEdQ8e8jfno0": {
"a": [
287.40234375,
914.2578125,
285.8282165527344,
911.815185546875,
284.25408935546875,
909.37255859375,
282.2027282714844,
906.395263671875,
280.1513671875,
903.41796875,
276.78955078125,
898.291015625,
273.427734375,
893.1640625,
263.05816650390625,
875.205810546875,
252.68862915039062,
857.24755859375,
244.23464965820312,
836.911865234375,
235.78067016601562,
816.576171875,
231.94952392578125,
796.96484375,
228.1183624267578,
777.353515625,
227.5712432861328,
755.10546875,
227.0241241455078,
732.857421875,
228.2040557861328,
712.0361328125,
229.3839874267578,
691.21484375,
232.53750610351562,
669.8369140625,
235.69100952148438,
648.458984375,
241.21624755859375,
627.6552734375,
246.7415008544922,
606.8515625,
253.85009765625,
585.7490234375,
260.95867919921875,
564.646484375,
270.43902587890625,
545.890625,
279.9193420410156,
527.134765625,
291.63824462890625,
506.9345703125,
303.35711669921875,
486.734375,
319.550537109375,
467.6796875,
335.74395751953125,
448.625,
351.7040100097656,
431.328125,
367.6640625,
414.03125,
383.87725830078125,
400.537109375,
400.0904235839844,
387.04296875,
414.1638488769531,
376.61773681640625,
428.2372741699219,
366.1925048828125,
442.07720947265625,
358.2266845703125,
455.91717529296875,
350.2608642578125,
470.58245849609375,
344.24908447265625,
485.24774169921875,
338.2373046875,
502.2078552246094,
333.6962890625,
519.16796875,
329.1552734375,
537.0115966796875,
326.99169921875,
554.8552856445312,
324.828125,
573.3057250976562,
324.23046875,
591.7561645507812,
323.6328125,
609.4300537109375,
325.9619140625,
627.10400390625,
328.291015625,
643.134521484375,
333.2373046875,
659.1649780273438,
338.18359375,
672.086669921875,
344.4005432128906,
685.00830078125,
350.61749267578125,
695.598388671875,
357.0929260253906,
706.1884765625,
363.568359375,
716.1141357421875,
372.1502990722656,
726.0397338867188,
380.73223876953125,
733.9068603515625,
391.54205322265625,
741.77392578125,
402.35186767578125,
747.01904296875,
415.7406005859375,
752.26416015625,
429.12933349609375,
754.2264404296875,
446.02850341796875,
756.1887817382812,
462.92767333984375,
754.3673095703125,
480.7870788574219,
752.5458984375,
498.646484375,
744.2771606445312,
519.4296875,
736.0084228515625,
540.212890625,
724.942138671875,
563.345703125,
713.8757934570312,
586.478515625,
700.701416015625,
610.2060546875,
687.5270385742188,
633.93359375,
673.2900390625,
657.95703125,
659.0531005859375,
681.98046875,
644.9493408203125,
706.009765625,
630.8455200195312,
730.0390625,
616.2130126953125,
754.642578125,
601.58056640625,
779.24609375,
587.6033325195312,
803.85546875,
573.6260986328125,
828.46484375,
561.6184692382812,
853.6572265625,
549.61083984375,
878.849609375,
539.8404541015625,
901.712890625,
530.0701293945312,
924.576171875,
523.724853515625,
946.5576171875,
517.3795776367188,
968.5390625,
514.0665283203125,
988.7568359375,
510.7535095214844,
1008.974609375,
510.87347412109375,
1027.443359375,
510.99346923828125,
1045.912109375,
514.531982421875,
1063.783203125,
518.0704345703125,
1081.654296875,
524.076904296875,
1097.08642578125,
530.0833740234375,
1112.5184326171875,
539.637939453125,
1127.494140625,
549.1925048828125,
1142.4698486328125,
561.935302734375,
1155.6396484375,
574.6781616210938,
1168.8095703125,
592.721923828125,
1175.864013671875,
610.765625,
1182.91845703125,
624.5576171875,
1182.474853515625
],
"b": [
638.349609375,
1182.03125
],
"c": [
287.40234375,
914.2578125
]
},
"-NBaQO22Ehat0ALjMWjM": {
"a": [
462.744140625,
313.0859375,
461.35986328125,
318.212890625,
459.9755859375,
323.33984375,
459.0217590332031,
335.429931640625,
458.06793212890625,
347.52001953125,
457.765380859375,
377.057861328125,
457.4627990722656,
406.595703125,
460.219482421875,
430.9150390625,
462.9761657714844,
455.234375,
466.402587890625,
476.919921875,
469.8290100097656,
498.60546875,
473.2528076171875,
520.578125,
476.67657470703125,
542.55078125,
479.7127685546875,
565.396484375,
482.7489318847656,
588.2421875,
486.1727294921875,
611.9638671875,
489.59649658203125,
635.685546875,
493.16131591796875,
659.7177734375,
496.7261657714844,
683.75,
500.152587890625,
705.734375,
503.5790100097656,
727.71875,
505.56048583984375,
746.48046875,
507.5419921875,
765.2421875,
507.82147216796875,
782.5390625,
508.1009826660156,
799.8359375,
507.18707275390625,
814.2700805664062,
506.273193359375,
828.7042236328125,
507.20489501953125,
840.8909301757812,
508.1366271972656,
853.07763671875,
509.2940979003906,
863.48193359375,
510.4515686035156,
873.88623046875,
511.78076171875,
882.4845581054688,
513.1099853515625,
891.0828857421875,
514.0345458984375,
897.1476440429688,
514.9590454101562,
903.21240234375,
515.482421875,
906.684326171875
],
"b": [
516.005859375,
910.15625
],
"c": [
462.744140625,
313.0859375
]
},
"-NBb9loplZIj60V5cN9o": {
"a": [
286.875,
781.25,
285.38031005859375,
778.807373046875,
283.8856201171875,
776.36474609375,
279.40582275390625,
763.452880859375,
274.9260559082031,
750.541015625,
268.792724609375,
728.7763671875,
262.65936279296875,
707.01171875,
260.27447509765625,
688.853515625,
257.8895568847656,
670.6953125,
258.273193359375,
648.16015625,
258.6568298339844,
625.625,
263.25,
602.7587890625,
267.8431701660156,
579.892578125,
274.291259765625,
559.677734375,
280.7393493652344,
539.462890625,
288.5150146484375,
522.154296875,
296.29071044921875,
504.845703125,
304.5374755859375,
490.30426025390625,
312.7842102050781,
475.7628173828125,
321.53802490234375,
462.7079772949219,
330.2918395996094,
449.65313720703125,
341.31427001953125,
435.6225280761719,
352.33673095703125,
421.5919189453125,
366.1409912109375,
407.654052734375,
379.94525146484375,
393.7161865234375,
396.195068359375,
381.03851318359375,
412.44488525390625,
368.36083984375,
429.073974609375,
356.889404296875,
445.7030334472656,
345.41796875,
462.96563720703125,
336.9072265625,
480.22821044921875,
328.396484375,
498.4242248535156,
322.2353515625,
516.6202392578125,
316.07421875,
534.6790771484375,
312.833984375,
552.7379760742188,
309.59375,
569.884521484375,
309.2861328125,
587.0311279296875,
308.978515625,
602.824951171875,
311.5929870605469,
618.6187744140625,
314.20745849609375,
631.9264526367188,
319.0470886230469,
645.234130859375,
323.88671875,
656.0743408203125,
331.1795654296875,
666.91455078125,
338.472412109375,
676.9518432617188,
346.9519348144531,
686.9891357421875,
355.43145751953125,
696.461181640625,
365.9828186035156,
705.9332275390625,
376.5341796875,
714.7192993164062,
390.51422119140625,
723.50537109375,
404.4942626953125,
730.869140625,
419.087890625,
738.2328491210938,
433.6815185546875,
743.1556396484375,
449.69677734375,
748.078369140625,
465.7120361328125,
750.9462890625,
483.06402587890625,
753.8141479492188,
500.416015625,
755.2696533203125,
517.701171875,
756.72509765625,
534.986328125,
755.9486083984375,
553.431640625,
755.1720581054688,
571.876953125,
752.6737670898438,
590.9169921875,
750.1754760742188,
609.95703125,
746.360107421875,
630.4619140625,
742.5447998046875,
650.966796875,
737.1474609375,
672.0517578125,
731.7501220703125,
693.13671875,
725.16357421875,
715.40234375,
718.5770263671875,
737.66796875,
710.6748046875,
761.1025390625,
702.7725830078125,
784.537109375,
694.2032470703125,
808.5546875,
685.6339111328125,
832.572265625,
676.80224609375,
856.595703125,
667.9705200195312,
880.619140625,
659.66357421875,
905.22265625,
651.3565673828125,
929.826171875,
642.7832641601562,
955.0185546875,
634.2099609375,
980.2109375,
626.5582275390625,
1005.412109375,
618.9064331054688,
1030.61328125,
613.62109375,
1055.8056640625,
608.3358154296875,
1080.998046875,
603.7189331054688,
1104.4443359375,
599.10205078125,
1127.890625,
598.0302124023438,
1149.5849609375,
596.9583740234375,
1171.279296875,
597.8680419921875,
1190.9169921875,
598.7777099609375,
1210.5546875,
602.5545654296875,
1226.28271484375,
606.3314208984375,
1242.0106201171875,
613.6282348632812,
1255.324951171875,
620.925048828125,
1268.639404296875,
630.5732421875,
1279.55419921875,
640.221435546875,
1290.468994140625,
652.9228515625,
1299.1455078125,
665.6243286132812,
1307.8218994140625,
682.8577270507812,
1313.30517578125,
700.0911254882812,
1318.7884521484375,
718.96337890625,
1320.426513671875,
737.8356323242188,
1322.064453125,
756.2913818359375,
1320.0341796875,
774.7470703125,
1318.00390625,
794.647705078125,
1310.416015625,
814.54833984375,
1302.828125,
833.2782592773438,
1290.248046875,
852.0081787109375,
1277.66796875,
868.888427734375,
1261.2763671875,
885.7687377929688,
1244.884765625,
899.1092529296875,
1228.7744140625,
912.44970703125,
1212.6640625,
920.2583618164062,
1196.55078125,
928.0670166015625,
1180.4375,
931.6397705078125,
1164.6142578125,
935.2125244140625,
1148.791015625,
933.9205322265625,
1133.263671875,
932.6285400390625,
1117.736328125,
928.1593017578125,
1101.6259765625,
923.6900634765625,
1085.515625,
916.7364501953125,
1070.322021484375,
909.7827758789062,
1055.1285400390625,
901.1302490234375,
1040.73046875,
892.4777221679688,
1026.332275390625,
880.4939575195312,
1014.0601196289062,
868.5101928710938,
1001.7879638671875,
854.4459228515625,
991.6820068359375,
840.3817138671875,
981.5760498046875,
821.6927490234375,
973.5603637695312,
803.0037841796875,
965.544677734375,
782.0819091796875,
960.7225341796875,
761.1600341796875,
955.900390625,
738.4935302734375,
953.2548828125,
715.8269653320312,
950.609375,
693.4188232421875,
949.138671875,
671.0106201171875,
947.66796875,
648.3309326171875,
947.0791015625,
625.6511840820312,
946.490234375,
605.07421875,
945.6142578125,
584.4972534179688,
944.73828125,
567.0791015625,
942.11328125,
549.6609497070312,
939.48828125,
535.9382934570312,
935.7993774414062,
522.2156372070312,
932.1104736328125,
511.81805419921875,
926.3345336914062,
501.4204406738281,
920.55859375,
493.36309814453125,
912.709228515625,
485.3057861328125,
904.85986328125,
480.9227600097656,
895.1206665039062,
476.53973388671875,
885.3814697265625,
477.9674072265625,
868.1383666992188,
479.39508056640625,
850.895263671875,
486.45751953125,
830.8206787109375,
493.5199279785156,
810.74609375,
502.87103271484375,
790.82421875,
512.22216796875,
770.90234375,
522.894287109375,
750.1015625
],
"b": [
533.56640625,
729.30078125
],
"c": [
286.875,
781.25
]
},
"-NBbWVa2cPI6Di4pTGgd": {
"a": [
228.8671875,
1125.78125,
226.8626251220703,
1122.459716796875,
224.85806274414062,
1119.13818359375,
224.1599884033203,
1116.453857421875,
223.4619140625,
1113.76953125,
228.27293395996094,
1102.779541015625,
233.08395385742188,
1091.78955078125,
271.62847900390625,
1045.445556640625,
310.17303466796875,
999.1015625,
367.5770263671875,
946.6748046875,
424.9810485839844,
894.248046875,
473.43603515625,
856.091796875,
521.8909912109375,
817.935546875,
553.8743896484375,
795.2919921875,
585.8577880859375,
772.6484375,
600.555908203125,
764.0286865234375,
615.2540893554688,
755.408935546875,
618.8121337890625,
755.47998046875,
622.3702392578125,
755.5510864257812,
621.8701171875,
764.5994873046875,
621.3700561523438,
773.6478881835938,
609.2318115234375,
803.08251953125,
597.093505859375,
832.5172119140625,
579.4390258789062,
867.6394653320312,
561.7845458984375,
902.76171875,
548.6998291015625,
933.8515625,
535.6151123046875,
964.94140625,
535.14404296875,
981.8939819335938,
534.6729736328125,
998.8465576171875,
542.7091674804688,
1004.682373046875,
550.745361328125,
1010.5181884765625,
567.2940673828125,
1005.7432861328125,
583.8427124023438,
1000.9683837890625,
608.8720703125,
985.8201293945312,
633.9014892578125,
970.671875,
656.3201904296875,
953.685546875,
678.7388916015625,
936.69921875,
697.34619140625,
920.5712890625,
715.9534912109375,
904.443359375,
728.1439208984375,
897.5314331054688,
740.3344116210938,
890.6195068359375,
745.447265625,
889.2550659179688,
750.56005859375,
887.890625,
753.517333984375,
888.916015625,
756.474609375,
889.94140625,
761.6083984375,
893.1914672851562,
766.7421875,
896.4415283203125,
784.9820556640625,
897.8349609375,
803.2219848632812,
899.2283935546875,
844.113037109375,
892.4257202148438,
885.0040893554688,
885.623046875,
928.599609375,
878.01171875
],
"b": [
972.1951293945312,
870.400390625
],
"c": [
228.8671875,
1125.78125
]
},
"-NBbWVhTA8prRxo6Ncal": {
"a": [
275.009765625,
1599.21875,
275.20355224609375,
1594.62646484375,
275.3973693847656,
1590.0341796875,
281.7419738769531,
1575.442138671875,
288.0865783691406,
1560.85009765625,
313.8519287109375,
1526.257080078125,
339.6172790527344,
1491.6640625,
373.3277282714844,
1474.900390625,
407.0381774902344,
1458.13671875,
437.6478271484375,
1456.8798828125,
468.25750732421875,
1455.623046875,
482.6756591796875,
1468.997314453125,
497.09381103515625,
1482.3714599609375,
506.9387512207031,
1498.676025390625,
516.78369140625,
1514.980712890625,
524.2693481445312,
1532.1124267578125,
531.7550048828125,
1549.244140625,
538.1934204101562,
1562.647216796875,
544.6318359375,
1576.0501708984375,
551.3106689453125,
1581.963623046875,
557.9894409179688,
1587.876953125,
568.3466796875,
1586.3785400390625,
578.7039794921875,
1584.880126953125,
594.7960815429688,
1573.2850341796875,
610.88818359375,
1561.68994140625,
628.3709716796875,
1551.53125,
645.8536987304688,
1541.37255859375,
663.8954467773438,
1544.482177734375,
681.9371948242188,
1547.591796875,
698.556396484375,
1560.4384765625,
715.1756591796875,
1573.28515625,
730.3394775390625,
1585.9033203125,
745.5032348632812,
1598.521484375,
763.94580078125,
1604.421875,
782.3883056640625,
1610.322265625,
791.9754028320312,
1609.1650390625
],
"b": [
801.5625,
1608.0078125
],
"c": [
275.009765625,
1599.21875
]
}
}
}