Firebase - Effective User Management - Swift

I have an application in which the user may like things in the application, for obvious reasons, the user cannot love something twice. To make sure of this, when the user likes something, I save the article key in a list in the database. The problem is that the most efficient way to load a table is to check if the user liked this article already.

But after that I need advice, as I see it. I have two ways I can do this.

  • I can load the entire list when the application starts, and then, when the table view is loaded, I can check if the article key is in the list of already similar keys. (This is what I'm leaning towards). The only problem with this is that when the user liked to say 1000 things, then every time the application starts, he will have to download 1000 lines from Firebase, which is a lot, another problem means that the articles can't start downloading until this request is completed.

  • Pass the article key to the cell, and then query the database for that key to find out if it exists, the reason I'm worried about this is that it means that every request will be executed every time the cell loads , and this is a lot of requests and network activity. I am not sure if this will be a problem.

Thanks in advance!:)

+4
source share
1 answer

I would suggest not preventing duplication of "morals" on the client side; it will be too comprehensive as you described. You can do two things:

  • You can prevent duplication by structuring your database as follows:

    {
      "likes": {
         "user1Key": {
            "objectLikedKey1": true,
            "objectLikedKey2": true
            ...
         }
       }
    }
    

Thus, the data structure will prevent duplication by default. Think of it as a set!

  1. If your database is structured differently and for some reason you need it, you can use the rule to prevent duplication. (I'll just take a random structure here so you can get this idea)

    {
      "rules": {
         ".read": true,
         "userLikes": {
            "$user": {
               ".write": "!data.exists()"
            }
         }
       }
    }
    

This means that you can only write to the database if the data does not already exist.

Update:

, , ""

    {
      "objects": {
         "object1Key": {
            "user1Key": true,
            "user2Key": true
            ...
            "timestamp": "2017-01-01 12:00:00 +0000"
          }
       }
    }

(1) , . , , .

- Facebook- , , , . , - , ( 5 10) , . , , , , , , , .

, , . , "" , , , , Firebase .

:

- , , , , :

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        let lastElement = yourDataSource.count - 1
        if indexPath.row == lastElement {
            // load more
        }    
}

-Pagination - , , , , , ( , , ), . queryOrderedByChild(), queryStartingAtValue() queryLimitedToFirst(), , , !

+2

Source: https://habr.com/ru/post/1665302/


All Articles