I've built a music player app which automatically fetches songs from a server at start-up.
The issue is that, if I use NetworkImage to display the covers for the songs on an overview page, they are refetched everytime I leave the page and then return to it.
I already tried using CachedNetworkImage, but I don't want to store the song covers in the users cache. The images should persist for one session and then be disposed.
I could probably clear the cache everytime the user closes the app but there has to be a better way
You could try storing the images in memory while app is running, then dispose when app is closed.
Third party libraries such as flutter_cache_manager can help achieve this.
It allows persistence of images for app session's duration.
Related
Hi everyone I need help with downloading images in background in our flutter app
We are showing users list of images in our app we need to allow users to select these images and save them to thier phone but the download has to happen in the background so that the user can continue using rest of the features of the app
Our app is developed in flutter
Our backend is written in laravel 8
And database is SQL
I am a newbie and so I don't have much clarity regarding what more information shud I put in to help you all understand the code so I am adding a screenshot of the UI of our app search page
where we need to allow users to select multiple images and save them to their phone local storage but the download has to happen in background as the image size is big
You can use a async function where you call it and the app goes on. In the background all images are going to be saved on the phone while you still can use the app
My app uses a-lot of cached images which (I think is the reason causing my app to crash) when i scroll to view so many at one time.
My app is all about images inside list/grid view.
Is it good practice to cache them all?
Note:
Please note I'm using SliverGrid which is loading images lazily.
If you going to manage a buch of amount image is better, download them to a local directory and take the url like a name in a Data Base then you can ask on every request if your url is on your data base if is there then the file in your directory exist if not, you do the request and store it file in directory and name on db. This is better when your app manage a buch of information in cache like image, because on some devices the app can be crash o even restar the device.
I am trying to store images locally while the user is running the app and also keep the images stored locally while the app is in the background. When the user terminates the app, then the data should disappear locally and only be loaded once the user reopens the app. I know how to load the images initially, but I am considering where/how to store the images locally.
I know that Swift has the file system, but I was wondering if this is the best practice for this kind of implementation or if there is a better solution.
Doing exactly what you want is not possible, but you can do something similar in functionality.
To cache images you can simply use an existing library, like SDWebImage. It manages asynchronous image downloading and caching for you, and to clear cached images you can simply do
SDImageCache.sharedImageCache().clearMemory()
SDImageCache.sharedImageCache().clearDisk()
Now, as for when you want to clear the cache, it get's a little tricky.
You can't clear the cache when terminating the app, because there is no way to reliably detect when the app is terminated. There is UIApplicationDelegate.applicationWillTerminate method, but it is not called if the app is terminated while in suspended state.
But you can do a workaround: simply clear the cache upon launching the app in UIApplicationDelegate.application(_:didFinishLaunchingWithOptions:), which will be functionally equivalent of what you want to do.
I created a web app and added to my iPhone Home Screen. When I switch to another app and back, iPhone automatically reload my web app. This breaks my app flow.
How do I prevent iPhone from reloading the app?
I have apple-mobile-web-app-capable meta tag enabled to hide Safari toolbar and I don't want to turn it off.
I just found this related question on SO: Stop native web app from reloading itself upon opening on iOS
As it seems it's a limitation of Safari, a proposed solution is to persist your web apps state using Javascript and HTML5 localStorage. When your web app is launched, check for the persisted state and load it if available.
You can read about using localStorage in Safari here: http://developer.apple.com/library/safari/#documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007256-CH1-SW1
Hope that helps you. At least it did for me, as I had the same problem as you. :-)
The short answer is that you can't control this. Sometimes iOS will keep a web app active in the background, at other times it will kill it. It's entirely related to how much memory is available on the device.
So, your best approach is to minimise the problems presented by this reload. Make sure your webapp updates the URL when you move from view to view, either by changing location.hash or using history.pushState(). This will allow you to reload whatever view the user was on before they switched apps. There are pagehide and pageshow events that allow you to execute code when the user moves away from your app - take that opportunity to store local state in localStorage and/or IndexedDB, then fetch that data again when the webapp is reopened.
I found a hack, tested on iOS 11.4.1/12.0
Open file uploading window and then switch back to the home screen.
The app still continues to work, in my case audio is playing and localStorage is updating
Proofs:
https://youtu.be/heehLUhGKYY
PS. note how song progress changes when we seek, it proves that app works in the background
Update: as this answer is receiving downvotes, I added this explanation.
Your problem might not be the actual reload, but the fact that Mobile Safari treats your user's cache and cookies differently when your web app is opened through the browser, than when it's 'installed' as a web app to the home screen. Although the solutions proposed here that use localStorage will work, they're a lot of work for client-side logic that can be avoided if your server is already responsible for persisting the session state of your user. The 30-second solution is to simply explicitly set the session cookie to have a longer lifetime.
This allows you to keep the state intact even between device reboots, so even though it doesn't technically stop the web app from being reloaded when launched from the home screen, it is an easy way to restore the state for the user without him/her noticing the reload - which in many cases I suspect is the real problem.
For a more elaborate discussion of this strategy and code examples, take a look at these questions and my answers there:
Maintain PHP Session in web app on iPhone
iPhone "Bookmark to Homescreen" removes cookies and session?
I'm currently creating an iPhone app where in one part of my app you can view your twitter stream. I'm unsure if I need to ever save the twitter information to a sqlite database or not.
So here is the flow of this part of the app:
press button to see twitter stream
go get twitter stream
display twitter stream in table view
I'm wondering if I should ever save the twitter stream into a database. Any advice?
I would say you should save the twitter stream. You should almost always try to save some application state in an iPhone app. This way, if the user is interrupted (a phone call) they can jump back into your app without missing a beat.
There are a few different ways to persist data in an iPhone app. Instead of bothering with using a SQLite database you will almost certainly want to use Core Data, which is new in iPhone OS 3.0
If you won't ask the user to provide his/her twitter credentials and it will be an anonymous stream, you don't need to store anything.
But the minute you want to store some preferences, actual state (to show the user what he/she was seeing when a phone call came or after application restart) you will need to store persistent data.
I think it's important to cache web data. With a cache, you can present data immediately on app startup - this is important on the iPhone OS because users are constantly opening and closing apps. Having your data immediately available is a big win for the user.
You can make the caching very simple, just have a single table with the URL as one column and the HTTP response as a second. Then you don't have to change any of your code to make the caching happen.
Alternatively, you will need to define a data model and manage that through CoreData or sqlite.