简体   繁体   English

如何了解Realm在Android上的行为?

[英]How to understand Realm's behavior on android?

I try to go deeper in the Realm, I'm using it last few days in our project and can't understand some strange behavior as for me, in the documentation I doesn't find any useful information, I looked in their samples apps but still not enough information, so can anyone explain me. 我尝试更深入地了解Realm,我在项目中使用了几天,对我来说还无法理解一些奇怪的行为,在文档中我找不到任何有用的信息,而是在他们的示例应用中查看过但仍然没有足够的信息,所以任何人都可以向我解释。 Why model returned by Realm are empty, in browser it shows correct data, but not in debug mode. 为什么Realm返回的模型为空,在浏览器中它显示正确的数据,而不是在调试模式下。 领域调试

According to the sample "Intro example", here is snippet: 根据样本“简介示例”,以下是代码段:

private void basicCRUD(Realm realm) {
    showStatus("Perform basic Create/Read/Update/Delete (CRUD) operations...");

    // All writes must be wrapped in a transaction to facilitate safe multi threading
    realm.beginTransaction();

    // Add a person
    Person person = realm.createObject(Person.class);
    person.setId(1);
    person.setName("Young Person");
    person.setAge(14);

    // When the transaction is committed, all changes a synced to disk.
    realm.commitTransaction();

    // Find the first person (no query conditions) and read a field
    person = realm.where(Person.class).findFirst();
    showStatus(person.getName() + ":" + person.getAge());

    // Update person in a transaction
    realm.beginTransaction();
    person.setName("Senior Person");
    person.setAge(99);
    showStatus(person.getName() + " got older: " + person.getAge());
    realm.commitTransaction();

    // Delete all persons
    realm.beginTransaction();
    realm.allObjects(Person.class).clear();
    realm.commitTransaction();
}

I spent all day. 我整天都在度过。 My main problem next, I have one activity A within one fragment with adapter(lets says that has model of the rabbit and displays his 4 carrots), after click "+" on the fragment I replace with a new fragment where I create 3 carrots, then after popBackStack() I wont add this 3 carrots to the previous 4. 接下来的主要问题是,我在一个带适配器的片段中有一个活动A(让我们说它具有兔子的模型并显示他的4个胡萝卜),在片段上单击“ +”后,我将替换为一个新片段,在其中创建3个胡萝卜,然后在popBackStack()之后,我不会将这3个胡萝卜添加到前面的4个中。

How I do it: Activity (has instance of Rabbit which has 4 carrots, saved in Realm)--> Fragment A (4 carrots) --> Fragment B ( get instance from Realm, carrots in empty, why? I wont add 3 carrot and save to Realm my rabbit)--> popBackStack --> OnCreateView Framgent A (get Rabbit from Realm, adapter try to display data but carrots is empty. Go home). 我该怎么做: 活动 (有一个Rabbit实例,其中有4个胡萝卜,保存在Realm中)-> 片段A(4个胡萝卜) -> Fragment B从Realm中获取实例,胡萝卜为空,为什么?我不会添加3胡萝卜并保存到Realm我的兔子)-> popBackStack- > OnCreateView Framgent A (从Realm中获取Rabbit,适配器尝试显示数据,但胡萝卜为空。回家)。

Realm is a zero-copy database. 领域是零拷贝数据库。 This means that unlike ORM's that copy all their data to the Java heap before you can access it, we store all our data in native memory. 这意味着与ORM不一样,ORM会将所有数据复制到Java堆中,然后再访问它,我们将所有数据存储在本机内存中。 You can think of RealmObjects as type-safe cursor objects. 您可以将RealmObjects视为类型安全的游标对象。 This also means that the IntelliJ debugger will not show the correct values, as it shows the values in the Java heap. 这也意味着IntelliJ调试器将不会显示正确的值,因为它会显示Java堆中的值。 We uses proxy classes to override the behaviour of your getters and setters (in your screen shoot: PersonRealmProxy), which is why using the getX() methods will return the correct values. 我们使用代理类覆盖您的getter和setter的行为(在屏幕截图中:PersonRealmProxy),这就是为什么使用getX()方法将返回正确值的原因。

Regarding the second question, then it would be easier to create an issue on GitHub so we can ask for more details: https://github.com/realm/realm-java/issues 关于第二个问题,那么在GitHub上创建一个问题会更容易,因此我们可以要求更多详细信息: https : //github.com/realm/realm-java/issues

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

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