Search code examples
c#xamluwpbackground-taskvideo-library

UWP KnownFolder data caching


I am trying to accomplish following tasks with KnownFolders, i.e: VideoLibrary in my UWP app

  1. Faster Load Time. ( I need to load thumbnails and file names from the KnownFolders, which might take little more time if the library has huge number of files. Is there a faster way to cache and store them , maybe in sqlite database, and on each next run just get them from DB, can that be faster?

  2. Syncing with the system file changes ( if a new file is added or deleted within that specific library, while the app is running, how can the app be notified about that )

  3. Basically I want the app to load the library data faster as it opens so what would be the recommended way to achieve that? Should I use a background task for the app? but keeping data in memory when app isnt running might not be a good solution or is it? or maybe I can start the process of extracting data within App.xaml.cs as soon as the app launches ( before navigating to library page ) would that make performance improvement?

  4. Basically : I am looking for some suggestions and discussion here because there not might be a strict answer for my question here. Thanks in advance any suggestion will be appreciated. MY main 2 goals are to sync the library with delete and add files in the physical folder, and improve loading performance of library in the app. ( and yeah I am loading the library in a gridview with thumbnails as well.


Solution

  • StorageFileQueryResult fileQuery;
    
    async void CreateWatchFolder(StorageFolder folder)
        {
            var options = new QueryOptions();
            options.FolderDepth = FolderDepth.Deep;
            fileQuery = folder.CreateFileQueryWithOptions(options);
            _fileQuery.ContentsChanged += OnContentsChanged;
            var files = await _fileQuery.GetFilesAsync();
        }
    
        async void OnContentsChanged(IStorageQueryResultBase sender, object args)
        {
            // Do stuff, e.g. check for changes
        }
    

    For #2 of your question, this will give you a watch folder. Any time there is a change in that StorageFolder it will fire the event. It doesn't know which file changes were made, but you can write code to check each time. See here for more details.

    Regarding #1, it's really hard to say it's worth testing out. I personally have had terrible performance when playing around with Sqlite and EF with UWP, but I'm anything but an expert on that.

    Question #3, you might consider caching your thumbnails to one of the app-data folders. LocalCache would be the obvious choice, but actually if you do just Local then you can bind URI paths for the image sources in your UI. This doesn't work with images in LocalCache last I checked (about March or so) but it does with Local. It's much faster than loading the file as a StorageFile and then creating a Bitmap with it.