Turkish Things utilizes Parse as its backend. I heard about Parse during my iOS course at Flatiron School and thought that it was a super cool idea to get started with an app, with a really short learning curve to start sorting and retrieving data.
I have been building web and PC apps based on relational databases for years. One of the biggest challenges I had with Parse was wrapping my head around NoSQL which Parse works with. I am used to creating relationships using Foreign Keys — which you really don’t do when you’re building apps on a NoSQL database. You instead use “Relations” — where you embed pointers to related objects on other tables.
Everything starts making more sense after writing some test code to see how all this works.
One of the other challenges that this switch to NoSQL brought was the lack of “GROUP BY” queries which I heavily rely on traditional relational databases. On Turkish Things, we have venues, and each venue has multiple reviews. Each review has a star rating, and a venue has an “average rating” which is calculated by:
Average Rating for Venue: Total Ratings / Review Count.
With the lack of AVERAGE, SUM and GROUP BY commands that we use with SQL, I had to come up with an alternative solution. While it’s debatable whether or not this makes the most sense, but I decided to keep two columns on my Venues table named: ratingCount and ratingTotal to be able to calculate the average rating.
There was one problem of updating these fields. When should I increment the ratingCount and calculate a new sum for ratingTotal? Should I do that on the device, every time a new review is inserted to my database? Sounded too costly.
This is what the code looks like:
The algorithm first finds the “Venue” that the Venue Review is associated with. Then it increments the ratingCount by one, and then adds the new rating to the ratingTotal variable. This way I can accurately calculate the average rating for each venue on Turkish Things.