问题
I am creating a Firebase Android and Web app, that simply collects weight data of model trucks on an event base. So I have a list of trucks and their properties. All cars attending the event are listed in the database under the attendingCars path, sample for one entry:
"attendingCars" : {
"-K_t-PnY49QJP5QXQJ5_" : {
"carDescription" : "MB SK Sattel mit Kippmulde",
"carEmptyWeight" : 7.456999778747559,
"carLoadCnt" : 13,
"carLoadSummary" : 12.059002876281738,
"carLoadTotal" : 109.00000762939453,
"carOwnerAlias" : "Juergen",
"carThumbHeight" : 120,
"carThumbImage" : "https://firebasestorage.googleapis.com/v0/b/freight-weight.appspot.com/o/images%2FcarThumbImage%2FJPEG_20170107_135650_1498993627.jpg?alt=media&token=85c67cc2-8fbf-4196-a5a5-2456f9af8cb4",
"carThumbWidth" : 160
}
}
This data of the cars is also used to present the user a summary of all weight data collected per car, kind of ranking list using Firebase-UI.
There is a separate path per event called "weightDB", where each weight is collected, so all individual ones. Part sample below...
"weightDB" : {
"-Kbam_vawQAITyokJSTi" : {
"carDbItem" : {
"description" : "MB SK Sattel mit Kippmulde",
"emptyWeight" : 7.456999778747559,
"ownerUserID" : "JE0hF1E3XGOHxLCG2qFLRLOnp693",
"pictureHeight" : 1944,
"pictureThumbHeight" : 120,
"pictureThumbUri" : "https://firebasestorage.googleapis.com/v0/b/freight-weight.appspot.com/o/images%2FcarThumbImage%2FJPEG_20170107_135650_1498993627.jpg?alt=media&token=85c67cc2-8fbf-4196-a5a5-2456f9af8cb4",
"pictureThumbWidth" : 160,
"pictureUri" : "https://firebasestorage.googleapis.com/v0/b/freight-weight.appspot.com/o/images%2FcarImage%2FJPEG_20170107_135650_1498993627.jpg?alt=media&token=241ca576-f618-406e-9e69-f7b655b12672",
"pictureWidth" : 2592
},
"carDbItemKey" : "-K_t-PnY49QJP5QXQJ5_",
"freightTypeItem" : {
"name" : "Frachtgut"
},
"freightTypeItemKey" : "manualFreightTypeID-5",
"loadWeight" : 2.3430004119873047,
"loggerUserID" : "M9EkACOXX7SVWqRHmqVB2euErO03",
"ownerAlias" : "Juergen",
"totalWeight" : 9.800000190734863
},
"-KbaqrWbOFotXb2ztu7u" : {
"carDbItem" : {
"description" : "MB SK Sattel mit Kippmulde",
"emptyWeight" : 7.456999778747559,
"ownerUserID" : "JE0hF1E3XGOHxLCG2qFLRLOnp693",
"pictureHeight" : 1944,
"pictureThumbHeight" : 120,
"pictureThumbUri" : "https://firebasestorage.googleapis.com/v0/b/freight-weight.appspot.com/o/images%2FcarThumbImage%2FJPEG_20170107_135650_1498993627.jpg?alt=media&token=85c67cc2-8fbf-4196-a5a5-2456f9af8cb4",
"pictureThumbWidth" : 160,
"pictureUri" : "https://firebasestorage.googleapis.com/v0/b/freight-weight.appspot.com/o/images%2FcarImage%2FJPEG_20170107_135650_1498993627.jpg?alt=media&token=241ca576-f618-406e-9e69-f7b655b12672",
"pictureWidth" : 2592
},
"carDbItemKey" : "-K_t-PnY49QJP5QXQJ5_",
"freightTypeItem" : {
"name" : "Frachtgut"
},
"freightTypeItemKey" : "manualFreightTypeID-5",
"loadWeight" : 0.3430004119873047,
"loggerUserID" : "M9EkACOXX7SVWqRHmqVB2euErO03",
"ownerAlias" : "Juergen",
"totalWeight" : 7.800000190734863
},
"-KbarlMmUQau_JsJFIZ6" : {
"carDbItem" : {
"description" : "MB SK Sattel mit Kippmulde",
"emptyWeight" : 7.456999778747559,
"ownerUserID" : "JE0hF1E3XGOHxLCG2qFLRLOnp693",
"pictureHeight" : 1944,
"pictureThumbHeight" : 120,
"pictureThumbUri" : "https://firebasestorage.googleapis.com/v0/b/freight-weight.appspot.com/o/images%2FcarThumbImage%2FJPEG_20170107_135650_1498993627.jpg?alt=media&token=85c67cc2-8fbf-4196-a5a5-2456f9af8cb4",
"pictureThumbWidth" : 160,
"pictureUri" : "https://firebasestorage.googleapis.com/v0/b/freight-weight.appspot.com/o/images%2FcarImage%2FJPEG_20170107_135650_1498993627.jpg?alt=media&token=241ca576-f618-406e-9e69-f7b655b12672",
"pictureWidth" : 2592
},
"carDbItemKey" : "-K_t-PnY49QJP5QXQJ5_",
"freightTypeItem" : {
"name" : "Frachtgut"
},
"freightTypeItemKey" : "manualFreightTypeID-5",
"loadWeight" : 0.44300031661987305,
"loggerUserID" : "M9EkACOXX7SVWqRHmqVB2euErO03",
"ownerAlias" : "Juergen",
"totalWeight" : 7.900000095367432
},
Now obviously, I need to summary the weight values per car. Right now the only way to do this, is to do an addChildListener on the "weightDB" and every time a weight is added, I recalculate all sum of all cars. First I thought I could do this when I add an weightDB item, but when I am offline or multiple APPs will do this at the same time, this will lead to false data. So at the moment each APP does the calculation and write it back to the database (only if it sum more weight data than already there, based on the attendingCars/$car/carLoadCnt value I want to have the sum in the database, so I have it ready for display in the Web and Android APP, without looking at the weightDB itself.
But I end up with a lot of listeners I have to take track of + it sound stupid to do it this way.
Hope someone can point me to a better way.
If I missed to provide anything, let me know, but I think the data in use should be enough.
Right now I focus on the Android APP and the Web frontend only displays the summary.
Thanks
来源:https://stackoverflow.com/questions/41915357/best-practize-summarize-firebasedatabase-values