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.

Lazy loading using the main loop.

9 Responses to “Lazy loading”


  1. There is a typo in cleanup_load_items in the g_assert. It should probably be == not =.

  2. mike Says:

    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?

  3. ebassi Says:

    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.


  4. 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(),

  5. mike Says:

    Duh… I was looking at gtk_idle_add_full.

  6. Matthias Liegend Says:

    I believe you meant to pass “data” rather than “store” for the third parameter of g_idle_add_full. Thanks for the example code!

  7. ebassi Says:

    nelson: thanks!

    matthias: yep, fixed that :-)


  8. [...] 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. [...]


Comments are closed.