I am trying to access the indexeddb from my app (so I can update a live tile) in a javascript background task.
I am setting up the task like so:
var builder = new Windows.ApplicationModel.Background.BackgroundTaskBuilder();
builder.name = taskName;
builder.taskEntryPoint = "js\\task\\backgroundTask.js";
//Run every 8 hours if the device is on AC power
var trigger = new Windows.ApplicationModel.Background.MaintenanceTrigger(480, false);
builder.setTrigger(trigger);
var task = builder.register();
The task looks like this:
(function () {
"use strict";
var backgroundTaskInstance = Windows.UI.WebUI.WebUIBackgroundTaskInstance.current;
var cancel = false;
function doWork() {
var key = null, settings = Windows.Storage.ApplicationData.current.localSettings;
//todo access db and update tile
close();
}
if (!cancel) {
doWork();
} else {
close();
}
function onCanceled(cancelSender, cancelReason) {
cancel = true;
}
backgroundTaskInstance.addEventListener("canceled", onCanceled);
})();
The task seems to work fine but I can’t work out if I can access the db (or any of the apps resources). Is this sort of thing even possible, if so what have I missed?
According to http://msdn.microsoft.com/en-us/library/windows/apps/hh453270.aspx, the WorkerGlobalScope in a Store app, which is in effect when you’re in a background task, makes indexedDB available. IndexedDB works within local appdata, so should be usable as any other appdata operations.
Generally speaking, background tasks can modify app data and issue tile/toast/badge updates. As Slace points out, you can’t access other app resources, so appdata is how you share data with the main app.