I think that the firebase function updates the list that I have in the firebase database , the subscription that is subscribed to this list is written. From what the output of the list looks like on my phone (in the application) ... and from what my console output looks like (how it repeats), it seems that it captures the whole list and displays it every time it is added. So (I looked at this) ... I believe that this equation is what happens:
(N(N + 1))/2
How do you get the sum of all numbers from 1 to N. By doing the math in my case (N = 30 or so), I get about 465 entries ... so you can see that this is loading tone when I want it to downloaded the first 10.
To show what happens with the exit, here is pastebin https://pastebin.com/B7yitqvD .
In the output, pay attention to the array that is above / to length - 1 load . You can see that it quickly returns an array with one more record each time and adds it to the list. I did a very rough count of the number of items in my list, and I got 440 ... so it roughly corresponds to number 465.
The chain of events begins on a page that is not a list page with this function, which initiates sorting on the firebase functions side:
let a = this.http.get('https://us-central1-mane-4152c.cloudfunctions.net/sortDistance?text='+resp.coords.latitude+':'+resp.coords.longitude+':'+this.username); this.subscription6 = a.subscribe(res => { console.log(res + "response from firesbase functions"); loading.dismiss(); }, err => { console.log(JSON.stringify(err)) loading.dismiss(); })
Here is the function on the list page, which, in my opinion, captures the whole view for some reason. I believe the subscription is repeated as a firebase function .
loadDistances() { //return new Promise((resolve, reject) => { let cacheKey = "distances" let arr = []; let mapped; console.log("IN LOADDISTANCES #$$$$$$$$$$$$$$$$$$$$$"); console.log("IN geo get position #$$$$$$$5354554354$$$$$$$"); this.distancelist = this.af.list('distances/' + this.username, { query: { orderByChild: 'distance', limitToFirst: 10 }}); this.subscription6 = this.distancelist.subscribe(items => { let x = 0; console.log(JSON.stringify(items) + " length - 1 load"); items.forEach(item => { let storageRef = firebase.storage().ref().child('/settings/' + item.username + '/profilepicture.png'); storageRef.getDownloadURL().then(url => { console.log(url + "in download url !!!!!!!!!!!!!!!!!!!!!!!!"); item.picURL = url; }).catch((e) => { console.log("in caught url !!!!!!!$$$$$$$!!"); item.picURL = 'assets/blankprof.png'; }); this.distances.push(item); if(x == items.length - 1) { this.startAtKey4 = items[x].distance; } x++; }) //this.subscription6.unsubscribe(); }) }
The subscription in loadDistances function works fine until I refresh the list from another page - another indicator that it can capture the whole view and list it again as it is sorted.
I tried how I could come up with unsubscribe from the list after I updated ... so then I could just load the list of 10 the next time the page enters the list, and not immediately after updating (again and again). I know that firebase functions is in beta . Could this be a mistake on their side? Here is my firebase functions code:
exports.sortDistance = functions.https.onRequest((req, res) => { // Grab the text parameter. var array = req.query.text.split(':'); // Push the new message into the Realtime Database using the Firebase Admin SDK. // Get a database reference to our posts var db = admin.database(); var ref = db.ref("profiles/stylists"); var promises = []; // Attach an asynchronous callback to read the data at our posts reference ref.on("value", function(snapshot) { //console.log(snapshot.val()); var snap = snapshot.val(); for(const user in snap) { promises.push(new Promise(function(resolve, reject) { var snapadd = snap[user].address; console.log(snapadd + " snap user address (((((((())))))))"); if(snapadd != null || typeof snapadd != undefined) { googleMapsClient.geocode({ address: snapadd }).asPromise() .then(response => { console.log(response.json.results[0].geometry.location.lat); console.log(" +++ " + response.json.results[0].geometry.location.lat + ' ' + response.json.results[0].geometry.location.lng + ' ' + array[0] + ' ' + array[1]); var distanceBetween = distance(response.json.results[0].geometry.location.lat, response.json.results[0].geometry.location.lng, array[0], array[1]); console.log(distanceBetween + " distance between spots"); var refList = db.ref("distances/"+array[2]); console.log(snap[user].username + " snap username"); refList.push({ username: snap[user].username, distance: Math.round(distanceBetween * 100) / 100 }) resolve(); }) .catch(err => { console.log(err); resolve();}) } else { resolve(); } }).catch(err => console.log('error from catch ' + err))); //console.log(typeof user + 'type of'); } var p = Promise.all(promises); console.log(JSON.stringify(p) + " promises logged"); res.status(200).end(); }, function (errorObject) { console.log("The read failed: " + errorObject.code); }); });
Which is strange, when I check the firebase functions logs, all this seems to work only once ... but I still think that the subscription can capture the whole sorting process in some strange way, quickly returning it. To be as clear as possible, with what I think is happening - I think that every stage of this kind is captured in (N(N + 1))/2 ... starting at 1 and approaching about 30 ... and the sort amount ends (1/10 length repeats over and over).