johnslaby
johnslaby

Reputation: 83

MvvmCross - how do I access SQLite in a windows store background task?

I have a store app that uses the mvvmcross sqlite plugin (community edition). This app has a periodic background task that accesses the database to get data to be shown in a live tile. I can't see how I can get access to this database from the background task. I would like to use the mvvmcross sqlite plugin in the background task, but I don't see how to initialize the mvvmcross environment properly.

Upvotes: 1

Views: 250

Answers (2)

johnslaby
johnslaby

Reputation: 83

I was able to solve the problem in a straightforward way. Since the background task only needed the SQLite data service from the PCL core project, I did the following:

  1. Included a reference to the Core project.
  2. Added the nuget packages for MvvmCross and the SQLite community plugin.
  3. Deleted all of the files and folders added when doing the mvvmcross install: Bootstrap/, Todo-Mvvmcross/, Views/, DebugTrace.cs, and Setup.cs.
  4. There is a current limitation in the nuget installer that requires some additional edits to the project file to handle multiple store platforms (x86, ARM, and x64), see 'Cirrius.Mvvmcross.Community.Plugins.SQLite.WindowsStore needs platform-specific dlls for X86 and ARM' on Stack Overflow for details. Make sure you put the Choose statement after the default SQLite.WindowsStore reference and you need to leave the default reference in the project file. You will also need to adjust the HintPath based on the location/names of your references.
  5. Initialized the SQLite data service by explicitly calling the factory and creating a new instance of the data service:

        var factory = new MvxStoreSQLiteConnectionFactory();
        IMyDataService repository = new MyDataService(factory);
    

I then have access to the data service with no other overhead associated with mvvmcross.

Upvotes: 1

Stuart
Stuart

Reputation: 66882

If you want to initialize the full MvvmCross framework including all of your app, then you'll need to run your Setup class.

In WinRT, this could be as simple as calling:

         var setup = new Setup(null /*rootFrame*/);
         setup.Initialize();

although it may require you to do a little work to:

  1. Make sure your presenter does not use the null rootFrame
  2. Provide some other means to create a UI thread dispatcher - currently MvxStoreViewDispatcher relies on .Dispatcher access - see https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.WindowsStore/Views/MvxStoreViewDispatcher.cs - to do this, you could override InitializeViewDispatcher with something like:

    protected override void InitializeViewDispatcher()
    {
        if (_rootFrame != null)
        {
             base.InitializeViewDispatcher(); return;
        }
    
        var dispatcher = new NonMainThreadDispatcher();
        Mvx.RegisterSingleton<IMvxMainThreadDispatcher>(dispatcher);
    }
    
    public class NonMainThreadDispatcher : MvxMainThreadDispatcher
    {
        public bool RequestMainThreadAction(Action action)
        {
            action();
        }
    }
    

If you want to initialize less functionality than the entire framework (e.g. for memory reasons) then you can also consider creating special Setup and App classes just for your background task.

Aside> This is similar to questions like these in Android - Using MvvmCross from content providers and activities and MvvmCross initialization

Upvotes: 1

Related Questions