简体   繁体   中英

Gradually downloading Firebase children iOS

So I have an iOS Firebase project that works kind of like Tinder. There is a node that contains all products as children.

When the user enters the app, he/she can swipe left for products they don't want and right for ones they want to save.

My issue is that currently, my Firebase project downloads all the products at once which obviously isn't viable for 100+ products with images. I want to implement a system that downloads 3 products at first. Then, once the user swipes left/right for the first product it downloads another one and adds it to the back of the list so that there is always a three product buffer.

The question is, how can I get Firebase to spit out one .childAdded only when the user swipes (my children are all .childByAutoId )?

Bear in mind that products can be deleted, so simply keeping an index of what item we are currently at isn't possible.

Thank you.

The first thing that comes to mind is, you simply keep track of how many items you currently have in buffer. If it is lower than the minimum, you will download more, Firebase does all download operations in background so no need to worry about that. You can also use closures on the counters to automate the downloads.

To download a specific amount out of a list of child nodes, check out firebase queries. They are used for sorting the nodes that are returned to you as well as for getting nodes at specific indices.

However, this does not solve your problem of nodes being potentially deleted at any given moment. For that, you could try to calculate the hash of the current item being viewed and compare it against a hash table of items that have been viewed in the current session. If a match is found then that item has been viewed already and there has been some shuffling of indices in the database. (ie an item has been deleted)

Just a thought.

You need to store some sort of marker with each item (perhaps a timestamp if you wish to gradually download the items based on the child's creation time).

Then simply use a query like this:

var startingValue: String = ""
if itemsArray.count == 0 {
   startingValue = Date()    // start at the current date and work your way backwards
}
else {
   startingValue = itemsArray.last.timestamp   // last item we stopped at in the retrieval process
}

itemsRef.queryOrderedByChild("timestamp").queryStartingAtValue(startingValue).queryLimitedToFirst(numberOfItemsToBeRetrievedPerQuery).observeEventType(FIRDataEventType.value, withBlock: { (snapshot) in
    // append the retrieved items to your array
})

Note that in order for this to work (getting the most recent items first, which I'm assuming is what you're trying to do), you need to store the UNIX timestamps with a negative sign (so it would be reversed). So your structure would look something like this:

"items" : {
     "item1Key" : {
       "itemName": "Item 1"
       "moreItemData": "random data"
       "timestamp": -1.57983164250361E12
     },
     "item2Key" : {
       "itemName": "Item 2"
       "moreItemData": "random data"
       "timestamp" : -1.42182335765741E12
     },
     ...
}

You may find this question 's answer helpful with your implementation!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM