Fix retain cycles
This commit is contained in:
@@ -5,8 +5,8 @@ public class MessageListModel : Object, ListModel
|
||||
{
|
||||
public signal void messages_changed();
|
||||
|
||||
public SortedSet<Message> messages {
|
||||
owned get { return _messages.read_only_view; }
|
||||
public ArrayList<Message> messages {
|
||||
get { return _messages; }
|
||||
}
|
||||
|
||||
public bool is_group_chat {
|
||||
@@ -17,19 +17,34 @@ public class MessageListModel : Object, ListModel
|
||||
|
||||
public string conversation_guid { get; private set; }
|
||||
|
||||
private SortedSet<Message> _messages;
|
||||
private ArrayList<Message> _messages;
|
||||
private HashSet<string> participants = new HashSet<string>();
|
||||
|
||||
public MessageListModel(string conversation_guid) {
|
||||
_messages = new TreeSet<Message>((a, b) => {
|
||||
// Sort by date in descending order (newest first)
|
||||
return a.date.compare(b.date);
|
||||
});
|
||||
private ulong handler_id = 0;
|
||||
|
||||
Repository.get_instance().messages_updated.connect(got_messages_updated);
|
||||
public MessageListModel(string conversation_guid) {
|
||||
_messages = new ArrayList<Message>();
|
||||
this.conversation_guid = conversation_guid;
|
||||
}
|
||||
|
||||
|
||||
~MessageListModel() {
|
||||
// NOTE: this won't actually get destructed automatically because of a retain cycle with the signal handler.
|
||||
// unwatch_updates() should be called explicitly when the model is no longer needed.
|
||||
unwatch_updates();
|
||||
}
|
||||
|
||||
public void watch_updates() {
|
||||
if (this.handler_id == 0) {
|
||||
this.handler_id = Repository.get_instance().messages_updated.connect(got_messages_updated);
|
||||
}
|
||||
}
|
||||
|
||||
public void unwatch_updates() {
|
||||
if (this.handler_id != 0) {
|
||||
Repository.get_instance().disconnect(this.handler_id);
|
||||
this.handler_id = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public void load_messages() {
|
||||
try {
|
||||
Message[] messages = Repository.get_instance().get_messages(conversation_guid);
|
||||
@@ -82,6 +97,6 @@ public class MessageListModel : Object, ListModel
|
||||
}
|
||||
|
||||
public Object? get_item(uint position) {
|
||||
return _messages.to_array()[position];
|
||||
return _messages.get((int)position);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user