Firebase Storage: How to reduce requests? (iOS) - ios

I'm developing a chat app with Firebase. Am currently still in development phase.
Profile pictures of test users are uploaded to Firebase Storage, and are downloaded in the home screen (with all the pictures). I realized that with that I very quickly used up storage download requests (easily hit 3,000 requests in one night, and hit the free plan quota!).
What are some best practices I could use to minimize download requests? Just to be sure I'm doing it right - I'm sending a GET request to the Firebase Storage url directly: https://firebasestorage.googleapis.com/... to download the image. Is that the right way to do it?

Two suggestions that might help:
Cache your images! If you keep requesting the same images over and over again over the network, that's going to use up your quota pretty fast. Not to mention your user's battery and network traffic. After you retrieve an image from the network, save it locally, and then the next time you need an image, look for it locally before you make another network request. Or consider using a library like PINRemoteImage that does most of the work for you. (Both on the retrieving as well as the caching side)
Consider uploading smaller versions of your image if you think you might be using them often. If your chat app, for instance, saves profile pictures as 1024x768 images, but then spend most of its time showing them as 66x50 thumbnails, you're probably downloading a lot of data you don't need. Consider saving both the original image and a thumbnail, and then grabbing the larger one only if you need it.
Hope that helps...

Related

How to achieve lightening fast image download like Instagra

I am making an app that displays images in tableViewCells similar to Instagram using Firebase as text based database and Amazon S3 as the storage for image file. I use Alamofire to download image in my app.
I was looking at the Instagram app and realized that they can display so many photos so quickly. I was wondering what is the general guide to achieve something like that? Taking into consideration of things like
When to start download the image?
Should the download stop at some point if required?
Is there ways to speed up the process with firebase, S3, Alamofire or is this combination doomed to be capped at an UN-satisifiable speed?
Caching image after download (already doing) and load from cache when possible)
How many parallel download should be going on at one time?
Note. I believe Instagram imags are 200kb~300kb each.

Upload photos with NSURLSession in background

I want to use NSURLSession to upload photos to cloud server(like OneDrive) and I hope the app can do works in background.
But NSURLSession just support "fromFile" not "fromData" in background mode, and I don't know how to get the NSURL of photos, so I write the photo's data into a file...
I think this is not a good way.
From the comments above, it seems like you'll be uploading several photos at a time and for that a "fromData" approach does not seem like a good idea.
Firstly, high resolution photos have several megabytes in size. If your loading a 5mb photo into memory, using NSData, you're probably fine. But loading 10 photos would then mean you'd need 50mb of memory to hold them all.
Secondly, uploading large chunks of data, such as photos, is also a computationally intensive task. You should most definitely not try to upload them all at the same time. This means you're going to have to build an upload queue that only starts uploading the next photo once the last one has finished. What happens then if internet connectivity is lost midway through the process? And what happens if the user closes the app all of the sudden?
Thus, I'd suggest you grab all the images' data and write them to a temporary folder somewhere. Then you start the upload process, which would find a file on the temporary folder, upload it, delete it once the upload finishes successfully and start over with the next file.
This way you're minimising your app's memory footprint, making it more resilient in the face of the many adversities an app may face (no internet connection, being prematurely closed by the user and so on) and also abiding by the APIs that are provided to you.

How to cache images in Meteor?

I'm building a mobile app using Meteor. To allow for offline usage of the app, I want the app to be able to download a large-ish json file while online, then access the data in the json file, written to MongoDB, while offline.
This works fine. However, in the downloaded json file, there are plenty of references to online images that won't display in the app once the app is offline.
So, I want to be able to download (a selection of) the images referenced in the json file to the app, so that the app can access them even when offline.
(Downloading images could happen in the background for as long as a connection is available.)
There's an implementation of imgCache.js available on Atmosphere, which fails to initialize for me.
I suppose it's theoretically possible to individually load each image to a canvas, save the canvas content to MongoDB, then load the content when needed. Info on some of this is here. But, this feels rather convoluted and, if really feasible, I would expect someone to have done this before with success.
How can I do achieve caching of images for offline use in Meteor?
So, you've probably already read this article about application cache.
If the images are static, you can just include them in the manifest. Be sure you understand the manifest and cache expirations (see the article).
If the images are dynamic, you'll find some techniques to store images in local storage
If that's the case, this may be what you want.

iOS app file storage. YouTube, parse.com or local?

I have an iOS app that uses a lot of pictures and videos. Currently, I use parse.com as backend and I store the pictures in parse.com database. Imagine I have 200 tasks, each task with two pictures and one video. Now, each user will have his own set of tasks, for example, user A may have 20 tasks, user B 40 and user C 50, all different tasks included on those 200.
Now, I'm worried about app performance, not so much server storage and other structure details...
I don't think including all pictures in the app bundle is a good idea, because user will use only about 40% of the pictures at a time, so it wouldn't be wise to include it all in the app. Instead, I have the pictures in database and when user needs, the app makes a request and gets it. Am I right about this? Is this the best performance?
Regarding the videos, should I store the videos in youTube.com or in my parse.com database? I think youTube is the best option because I can live stream the video whether in parse.com I will need to download the video first. Does that make sense?
If there are any other services or better way to handle pictures and videos in an iOS app, please let me know.
Regarding images - the best performance here is to store them remotely on a server and from what it seems, you're doing it right.
Storing videos on YouTube is not the best idea but definitely better then just storing them on your server - if you have the ability to upload multiple Youtube videos from lots of users and if Google API allows that - I'd go for this option.
In general I would store as little as possible on the device, apart from the image cache maybe. Nevertheless even the image cache sometimes grows to hundreds of MBs so you have to think about it as well.

How to download and store images to not be cleared by device later?

I have an application which downloads its product data(descipttions and images) from server and stores them localy to be available offline. For product images I'm using forge.file.cacheURL which works great but on my iPAD, this cache is being cleared during the day when I work with another apps. This causes my application to have only descipription texts available without images and user must connect to internet and synchronize again what is quite annoying. Is there a better way how to implement this scenario?
Have you tried to use the forge.prefs module for this purpose? It allows you so persistantly save key-value-pairs locally (much like HTML5 localstorage). Read more about the exact syntax in Trigger.io's official API documentation.
I'm not quite sure whether its possible to save images with this method, but you could easily transform your images into strings and vice-versa. Check out the second chapter of this post by Robert Nyman on how to save images in localStorage.

Resources