Ege Akkaya
Ege Akkaya

Reputation: 186

Swift Firebase displaying from the database to a label.text in a tableview

I'm going to try my best to explain the question as it is a bit confusing. I'm creating a restaurant rating app for its users to rate restaurants(on a 1 to 5-star rating scale). I will be keeping the users votes on Firebase then calculate the average rating for that particular restaurant and display it in my app.

To summarize, in my main tableview there will be restaurant names and next to them their ratings, which are kept by a UIImage(star icon) and a label(to show their ratings). I have already created a comments section for each restaurant by keeping track with a parameter which cell(restaurant) the user has clicked on and based on that, that restaurant's node is created or updated in the firebase database and can show the comments specific to that restaurant in the app without a problem. When a restaurant is clicked, users are directed to that restaurant's comments section.

Now I'm also thinking about storing the ratings on the same nodes for each restaurant. As I said earlier, I want to store the overall ratings just next to the restaurants, in a label, before reaching to the comments section. My question is that after I store the ratings on my database, how will I actually manage to display the correct ratings to the labels? I mean I kept track of which comments to display in my comments section by creating a parameter to track which cell was clicked but now, I want my app to display the restaurant ratings directly, before the user clicks on the restaurant.

Is this possible? If yes, how can I manage this? I hope I explained things clearly. I'd appreciate any help. Thank you very much for reading my long post!

Upvotes: 0

Views: 194

Answers (2)

Modesto Cabrera
Modesto Cabrera

Reputation: 539

It should be fairly straightforward:

  • First model your rating system; Second keep instances of those rating;
  • create a method within your Restaurants class that returns the current rating based on the result of your rating system.
  • ** hint: (What this method returns is what you display on your label.) ***

Finally, In your tableview/collectionView label you should just be calling that method. If you do everything correctly. The tableview will collect that data and display regardless of any changes you make to your rating system in the future.

To be more explicit:

  • load your restaurants up to firebase.
  • when a user rates a Restaurant, have a method within the Restaurant class to handle such requests. e.g. currentRestaurant.updateRating()
  • Load the changes up to the firebase database.
  • in tableview/CollectionView call something like currentRestaurant.retreiveRating()

Also, your dataManager should be handling any uploads/downloads not the actual Restaurant object.

Upvotes: 0

MQLN
MQLN

Reputation: 2328

this question is a little bit outside the scope of how questions should be asked here, but I'll do my best to give you some advice here!

What you're looking is absolutely possible, and honestly a great place to start for an iOS project.

1: Firebase database is a great tool, but if you're starting a new project, you should really be using Firebase Firestore. Firebase Database is being deprecated (i.e. they're going to cut of support for it at some point), and Firestore is generally easier for to get the hang of.

2: A common approach to do what you're trying to do with Firestore is to have one document contain all the data about each restaurant, and then for each restaurant to contain a collection of all the comments.

3: Firebase also has a feature called Functions, which you should absolutely check out. Functions can run whenever something "happens" in Firestore. Thus it's actually pretty easy to create a function that whenever a new review/comment for a restaurant is created it collects all the reviews, creates an average, and then would update your restaurant with that average score.

So here's a rough idea of a data structure that works:

 [restaurants]
  -> {restaurantId}
    - about
    - name
    - blahBlahBlah
    - averageRating
    - [reviews]
      -> {reviewId}
      - text
      - rating

Happy coding!

Upvotes: 2

Related Questions