Class SimpleAsyncResult
- All Implemented Interfaces:
AsyncResult,Proxy
GSimpleAsyncResult is deprecated in favor of
Task, which provides a simpler API.
GSimpleAsyncResult implements AsyncResult.
GSimpleAsyncResult handles Gio.AsyncReadyCallbacks, 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.
GSimpleAsyncResults 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().
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.Threads.
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
Nested ClassesModifier and TypeClassDescriptionstatic classSimpleAsyncResult.Builder<B extends SimpleAsyncResult.Builder<B>>Inner class implementing a builder pattern to construct a GObject with properties.static classNested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClassNested classes/interfaces inherited from interface org.gnome.gio.AsyncResult
AsyncResult.AsyncResult$Impl, AsyncResult.AsyncResultIface -
Field Summary
Fields inherited from class org.javagi.base.ProxyInstance
address -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new SimpleAsyncResult.SimpleAsyncResult(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 SimpleAsyncResultasParent()Returns this instance as if it were its parent type.static SimpleAsyncResult.Builder<? extends SimpleAsyncResult.Builder> builder()ASimpleAsyncResult.Builderobject constructs aSimpleAsyncResultwith the specified properties.voidcomplete()Deprecated.UseGTaskinstead.voidDeprecated.UseGTaskinstead.static SimpleAsyncResulterror(@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 SimpleAsyncResultfromError(@Nullable GObject sourceObject, @Nullable AsyncReadyCallback callback, GError error) Deprecated.Use g_task_new() and g_task_return_error() instead.booleanDeprecated.UseGTaskand g_task_propagate_boolean() instead.Deprecated.UseGTaskand g_task_propagate_pointer() instead.longDeprecated.UseGTaskand g_task_propagate_int() instead.Deprecated.UseGTaskand g_task_get_source_tag() instead.static TypegetType()Get the GType of the SimpleAsyncResult classstatic booleanisValid(AsyncResult result, @Nullable GObject source, @Nullable MemorySegment sourceTag) Deprecated.UseGTaskand g_task_is_valid() instead.booleanDeprecated.UseGTaskinstead.voidrunInThread(SimpleAsyncThreadFunc func, int ioPriority, @Nullable Cancellable cancellable) Deprecated.UseGTaskand g_task_run_in_thread() instead.voidsetCheckCancellable(@Nullable Cancellable checkCancellable) Deprecated.UseGTaskinstead.voidDeprecated.UseGTaskand g_task_return_new_error() instead.voidsetFromError(GError error) Deprecated.UseGTaskand g_task_return_error() instead.voidsetHandleCancellation(boolean handleCancellation) Deprecated.voidsetOpResGboolean(boolean opRes) Deprecated.UseGTaskand g_task_return_boolean() instead.voidsetOpResGpointer(@Nullable MemorySegment opRes) Deprecated.UseGTaskand g_task_return_pointer() instead.voidsetOpResGssize(long opRes) Deprecated.UseGTaskand g_task_return_int() instead.voidDeprecated.UseGTaskand g_task_return_error() instead.static SimpleAsyncResulttakeError(@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, 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, withPropertiesMethods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClassMethods inherited from class org.javagi.base.ProxyInstance
equals, handle, hashCodeMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods 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
GSimpleAsyncResultin 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.
-
SimpleAsyncResult
public SimpleAsyncResult()Creates a new SimpleAsyncResult.
-
-
Method Details
-
getType
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuperkeyword, 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 newGSimpleAsyncResultwith 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 aGSimpleAsyncResultfrom 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 aGSimpleAsyncResultfrom an error condition, and takes over the caller's ownership oferror, so the caller does not need to free it anymore.- Parameters:
sourceObject- aGObject, ornullcallback- aGAsyncReadyCallback.error- aGError- Returns:
- a
GSimpleAsyncResult - Since:
- 2.28
-
isValid
@Deprecated public static boolean isValid(AsyncResult result, @Nullable @Nullable GObject source, @Nullable @Nullable MemorySegment sourceTag) Deprecated.UseGTaskand 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,
resultis checked to ensure that it is really aGSimpleAsyncResult. Second,sourceis checked to ensure that it matches the source object ofresult. Third,sourceTagis checked to ensure that it is equal to thesourceTagargument 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 eithersourceTagorresult's source tag isnull, then the source tag check is skipped.)- Parameters:
result- theGAsyncResultpassed to the _finish function.source- theGObjectpassed to the _finish function.sourceTag- the asynchronous function.- Returns:
TRUEif all checks passed orFALSEif any failed.- Since:
- 2.20
-
complete
Deprecated.UseGTaskinstead.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.UseGTaskinstead.Completes an asynchronous function in an idle handler in the thread-default main context (seeMainContext.pushThreadDefault()) 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.UseGTaskand g_task_propagate_boolean() instead.Gets the operation result boolean from within the asynchronous result.- Returns:
trueif the operation's result wastrue,falseif the operation's result wasfalse.
-
getOpResGpointer
Deprecated.UseGTaskand g_task_propagate_pointer() instead.Gets a pointer result as returned by the asynchronous function.- Returns:
- a pointer from the result.
-
getOpResGssize
Deprecated.UseGTaskand g_task_propagate_int() instead.Gets a gssize from the asynchronous result.- Returns:
- a gssize returned from the asynchronous function.
-
getSourceTag
Deprecated.UseGTaskand g_task_get_source_tag() instead.Gets the source tag for theGSimpleAsyncResult.- Returns:
- a
gpointerto the source object for theGSimpleAsyncResult.
-
propagateError
Deprecated.UseGTaskinstead.Propagates an error from within the simple asynchronous result to a given destination.If the
GCancellablegiven to a prior call to g_simple_async_result_set_check_cancellable() is cancelled then this function will returntruewithdestset appropriately.- Returns:
trueif the error was propagated todest.falseotherwise.- Throws:
GErrorException- seeGError
-
runInThread
@Deprecated public void runInThread(SimpleAsyncThreadFunc func, int ioPriority, @Nullable @Nullable Cancellable cancellable) Deprecated.UseGTaskand 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- optionalGCancellableobject,nullto ignore.
-
setCheckCancellable
Deprecated.UseGTaskinstead.Sets aGCancellableto 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- aGCancellableto check, ornullto unset- Since:
- 2.32
-
setError
Deprecated.UseGTaskand 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.UseGTaskand 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
GCancellablepassed to g_simple_async_result_run_in_thread().- Parameters:
handleCancellation- agboolean.
-
setOpResGboolean
Deprecated.UseGTaskand g_task_return_boolean() instead.Sets the operation result to a boolean within the asynchronous result.- Parameters:
opRes- agboolean.
-
setOpResGpointer
Deprecated.UseGTaskand 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.UseGTaskand g_task_return_int() instead.Sets the operation result within the asynchronous result to the givenopRes.- Parameters:
opRes- agssize.
-
takeError
Deprecated.UseGTaskand 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- Since:
- 2.28
-
builder
ASimpleAsyncResult.Builderobject constructs aSimpleAsyncResultwith the specified properties. Use the variousset...()methods to set properties, and finish construction withSimpleAsyncResult.Builder.build().
-