fromFunction static method

ObjCBlock<Void Function(Pointer<Void>, Pointer<ObjCObject>?)> fromFunction(
  1. void fn(
    1. Pointer<Void>,
    2. ObjCObjectBase?
    ), {
  2. bool keepIsolateAlive = true,
})

Creates a block from a Dart function.

This block must be invoked by native code running on the same thread as the isolate that registered it. Invoking the block on the wrong thread will result in a crash.

If keepIsolateAlive is true, this block will keep this isolate alive until it is garbage collected by both Dart and ObjC.

Implementation

static objc.ObjCBlock<ffi.Void Function(ffi.Pointer<ffi.Void>, ffi.Pointer<objc.ObjCObject>?)> fromFunction(
  void Function(ffi.Pointer<ffi.Void>, objc.ObjCObjectBase?) fn, {
  bool keepIsolateAlive = true,
}) => objc.ObjCBlock<ffi.Void Function(ffi.Pointer<ffi.Void>, ffi.Pointer<objc.ObjCObject>?)>(
  objc.newClosureBlock(
    _ObjCBlock_ffiVoid_ffiVoid_objcObjCObject_closureCallable,
    (ffi.Pointer<ffi.Void> arg0, ffi.Pointer<objc.ObjCObject> arg1) =>
        fn(arg0, arg1.address == 0 ? null : objc.ObjCObjectBase(arg1, retain: true, release: true)),
    keepIsolateAlive,
  ),
  retain: false,
  release: true,
);