I have a Parse.com class, called Node, which contains a number column called cost.
My client app allows for creation of multiple nodes at once, which get saved to Parse using a SaveAll call.
I then have another class NodeTotals which will contain the total of all cost grouped according a certain criteria.
I am trying to implement an efficient method to do the totals in cloud code. My initial approach was to have an AfterSave function (on Node) that:
- queries all existing
Noderows that meet my criteria - sums the
coston the returned results - queries to see if a
NodeTotalentry exists, updates the total if yes, or creates a new entry if not
But this approach is not optimal I fear - for at least the following 2 reasons:
- performing all these queries on every
Nodesave cant be good - if a bunch of new
Nodeobjects are created by the client, which then does a SaveAll, multiple instances of my AfterSave function get called in parallel - each one thinks that there are no existingNodeTotalentries, so creates a new one (third point above) - resulting in more than oneNodeTotalrow being created where there really should be only one
I am pretty sure there must be a better way of doing what I am trying to do - just need some bright spark to point it out!