capacitor-background-runner icon indicating copy to clipboard operation
capacitor-background-runner copied to clipboard

Accessing CapacitorKV from App

Open ChristianK43 opened this issue 1 year ago • 3 comments

Hey, I'm working on implementing a background task and need it to stop running when the app is in the foreground. Using an event dispatch when the app goes into the background doesn't work because there's no active runner when the appStateChange event triggers.

It seems to me that the simplest solution would be to use CapacitorKV to set the state during the appStateChange event to handle this. Is this possible? I couldn't find any information on this in the documentation. I've seen that #99 would allow to access the App-API, but it would be great to have a solution until it gets merged.

ChristianK43 avatar Aug 22 '24 09:08 ChristianK43

@ChristianK43 You can use @capacitor/preferences and configure it to use same xml file as background runner

// app
import {Preferences} from '@capacitor/preferences'

await Preferences.configure({
    group: 'com.example.background'
})
await Preferences.set({
    key: 'test',
    value: 'true',
})

// background
addEventListener('example', async (resolve, reject, args) => {
	console.log('kv test', CapacitorKV.get('test'))
})

yshalsager avatar Sep 02 '24 07:09 yshalsager

@yshalsager Is there a way to do it with @ionic/storage?

web-jw avatar Nov 19 '24 15:11 web-jw

@yshalsager Is there a way to do it with @ionic/storage?

No idea, never used.

yshalsager avatar Nov 19 '24 18:11 yshalsager