1
votes

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 Node rows that meet my criteria
  • sums the cost on the returned results
  • queries to see if a NodeTotal entry 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:

  1. performing all these queries on every Node save cant be good
  2. if a bunch of new Node objects 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 existing NodeTotal entries, so creates a new one (third point above) - resulting in more than one NodeTotal row 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!

1

1 Answers

0
votes

for anyone who comes across this, i ended up doing the following :

in order to address issue 1) I made sure that the Node Total was created at the point in time that the Node was created - thereby eliminating the need to query for existence at time of update every time

for issue 2) is used the Parse Object.Increment function to ensure atomic operation