Frage

as you can probably remember from my previous question about bidirectional relations in Green DAO, I have a chat that consists of conversations and messages.
every message has a parent conversation, and the conversation has a list of messages.

when I try and add a new message, it works perfectly as long as I don't close the application.
This is the code I use for adding a message to an existing conversation:

I now have my code that does this:

Conversation conv = getConversation();
List<Message> list = conv.getMessageList();

Message msg = new Message();
msg.setConversationId(conv,getId());
MessageDao.insert(msg);


list.add(msg);

conv.update();
// ConversationDao.update(conv);

when I open the application the next time and call conv.getMessageList(), the result is an empty ArrayList.

what am I doing wrong?

EDIT:

I changed my code and now it looks like this:

Conversation conv= getCOnversation();

Message msg = new Message();
msg.setConversation(conv);
MessageDao.insert(msg);

conv.resetMessageList();

unfortunately, now more than ever (even on same run), still calling conv.getMessageList() returns an empty java.list.

EDIT 2:

here is code for the generator (this is not the real code, but only what is important to the question.

Entity message = schema.addEntity("Message");
message.addIdProperty().autoincrement();

Entity conversation =schema.addEntity("Conversation");
conversation.addIdProperty().autoincrement();

Property parentConversation = message.addLongProperty("parentConversation")
        .getProperty();
Property messages = conversation.addLongProperty("messages").getProperty();
message.addToOne(conversation, parentConversation);
conversation.addToMany(message, messages);

EDIT 3

for now, instead of calling conv.getMessageList(); I running this line and it work fine.

List<Message> messageList = MessageDao.queryBuilder()
    .where(Properties.parentConversation.eq(conv.getId())
    .list()

I suspect a major issue for me or greenrobot in the implementation of all this.

is this an issue of running the above code while Session.runInTx?

War es hilfreich?

Lösung

This is the problematic section:

Property parentConversation = message.addLongProperty("parent")
    .getProperty();
Property messages = conversation.addLongProperty("messages")
    .getProperty();

message.addToOne(conversation, parentConversation);
conversation.addToMany(message, messages);

For 1:N relations, you must use the same property for both your ToOne and ToMany relation. Try this and regenerate your code:

Property parentConversation = message.addLongProperty("parent")
    .getProperty();
message.addToOne(conversation, parentConversation);
conversation.addToMany(message, parentConversation);

Andere Tipps

conv.getMessageList returns just a java list. adding a message there will not save it to database. you need to save message to database via messageDao.

Just a guess: is your conversation persisted before calling msg.setConversation(conv)? Make sure by introducing a sanity check:

Conversation conv = getConversation();
if(conv.getId() == null) {
    throw new RuntimeException("Conversation not persisted: " + conv);
}
Message msg = new Message();
msg.setConversation(conv);
MessageDao.insert(msg); // Breakpoint here, is parentConversation set?
conv.resetMessageList();

If that's the issue, you must make sure that your conversation is persisted, e.g. by calling conversationDao.insertOrReplace(conv).

Once that works, you can also consider replacing conv.resetMessageList() with this line:

conv.getMessages().add(msg); // Keep same list, also more efficient

If that does not help, please debug it and check if msg has parentConversation set before it is inserted (see comment in the code).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top