Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Problems when using objc_util()
-
@zrzka wrote a drag script
-
This post is deleted! -
@rozaimech, good to note that the view you attach the drag on must be
touch_enabled
. Just spent a while wondering why my Label is not happening... -
@cvp
drag already written
and drop already written, too
but Random crashes have no error log -
@rozaimech did you install @dgelessus ´s log as adviced by @jonb in a previous post of this topic? And is it a _objc_exception.txt file in the root?
-
@cvp
The first call successfully gets the dragged data, but the second flash back. I'm trying to resolve this error.The error log isFatal Python error: Segmentation fault Thread 0x000000016fc77000 (most recent call first):
-
@rozaimech I got it more than ten times during my tests for trying to help you. I didn't find the reason.
If you change the code of a delegate method, restart Pythonista, that helps often -
@cvp
ok....I should have avoided this mistake long ago...
Just let this asynchronous function run on the main thread. Everything is normal, no flashbacks, and no errors........ -
@rozaimech Congratulations....
If not secret/confidential 😀, please share your code -
@cvp
There will be an error message:TypeError..# coding: utf-8 from objc_util import * import ui def dropInteraction_canHandleSession_(_m,_c,interaction,session): return True def dropInteraction_sessionDidUpdate_(_m,_c,interaction,session): return ObjCClass('UIDropProposal').alloc().initWithDropOperation(2).ptr def dropInteraction_performDrop_(_m,_c,interaction,session): session=ObjCInstance(session) def handler(_cmd,obj_ptr): obj=ObjCInstance(obj_ptr) print(obj) handler_block = ObjCBlock(handler, restype=None, argtypes=[c_void_p,c_void_p]) for item in session.items(): itemProvider =item.itemProvider() uiimage =ObjCClass('UIImage') nsstring =ObjCClass('NSString') if(itemProvider.canLoadObjectOfClass(uiimage)): on_main_thread(itemProvider.loadObjectOfClass_completionHandler_(ObjCClass('UIImage'),handler_block)) elif(itemProvider.canLoadObjectOfClass(nsstring)): on_main_thread(itemProvider.loadObjectOfClass_completionHandler_(nsstring,handler_block)) else: continue methods=[dropInteraction_canHandleSession_,dropInteraction_sessionDidUpdate_,dropInteraction_performDrop_] protocols=['UIDropInteractionDelegate'] dropDelegate=create_objc_class('MydropDelegate',methods=methods,protocols=protocols) Delegate=dropDelegate.alloc().init() dropInteraction=ObjCClass('UIDropInteraction').alloc().initWithDelegate(Delegate) drop_view = ui.View(bg_color='blue',frame=(0,0,500,500)) drop_view.objc_instance.setUserInteractionEnabled(True) drop_view.objc_instance.addInteraction(dropInteraction) drop_view.present()
-
@rozaimech I don't understand why
on_main_thread(itemProvider.loadObjectOfClass_completionHandler_
raises this error
Traceback (most recent call last): File "_ctypes/callbacks.c", line 234, in 'calling callback function' File "/private/var/mobile/Containers/Shared/AppGroup/668A7D98-7216-47ED-917D-AA0B6173167E/Pythonista3/Documents/drop_provider.py", line 26, in dropInteraction_performDrop_ on_main_thread(itemProvider.loadObjectOfClass_completionHandler_( nsstring, handler_block)) File "/var/containers/Bundle/Application/34BAEE1A-BC33-4D6F-A0C1-B733E4991F31/Pythonista3.app/Frameworks/Py3Kit.framework/pylib/site-packages/objc_util.py", line 1085, in on_main_thread raise TypeError('expected a callable') TypeError: expected a callable
Sure @JonB could help you
-
@cvp
jonb said :
the Pythonista hava two threads
1.The main thread, or ui thread
2.The interpreter threadI don't know if the error is related to IOS thread.
but I remember that the apple development document said that the NSItemProvider interface should be used in DispatchQueue.main.async { }I'm learning something about it..
-
@rozaimech what is strange is that itemProvider.loadObjectOfClass_completionHandler_ has a
__call__
method, thus should be callable
-
The code works, the problem is that on_main_thread function (see objc_util source code) raises an error if the called function is not Python-callable, but on_main_thread function continues and the function is called anyway...
-
on_main_thread(itemProvider.loadObjectOfClass_completionHandler_(ObjCClass('UIImage'),handler_block))
Try instead:
on_main_thread(itemProvider.loadObjectOfClass_completionHandler_)(ObjCClass('UIImage'),handler_block)
In other words, on_main_thread returns a method, which you call using arguments. The way you had it, you were calling a function, and passing it's output, which was not a function, to on_main_thread.
on_main_thread(original_func)(args)
I would also question whether you need to be calling on_main_thread at all here.
-
@JonB said:
on_main_thread(itemProvider.loadObjectOfClass_completionHandler_)(ObjCClass('UIImage'),handler_block)
Works 😀
No other words than magic
-
Ahh, I see you are trying to make sure the callback gets run on the main thread... The above code doesn't do that--it just sets the callback on the main thread.
If things within the callback must be done on the main thread, you need such code inside the callback handler itself. For instance, you could use a decorator on the handler.
Another common problem is calling ObjCInstance on some non object. @shinyformica had a thread a while back about a wierd seg fault, that we were able to fix I think by prechecking some condition in a loop, and only proceeding once things were correct. The details evade me.
-
@JonB Without on_main_thread, segmentation fault
-
@JonB this also gives segmentation fault
@on_main_thread def handler(_cmd,obj_ptr):
-
@cvp said:
on_main_thread(itemProvider.loadObjectOfClass_completionHandler_)(ObjCClass('UIImage'),handler_block)
works once and crashes with segmentation fault the second time...