Closed ulfgebhardt closed 3 years ago
Thought this might help. I've been also encountering this issue and trying file system based solutions such as react-native-fs-store and redux-persist-fs-storage were performing too slow.
the current solution i've come up with, was a custom wrapper around AsyncStorage that splits the large persisted object (key => 'apollo-cache-persist' by default)
this is the wrapper SplitAsyncStorage
keep in mind it is not thoroughly tested but it does seem to perform much better than the file system alternatives. you still may reach the 2MB limit in an extreme case that one of the inner values is large enough, tho the chances of it happening seems significantly lower.
Hi, just a tip here - try using react-native-mmkv-storage
- it doesn't have the 2MB limitation and from my testing it performs better than filesystem alternatives.
I've updated the readme in https://github.com/apollographql/apollo-cache-persist/pull/350.
@bureyburey I suppose MMKV could be more performant than parsing and serializing the cache again in the wrapper.
@wodCZ I've actually tried it as soon as i've seen the #350 but couldn't make it work (the api is somewhat different from AsyncStorage). I tried making a wrapper around it that uses the getMapAsync and setMapAsync methods but ended up having the app crashing on startup.
Would to have any pointers regarding proper integration of it :)
@wodCZ this sounds very interesting. I will see if we can land minimal react native sample application with the best storage. I think no one cares about storages itself - people want solution for the platforms. TBH.. I would be more keen to ditch async-storage because it is actually done as two different packages (expo and community) which is total mess. Having performant, reliable storage picked for platform is good, but do not feel strong enough to propose one without driving it to production.
@bureyburey Just seen your message. If we get some simple app we can figure out wrapper and even publish it as separate package that will provide great experience for the react native (as web seems to be working fine. Then we can even refine overal storage interface so we support this two platforms properly.
@wtrocki seems Expo adopted @react-native-community/async-storage
@bureyburey Yep. Despite that I still get people using old versions saying that things do not work :D
@wtrocki sounds great! :)
@bureyburey
The following works just fine for me - a simple drop-in replacement. I've noticed AsyncStorage had some API changes, maybe I'm bringing some confusion here since I'm not using latest versions especially of apollo.
"apollo-client": "^2.6.10",
"apollo-cache-inmemory": "^1.6.6",
"apollo-cache-persist": "^0.1.1",
"react-native-mmkv-storage": "^0.3.7",
import {InMemoryCache} from 'apollo-cache-inmemory';
import {CachePersistor} from 'apollo-cache-persist';
import MMKVStorage from 'react-native-mmkv-storage';
export const apolloCache = new InMemoryCache();
const MMKV = new MMKVStorage.Loader().withInstanceID('apolloCache').initialize();
export const cachePersistor = new CachePersistor({
trigger: 'background',
cache: apolloCache,
storage: MMKV,
maxSize: false,
debug: __DEV__,
});
@wtrocki We're stuck at Apollo v2 for foreseeable future. I will try to make a sample project with Apollo 3, but I'm a bit busy these days.
Notably I'd like to give a try to saving the cache as a native object/map (https://github.com/apollographql/apollo-cache-persist/pull/350#issuecomment-698304329), I believe skipping the need of JSON.stringify/parse would bring a nice performance boost, especially with large cache (we're currently at about 9mb JSON, Apollo cache can get pretty greedy 🙄).
@wodCZ many thanks! I will try this ASAP
EDIT Looking good 🤘 The docs does not include the getItem/setItem methods which made me try making a wrapper that failed miserably 😅
Working great now, many thanks 🍻
I think we have already sample APP PR from community member. I will look into adding things to it: https://github.com/apollographql/apollo-cache-persist/pull/357
It looks like this issue has been solved. Please let me know if it needs to be opened back up.
Using this Library with React-Native on Android following error can be thrown
Even tho the Readme recognizes this Error the given Solution is not working - the error occurs regardless of a
maxSize
.The correct Solution is to wrap the whole thing in a try-catch:
Therefore I advise to document this or adjust the interface of
persistor.restore()
to not throw.Ref: https://github.com/demokratie-live/democracy-client/blob/master/index.js#L213