简体   繁体   English

如何在 Fragment 中启用解析本地数据存储

[英]How to enable Parse Local datastore in Fragment

can anyone please help me?谁能帮帮我吗? I am trying to enable Parse Local Datastore in a fragment but its throwing out an error: java.lang.IllegalStateException: Parse#enableLocalDatastore(Context) must be invoked before Parse#initialize(Context, String, String) .我试图在片段中启用 Parse Local Datastore 但它抛出一个错误: java.lang.IllegalStateException: Parse#enableLocalDatastore(Context) must be Parse#enableLocalDatastore(Context) before Parse#initialize(Context, String, String) Below is the code I use which works perfectly fine in an Activity but not in Fragment and I know the getActivity context is not null since the second line works.下面是我使用的代码,它在 Activity 中工作得很好,但在 Fragment 中却没有,我知道 getActivity 上下文不为空,因为第二行有效。

Parse.enableLocalDatastore(getActivity());
Parse.initialize(getActivity(), DeveloperKey.ParseAppID, DeveloperKey.ParseClientKey);

you have to move your code related to Parse initialization to the Application class.您必须将与 Parse 初始化相关的代码移动到 Application 类。 Make class extending Application, add it to manifest and then in the onCreate() of you application call使类扩展应用程序,将其添加到清单,然后在应用程序调用的 onCreate() 中

Parse.enableLocalDatastore(getActivity());
Parse.initialize(getActivity(), DeveloperKey.ParseAppID, DeveloperKey.ParseClientKey);

Reading your logcat error you just need to interchange your code.阅读您的 logcat 错误,您只需要交换您的代码。

 Parse.initialize(getActivity(), DeveloperKey.ParseAppID, DeveloperKey.ParseClientKey);
 Parse.enableLocalDatastore(getActivity());

When your parse is enables after that you'l be able to enable your local database for it.当您启用解析之后,您就可以为其启用本地数据库。

I realised using pin() automatically saves to the local datastore so there was no need to explicitly use enableLocaldatastore() .我意识到使用pin()自动保存到本地数据存储,因此无需显式使用enableLocaldatastore() With this, I can query from localdatastore and get the cached result.有了这个,我可以从localdatastore查询并获得缓存的结果。

Parse.enableLocalDatastore(getActivity());

is an old way of enabling local datastore.是启用本地数据存储的旧方法。

Now you have to do like this.现在你必须这样做。

Parse.initialize(new Parse.Configuration.Builder(this)
                .applicationId(getResources().getString(R.string.parse_api_key))
                .clientKey(null)
                .server("YOUR_SERVER_URL/") // The trailing slash is important.
        .enableLocalDataStore().build()
        );

Try upgrading:尝试升级:

implementation "com.github.parse-community.Parse-SDK-Android:parse:x.xx.x" 

To a newer version.到较新的版本。 Like this:像这样:

How android ide did help me out: android ide 如何帮助我:
android ide 如何帮助我解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM