Lazy loading
March 30, 2006
Little post about a useful design pattern in GLib and GTK, written down after a question on the #gtk+ channel
While I’m not Philip and I won’t go as far as talking about this in public, I wrote down a simple design pattern for lazy loading stuff from a list into a GtkTreeView without blocking your interface or using threads.


March 30, 2006 at 8:15 pm
There is a typo in cleanup_load_items in the g_assert. It should probably be == not =.
March 30, 2006 at 9:11 pm
What do you do if you need to use your GtkTreeView before it’s completely loaded? How would you force the load?
Also, is the state definition really necessary, since the state transitions are effectively managed internally by the idle function manager?
Nitpick: doesn’t g_idle_add_full actually take 5 parameters?
March 30, 2006 at 9:27 pm
aleksey: fixed, thanks
mike: I’d use the same pattern shown by philip van hoof: proxy objects and loading only stuff that is effectively shown. the state definition is just to make clear we are using a state machine; it’s also useful for checking the internal state, and for avoiding multiple reloads. as for the g_idle_add_full(): no, it takes 4 parameters: the priority, the callback, the data to pass to the callback and the function to be called when the idle is removed.
March 31, 2006 at 11:56 am
Re: Lazy loading…
Emmanuel: if you are using a language like Python, you can let the language keep track of your state machine for something like that:
def load_items(treeview, liststore, items):
for obj in items:
liststore.append((obj.get_foo(),
…
March 31, 2006 at 2:30 pm
I’ve put this on http://live.gnome.org/GnomeRecipes
March 31, 2006 at 3:03 pm
Duh… I was looking at gtk_idle_add_full.
March 31, 2006 at 9:19 pm
I believe you meant to pass “data” rather than “store” for the third parameter of g_idle_add_full. Thanks for the example code!
March 31, 2006 at 10:07 pm
nelson: thanks!
matthias: yep, fixed that
April 3, 2006 at 9:52 am
[...] A few days ago Emmanuele blogged about “Lazy loading”. He illustrated how to make it possible to offload the loading of many items in for example a liststore. Yet not having to use a worker-thread. [...]