Show ratings of item in uitableview cell

前端 未结 2 1314
旧时难觅i
旧时难觅i 2021-01-20 05:48

I am currently working on a iOS app that shows 30 menu items in a UITableView and Users can then rate theses items out of 5 starts on the detail page which is shown when a u

2条回答
  •  悲&欢浪女
    2021-01-20 06:32

    You don't need Cloud Code Jobs or anything else. You can simply create another class having these attributes: itemId, numberOfVotes, averageRating. When user post a new rating, do the following on his device:

    averageRating = (averageRating*numberOfVotes + userVote) / (numberOfVotes+1)
    numberOfVotes++
    

    and store these two values in the cloud. When you need to display average rating, simply fetch averageRating value for the item.

    EDIT

    When I said about an extra class, I meant that you don't need to store every rating in the cloud. You can use just one class and store only an average rating for each item and a number of votes. And you can fetch average ratings for multiple items with a single API call by making a query like that:

    NSArray *ids = @[@"1",
                     @"2",
                     @"3"];
    [query whereKey:@"itemId" containedIn:ids];
    

    If you want to store each user's ratings (to check if user has already voted on that particular items or to display his ratings on another device), you can store them in iCloud container, that way you will only need one API call when user casts a vote and one call when user fetches 30 (or any number) of items.

    You can also look into using CloudKit instead of Parse, if you don't need to support iOS 7 and lower.

提交回复
热议问题