AsyncInitableT

This is the asynchronous version of GInitable; it behaves the same in all ways except that initialization is asynchronous. For more details see the descriptions on GInitable

A class may implement both the GInitable and GAsyncInitable interfaces.

Users of objects implementing this are not intended to use the interface method directly; instead it will be used automatically in various ways. For C applications you generally just call g_async_initable_new_async() directly, or indirectly via a foo_thing_new_async() wrapper. This will call g_async_initable_init_async() under the cover, calling back with NULL and a set GError on failure.

A typical implementation might look something like this:

1 
2 enum {
3 NOT_INITIALIZED,
4 INITIALIZING,
5 INITIALIZED
6 };
7 
8 static void
9 _foo_ready_cb (Foo *self)
10 {
11 GList *l;
12 
13 self->priv->state = INITIALIZED;
14 
15 for (l = self->priv->init_results; l != NULL; l = l->next)
16 {
17 GTask *task = l->data;
18 
19 if (self->priv->success)
20 g_task_return_boolean (task, TRUE);
21 else
22 g_task_return_new_error (task, ...);
23 g_object_unref (task);
24 }
25 
26 g_list_free (self->priv->init_results);
27 self->priv->init_results = NULL;
28 }
29 
30 static void
31 foo_init_async (GAsyncInitable       *initable,
32 int                   io_priority,
33 GCancellable         *cancellable,
34 GAsyncReadyCallback   callback,
35 gpointer              user_data)
36 {
37 Foo *self = FOO (initable);
38 GTask *task;
39 
40 task = g_task_new (initable, cancellable, callback, user_data);
41 
42 switch (self->priv->state)
43 {
44 case NOT_INITIALIZED:
45 _foo_get_ready (self);
46 self->priv->init_results = g_list_append (self->priv->init_results,
47 task);
48 self->priv->state = INITIALIZING;
49 break;
50 case INITIALIZING:
51 self->priv->init_results = g_list_append (self->priv->init_results,
52 task);
53 break;
54 case INITIALIZED:
55 if (!self->priv->success)
56 g_task_return_new_error (task, ...);
57 else
58 g_task_return_boolean (task, TRUE);
59 g_object_unref (task);
60 break;
61 }
62 }
63 
64 static gboolean
65 foo_init_finish (GAsyncInitable       *initable,
66 GAsyncResult         *result,
67 GError              **error)
68 {
69 g_return_val_if_fail (g_task_is_valid (result, initable), FALSE);
70 
71 return g_task_propagate_boolean (G_TASK (result), error);
72 }
73 
74 static void
75 foo_async_initable_iface_init (gpointer g_iface,
76 gpointer data)
77 {
78 GAsyncInitableIface *iface = g_iface;
79 
80 iface->init_async = foo_init_async;
81 iface->init_finish = foo_init_finish;
82 }

Members

Functions

getAsyncInitableStruct
GAsyncInitable* getAsyncInitableStruct(bool transferOwnership)

Get the main Gtk struct

initAsync
void initAsync(int ioPriority, Cancellable cancellable, GAsyncReadyCallback callback, void* userData)

Starts asynchronous initialization of the object implementing the interface. This must be done before any real use of the object after initial construction. If the object also implements GInitable you can optionally call g_initable_init() instead.

initFinish
bool initFinish(AsyncResultIF res)

Finishes asynchronous initialization and returns the result. See g_async_initable_init_async().

newFinish
ObjectG newFinish(AsyncResultIF res)

Finishes the async construction for the various g_async_initable_new calls, returning the created object or NULL on error.

Meta

Since

2.22