Class SimpleAsyncResult
- All Implemented Interfaces:
Proxy
,AsyncResult
GSimpleAsyncResult
is deprecated in favor of
Task
, which provides a simpler API.
GSimpleAsyncResult
implements AsyncResult
.
GSimpleAsyncResult
handles Gio.AsyncReadyCallback
s, error
reporting, operation cancellation and the final state of an operation,
completely transparent to the application. Results can be returned
as a pointer e.g. for functions that return data that is collected
asynchronously, a boolean value for checking the success or failure
of an operation, or a gssize
for operations which return the number
of bytes modified by the operation; all of the simple return cases
are covered.
Most of the time, an application will not need to know of the details
of this API; it is handled transparently, and any necessary operations
are handled by AsyncResult
’s interface. However, if implementing
a new GIO module, for writing language bindings, or for complex
applications that need better control of how asynchronous operations
are completed, it is important to understand this functionality.
GSimpleAsyncResult
s are tagged with the calling function to ensure
that asynchronous functions and their finishing functions are used
together correctly.
To create a new GSimpleAsyncResult
, call SimpleAsyncResult(org.gnome.gobject.GObject, org.gnome.gio.AsyncReadyCallback, java.lang.foreign.MemorySegment)
.
If the result needs to be created for a GError
, use
fromError(org.gnome.gobject.GObject, org.gnome.gio.AsyncReadyCallback, org.gnome.glib.GError)
or
takeError(org.gnome.gobject.GObject, org.gnome.gio.AsyncReadyCallback, org.gnome.glib.GError)
. If a GError
is not available
(e.g. the asynchronous operation doesn’t take a GError
argument),
but the result still needs to be created for an error condition, use
error(org.gnome.gobject.GObject, org.gnome.gio.AsyncReadyCallback, org.gnome.glib.Quark, int, java.lang.String, java.lang.Object...)
(or
SimpleAsyncResult#setErrorVa
if your application or binding
requires passing a variable argument list directly), and the error can then
be propagated through the use of
propagateError()
.
An asynchronous operation can be made to ignore a cancellation event by
calling setHandleCancellation(boolean)
with a
GSimpleAsyncResult
for the operation and FALSE
. This is useful for
operations that are dangerous to cancel, such as close (which would
cause a leak if cancelled before being run).
GSimpleAsyncResult
can integrate into GLib’s event loop,
GLib.MainLoop
, or it can use GLib.Thread
s.
complete()
will finish an I/O task directly
from the point where it is called.
completeInIdle()
will finish it from an idle
handler in the thread-default main context (see
MainContext.pushThreadDefault()
) where the GSimpleAsyncResult
was created. runInThread(org.gnome.gio.SimpleAsyncThreadFunc, int, org.gnome.gio.Cancellable)
will run the job in
a separate thread and then use
completeInIdle()
to deliver the result.
To set the results of an asynchronous function,
setOpResGpointer(java.lang.foreign.MemorySegment)
,
setOpResGboolean(boolean)
, and
setOpResGssize(long)
are provided, setting the operation's result to a gpointer
, gboolean
, or
gssize
, respectively.
Likewise, to get the result of an asynchronous function,
getOpResGpointer()
,
getOpResGboolean()
, and
getOpResGssize()
are
provided, getting the operation’s result as a gpointer
, gboolean
, and
gssize
, respectively.
For the details of the requirements implementations must respect, see
AsyncResult
. A typical implementation of an asynchronous
operation using GSimpleAsyncResult
looks something like this:
static void
baked_cb (Cake *cake,
gpointer user_data)
{
// In this example, this callback is not given a reference to the cake,
// so the GSimpleAsyncResult has to take a reference to it.
GSimpleAsyncResult *result = user_data;
if (cake == NULL)
g_simple_async_result_set_error (result,
BAKER_ERRORS,
BAKER_ERROR_NO_FLOUR,
"Go to the supermarket");
else
g_simple_async_result_set_op_res_gpointer (result,
g_object_ref (cake),
g_object_unref);
// In this example, we assume that baked_cb is called as a callback from
// the mainloop, so it's safe to complete the operation synchronously here.
// If, however, _baker_prepare_cake () might call its callback without
// first returning to the mainloop — inadvisable, but some APIs do so —
// we would need to use g_simple_async_result_complete_in_idle().
g_simple_async_result_complete (result);
g_object_unref (result);
}
void
baker_bake_cake_async (Baker *self,
guint radius,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *simple;
Cake *cake;
if (radius < 3)
{
g_simple_async_report_error_in_idle (G_OBJECT (self),
callback,
user_data,
BAKER_ERRORS,
BAKER_ERROR_TOO_SMALL,
"%ucm radius cakes are silly",
radius);
return;
}
simple = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
baker_bake_cake_async);
cake = _baker_get_cached_cake (self, radius);
if (cake != NULL)
{
g_simple_async_result_set_op_res_gpointer (simple,
g_object_ref (cake),
g_object_unref);
g_simple_async_result_complete_in_idle (simple);
g_object_unref (simple);
// Drop the reference returned by _baker_get_cached_cake();
// the GSimpleAsyncResult has taken its own reference.
g_object_unref (cake);
return;
}
_baker_prepare_cake (self, radius, baked_cb, simple);
}
Cake *
baker_bake_cake_finish (Baker *self,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *simple;
Cake *cake;
g_return_val_if_fail (g_simple_async_result_is_valid (result,
G_OBJECT (self),
baker_bake_cake_async),
NULL);
simple = (GSimpleAsyncResult *) result;
if (g_simple_async_result_propagate_error (simple, error))
return NULL;
cake = CAKE (g_simple_async_result_get_op_res_gpointer (simple));
return g_object_ref (cake);
}
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
SimpleAsyncResult.Builder<B extends SimpleAsyncResult.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static class
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
Nested classes/interfaces inherited from interface org.gnome.gio.AsyncResult
AsyncResult.AsyncResultIface, AsyncResult.AsyncResultImpl
-
Constructor Summary
ConstructorDescriptionSimpleAsyncResult
(MemorySegment address) Create a SimpleAsyncResult proxy instance for the provided memory address.SimpleAsyncResult
(@Nullable GObject sourceObject, @Nullable AsyncReadyCallback callback, @Nullable MemorySegment sourceTag) Deprecated.Use g_task_new() instead. -
Method Summary
Modifier and TypeMethodDescriptionprotected SimpleAsyncResult
asParent()
Returns this instance as if it were its parent type.static SimpleAsyncResult.Builder
<? extends SimpleAsyncResult.Builder> builder()
ASimpleAsyncResult.Builder
object constructs aSimpleAsyncResult
with the specified properties.void
complete()
Deprecated.UseGTask
instead.void
Deprecated.UseGTask
instead.static SimpleAsyncResult
error
(@Nullable GObject sourceObject, @Nullable AsyncReadyCallback callback, Quark domain, int code, String format, Object... varargs) Deprecated.Use g_task_new() and g_task_return_new_error() instead.static SimpleAsyncResult
fromError
(@Nullable GObject sourceObject, @Nullable AsyncReadyCallback callback, GError error) Deprecated.Use g_task_new() and g_task_return_error() instead.boolean
Deprecated.UseGTask
and g_task_propagate_boolean() instead.Deprecated.UseGTask
and g_task_propagate_pointer() instead.long
Deprecated.UseGTask
and g_task_propagate_int() instead.Deprecated.UseGTask
and g_task_get_source_tag() instead.static Type
getType()
Get the GType of the SimpleAsyncResult classstatic boolean
isValid
(AsyncResult result, @Nullable GObject source, @Nullable MemorySegment sourceTag) Deprecated.UseGTask
and g_task_is_valid() instead.boolean
Deprecated.UseGTask
instead.void
runInThread
(SimpleAsyncThreadFunc func, int ioPriority, @Nullable Cancellable cancellable) Deprecated.UseGTask
and g_task_run_in_thread() instead.void
setCheckCancellable
(@Nullable Cancellable checkCancellable) Deprecated.UseGTask
instead.void
Deprecated.UseGTask
and g_task_return_new_error() instead.void
setFromError
(GError error) Deprecated.UseGTask
and g_task_return_error() instead.void
setHandleCancellation
(boolean handleCancellation) Deprecated.void
setOpResGboolean
(boolean opRes) Deprecated.UseGTask
and g_task_return_boolean() instead.void
setOpResGpointer
(@Nullable MemorySegment opRes) Deprecated.UseGTask
and g_task_return_pointer() instead.void
setOpResGssize
(long opRes) Deprecated.UseGTask
and g_task_return_int() instead.void
Deprecated.UseGTask
and g_task_return_error() instead.static SimpleAsyncResult
takeError
(@Nullable GObject sourceObject, @Nullable AsyncReadyCallback callback, GError error) Deprecated.Use g_task_new() and g_task_return_error() instead.Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getMemoryLayout, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.gnome.gio.AsyncResult
getSourceObject, getUserData, isTagged, legacyPropagateError
-
Constructor Details
-
SimpleAsyncResult
Create a SimpleAsyncResult proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
SimpleAsyncResult
@Deprecated public SimpleAsyncResult(@Nullable @Nullable GObject sourceObject, @Nullable @Nullable AsyncReadyCallback callback, @Nullable @Nullable MemorySegment sourceTag) Deprecated.Use g_task_new() instead.Creates aGSimpleAsyncResult
.The common convention is to create the
GSimpleAsyncResult
in the function that starts the asynchronous operation and use that same function as thesourceTag
.If your operation supports cancellation with
GCancellable
(which it probably should) then you should provide the user's cancellable to g_simple_async_result_set_check_cancellable() immediately after this function returns.- Parameters:
sourceObject
- aGObject
, ornull
.callback
- aGAsyncReadyCallback
.sourceTag
- the asynchronous function.
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type. -
error
@Deprecated public static SimpleAsyncResult error(@Nullable @Nullable GObject sourceObject, @Nullable @Nullable AsyncReadyCallback callback, Quark domain, int code, String format, Object... varargs) Deprecated.Use g_task_new() and g_task_return_new_error() instead.Creates a newGSimpleAsyncResult
with a set error.- Parameters:
sourceObject
- aGObject
, ornull
.callback
- aGAsyncReadyCallback
.domain
- aGQuark
.code
- an error code.format
- a string with format characters.varargs
- a list of values to insert intoformat
.- Returns:
- a
GSimpleAsyncResult
.
-
fromError
@Deprecated public static SimpleAsyncResult fromError(@Nullable @Nullable GObject sourceObject, @Nullable @Nullable AsyncReadyCallback callback, GError error) Deprecated.Use g_task_new() and g_task_return_error() instead.Creates aGSimpleAsyncResult
from an error condition.- Parameters:
sourceObject
- aGObject
, ornull
.callback
- aGAsyncReadyCallback
.error
- aGError
- Returns:
- a
GSimpleAsyncResult
.
-
takeError
@Deprecated public static SimpleAsyncResult takeError(@Nullable @Nullable GObject sourceObject, @Nullable @Nullable AsyncReadyCallback callback, GError error) Deprecated.Use g_task_new() and g_task_return_error() instead.Creates aGSimpleAsyncResult
from an error condition, and takes over the caller's ownership oferror
, so the caller does not need to free it anymore.- Parameters:
sourceObject
- aGObject
, ornull
callback
- aGAsyncReadyCallback
.error
- aGError
- Returns:
- a
GSimpleAsyncResult
-
isValid
@Deprecated public static boolean isValid(AsyncResult result, @Nullable @Nullable GObject source, @Nullable @Nullable MemorySegment sourceTag) Deprecated.UseGTask
and g_task_is_valid() instead.Ensures that the data passed to the _finish function of an async operation is consistent. Three checks are performed.First,
result
is checked to ensure that it is really aGSimpleAsyncResult
. Second,source
is checked to ensure that it matches the source object ofresult
. Third,sourceTag
is checked to ensure that it is equal to thesourceTag
argument given to g_simple_async_result_new() (which, by convention, is a pointer to the _async function corresponding to the _finish function from which this function is called). (Alternatively, if eithersourceTag
orresult
's source tag isnull
, then the source tag check is skipped.)- Parameters:
result
- theGAsyncResult
passed to the _finish function.source
- theGObject
passed to the _finish function.sourceTag
- the asynchronous function.- Returns:
TRUE
if all checks passed orFALSE
if any failed.
-
complete
Deprecated.UseGTask
instead.Completes an asynchronous I/O job immediately. Must be called in the thread where the asynchronous result was to be delivered, as it invokes the callback directly. If you are in a different thread use g_simple_async_result_complete_in_idle().Calling this function takes a reference to this SimpleAsyncResult for as long as is needed to complete the call.
-
completeInIdle
Deprecated.UseGTask
instead.Completes an asynchronous function in an idle handler in the [thread-default main context][g-main-context-push-thread-default] of the thread that this SimpleAsyncResult was initially created in (and re-pushes that context around the invocation of the callback).Calling this function takes a reference to this SimpleAsyncResult for as long as is needed to complete the call.
-
getOpResGboolean
Deprecated.UseGTask
and g_task_propagate_boolean() instead.Gets the operation result boolean from within the asynchronous result.- Returns:
true
if the operation's result wastrue
,false
if the operation's result wasfalse
.
-
getOpResGpointer
Deprecated.UseGTask
and g_task_propagate_pointer() instead.Gets a pointer result as returned by the asynchronous function.- Returns:
- a pointer from the result.
-
getOpResGssize
Deprecated.UseGTask
and g_task_propagate_int() instead.Gets a gssize from the asynchronous result.- Returns:
- a gssize returned from the asynchronous function.
-
getSourceTag
Deprecated.UseGTask
and g_task_get_source_tag() instead.Gets the source tag for theGSimpleAsyncResult
.- Returns:
- a
gpointer
to the source object for theGSimpleAsyncResult
.
-
propagateError
Deprecated.UseGTask
instead.Propagates an error from within the simple asynchronous result to a given destination.If the
GCancellable
given to a prior call to g_simple_async_result_set_check_cancellable() is cancelled then this function will returntrue
withdest
set appropriately.- Returns:
true
if the error was propagated todest
.false
otherwise.- Throws:
GErrorException
- seeGError
-
runInThread
@Deprecated public void runInThread(SimpleAsyncThreadFunc func, int ioPriority, @Nullable @Nullable Cancellable cancellable) Deprecated.UseGTask
and g_task_run_in_thread() instead.Runs the asynchronous job in a separate thread and then calls g_simple_async_result_complete_in_idle() on this SimpleAsyncResult to return the result to the appropriate main loop.Calling this function takes a reference to this SimpleAsyncResult for as long as is needed to run the job and report its completion.
- Parameters:
func
- aGSimpleAsyncThreadFunc
.ioPriority
- the io priority of the request.cancellable
- optionalGCancellable
object,null
to ignore.
-
setCheckCancellable
Deprecated.UseGTask
instead.Sets aGCancellable
to check before dispatching results.This function has one very specific purpose: the provided cancellable is checked at the time of g_simple_async_result_propagate_error() If it is cancelled, these functions will return an "Operation was cancelled" error (
IOErrorEnum.CANCELLED
).Implementors of cancellable asynchronous functions should use this in order to provide a guarantee to their callers that cancelling an async operation will reliably result in an error being returned for that operation (even if a positive result for the operation has already been sent as an idle to the main context to be dispatched).
The checking described above is done regardless of any call to the unrelated g_simple_async_result_set_handle_cancellation() function.
- Parameters:
checkCancellable
- aGCancellable
to check, ornull
to unset
-
setError
Deprecated.UseGTask
and g_task_return_new_error() instead.Sets an error within the asynchronous result without aGError
.- Parameters:
domain
- aGQuark
(usuallyG_IO_ERROR
).code
- an error code.format
- a formatted error reporting string.varargs
- a list of variables to fill informat
.
-
setFromError
Deprecated.UseGTask
and g_task_return_error() instead.Sets the result from aGError
.- Parameters:
error
-GError
.
-
setHandleCancellation
Deprecated.Sets whether to handle cancellation within the asynchronous operation.This function has nothing to do with g_simple_async_result_set_check_cancellable(). It only refers to the
GCancellable
passed to g_simple_async_result_run_in_thread().- Parameters:
handleCancellation
- agboolean
.
-
setOpResGboolean
Deprecated.UseGTask
and g_task_return_boolean() instead.Sets the operation result to a boolean within the asynchronous result.- Parameters:
opRes
- agboolean
.
-
setOpResGpointer
Deprecated.UseGTask
and g_task_return_pointer() instead.Sets the operation result within the asynchronous result to a pointer.- Parameters:
opRes
- a pointer result from an asynchronous function.
-
setOpResGssize
Deprecated.UseGTask
and g_task_return_int() instead.Sets the operation result within the asynchronous result to the givenopRes
.- Parameters:
opRes
- agssize
.
-
takeError
Deprecated.UseGTask
and g_task_return_error() instead.Sets the result fromerror
, and takes over the caller's ownership oferror
, so the caller does not need to free it any more.- Parameters:
error
- aGError
-
builder
ASimpleAsyncResult.Builder
object constructs aSimpleAsyncResult
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withSimpleAsyncResult.Builder.build()
.
-