Android Realm initialization in project

倖福魔咒の 提交于 2019-11-30 01:38:34

问题


As seen in the official documentation on how to use Realm

// Initialize Realm
Realm.init(context);

// Get a Realm instance for this thread
Realm realm = Realm.getDefaultInstance();

I added dependencie to my project

classpath "io.realm:realm-gradle-plugin:2.0.2"

I can use this library normally but static method init apparently does not exist. Can someone post an example of how to initialize and save example object to database using this library? There's really not too many tutorials and the usage looks really easy after you manage to fire it up. Realm initialization sets up default configuration right? So is there a way to bypass that static init and set it manually?

--EDIT

When I'm trying to execute this code

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();

I get

Error:(33, 49) error: Builder(Context) is not public in Builder; cannot be accessed from outside package


回答1:


This constructor no longer exists:

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();

Use this instead:

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();

The example you refer to should have been updated as well?




回答2:


For me the actual problem was that Android studio couldn't update the library from some old version I initially connected to the project. Gradle had good version but actual libs files were old, solution for me was to manually re-download the files of this library.



来源:https://stackoverflow.com/questions/39972224/android-realm-initialization-in-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!