Comments (11)
Okay, the latest for this is: If you use the storage group "NativeStorage", it will be backwards-compatible with cordova-plugin-nativestorage
. For web this means you can access the root level of localStorage, fulfilling the original feature request in this issue. We'd like devs to migrate their data from NativeStorage, because the clear()
of the NativeStorage plugin on web seems to be very destructive (but please correct me if I'm mistaken).
On iOS and web, the Capacitor Storage plugin uses the storage group (default of "CapacitorStorage") as a prefix to separate key/values from other data. On Android, it uses the group as an entirely separate SharedPreferences file.
Please take a look at this PR and let me know your thoughts! I'll update this issue when dev releases are made for Capacitor 3 and the new storage plugin.
from capacitor-plugins.
@dwieeb Thank you, I can't wait for Capacitor 3 👍
from capacitor-plugins.
@jcesarmobile In my opinion it should be possible to set a custom KEY_PREFIX
(obviously it can unofficially be done, by just setting KEY_PREFIX
now, but it's not nice...). This would not only allow to seamlessly solve situations like this, but also to have capacitor/plugin independent keys (without the prefix).
from capacitor-plugins.
@jcesarmobile @dwieeb would you please explain that? I don't quite understand why it's not possible to configure a prefix for the storage key (remove prefix)? If capacitor is a good replacement for the old cordova project, then most likely cordova-plugin-nativestorage was used in the old project, which has no default prefix. With capacitor you should use the core storage plugin, which has _cap_
default prefix. So if you need to migrate application from cordova to capacitor and data in local storage is important, then it is not possible with capacitor core plugin and you have to stay with cordova-plugin-nativestorage.
NativeStorage.setItem('key1', 'value1'); // key1=value1
const res = await Plugins.Storage.get({ key: 'key1' });
console.log(res.value); // <== undefined
from capacitor-plugins.
The plugin is a wrapper around localStorage, so if you have data already in localStorage you will need to migrate it by checking current data, add it as Capacitor data and remove you copy of localStorage, or keep using localStorage as you already do. The only problem with that is it won't use the native implementation on iOS and Android.
Maybe we can add a migrate function for web implementation that does it for you, what do you thing @mlynch?
from capacitor-plugins.
Changed the issue to be a feature request since it doesn't really break anything, it has worked like that since the beginning.
Issues tagged with feature request are closed but tracked for reactions to gauge interest
from capacitor-plugins.
This is indeed an unexpected behavior that I thought for sure would be configurable. I would be willing to create a PR for the feature if necessary.
My current workaround is to just wrap the Capacitor Storage with a util class that removes the prefix from the get
method. Also, support auto serialization like the old Cordova/Ionic Storage.
// app/utils/Storage.ts
import { Plugins } from '@capacitor/core';
const { Storage: CapStorage } = Plugins;
export default class Storage {
static get(key): Promise<any> {
return CapStorage.get({ key: key.replace('_cap_', '') }).then(item => {
const value = item.value;
return typeof value === 'object' ? JSON.parse(value) : value;
});
}
static set(key, value): Promise<any> {
const setValue = typeof value === 'object' ? JSON.stringify(value) : value;
return CapStorage.set({ key, value: setValue });
}
static remove(key): Promise<any> {
return CapStorage.remove({ key });
}
}
Usage:
import Storage from 'app/utils';
NativeStorage.setItem('somekey', 'somevalue');
// or
Storage.set('somekey', 'somevalue');
// would both be retrievable by
Storage.get('somekey');
But this just seems ridiculous to have to do.
from capacitor-plugins.
@yacut For example on iOS, NativeStorage uses a custom UserDefaults group called "NativeStorage"
, and we use the standard UserDefaults at the moment (with a key prefix). To me, it doesn't make sense to share their group, but I could be convinced otherwise. Any changes to this behavior would result in a breaking change for apps unless we add in migration logic. I would like to exclude the ability to extract values from NativeStorage from the scope of this issue and instead focus on the original request: the ability to change the key prefix. This is a completely reasonable request and will be addressed in Capacitor 3. If you want, you can create a separate feature request for extracting values from the NativeStorage plugin.
from capacitor-plugins.
And I just found out the Android implementation doesn't even use the prefix to begin with, so this will be very difficult to implement in a cross-platform way. We may need to provide a way to migrate data.
from capacitor-plugins.
I am going to rewrite the iOS implementation to use a non-shared UserDefaults and rewrite the web implementation to use IndexedDB. This will remove the need for a prefix, and instead promote the idea of a "storage group". There will be a way to migrate data from the Capacitor 2 implementation.
from capacitor-plugins.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.
from capacitor-plugins.
Related Issues (20)
- Please provide marker rotation in google maps plugin HOT 2
- [splash-screen] Make docs usable for iOS app HOT 2
- Cannot select a printer from the Share dialogue box HOT 1
- bug(@capacitor/share): FATAL EXCEPTION java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tvoydnevnik/com.tvoydnevnik.MainActivity}: java.lang.SecurityException: com.tvoydnevnik: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts HOT 2
- Add Capacitor Webview HOT 2
- Add map events to be handled at client side HOT 2
- Complience with Apples Required Reason API HOT 18
- google.maps.Marker is deprecated HOT 1
- [Geolocation / Android] The GPS position is only updated every 5 seconds when using "watchPosition" function on Android HOT 1
- [Bug]: @capacitor/camera - Console (F12) error (undefined) clicking on the camera icon when still loading the camera or not alowing the permission yet. HOT 3
- [Feature] Deep linking for Chromebook Android (App appUrlOpen)
- @capacitor/share not work in android 14, capacitor 6 HOT 4
- [Bug]: @capacitor/camera - After taking a photo (web), a certain border around the photo is cut away. HOT 4
- [Bug]: @capacitor/camera - After turning the device from landscape to portrait, the wrong modes are used when taking a photo. HOT 3
- feat(@capacitor/device) Request to update device plugin to remove privacy-impacting APIs HOT 1
- @capacitor/browser add a new BROWSER_CLOSED event HOT 1
- [@capacitor/filesystem] Successful Filesystem.requestPermissions prompt never resolves due to missing case in callback HOT 5
- [Feature]: native camera max weight settings
- @capacitor/share not working with capacitor v6 HOT 2
- Filesystem.copy not working con content:/ android
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from capacitor-plugins.