omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    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.


    Traceback using Gestures module

    Pythonista
    custom-view gesture objc
    3
    25
    14402
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • shinyformica
      shinyformica last edited by

      And...doing it again...(I should really test my theories before posting)...if I simply force a new ObjC class definition to be created for the gesture delegate for each different view class, by modifying Gestures.py slightly so it takes a "name" in its init():

      try:
          PythonistaGestureDelegate = ObjCClass('PythonistaGestureDelegate_{0}'.format(name))
      except:
          PythonistaGestureDelegate = create_objc_class('PythonistaGestureDelegate_{0}'.format(name),
      

      this works, in that it now does what I expect and calls the methods defined on the specific view class which the Gestures object was created in. But my understanding of what's happening on the Objective-C side of things is not clear here.

      mikael 1 Reply Last reply Reply Quote 0
      • mikael
        mikael @shinyformica last edited by

        @shinyformica, I think the issue is related to the way I have used closure to access self in the ObjC delegate functions. Maybe we could find another way to connect to the Python instance.

        1 Reply Last reply Reply Quote 0
        • shinyformica
          shinyformica last edited by shinyformica

          @mikael yeah, that's definitely the reason, in some fashion. The Objective-C side of things is definitely holding tightly to the Gestures object instance that "self" refers to at the time the delegate ObjCClass is defined. I had to modify it even further to get it to work from one run of the script to another (without killing and restarting Pythonista in between) because it was still holding the reference to the first instance of my custom View class that created the ObjCClass from one run to the next, and thus passing the wrong gesture recognizer instances to the simultaneous gesture and fail/fail_other calls.

          Not knowing much about how this works under the hood, I don't know how to prevent it, but I'll look into it a bit more...perhaps it would be possible to hold or pass the reference to the Gestures object as data, and use some generic way to call the appropriate method on that object, instead of holding the method instance.

          mikael 1 Reply Last reply Reply Quote 0
          • mikael
            mikael @shinyformica last edited by mikael

            @shinyformica, latest version on Github now uses a class-level lookup to find the right instance when calling delegate functions. Could you please give it a try?

            1 Reply Last reply Reply Quote 0
            • shinyformica
              shinyformica last edited by

              @mikael just got to try this this morning. Unfortunately, while it seems like it would work, I once again encounter the strange issue of module-level things becoming undefined when the script is run a second time, from way back at the start of this thread. In this case it is the actual Gestures class which is suddenly None when the code tries to call Gestures.get_self(cls, key).

              Again, this only happens the second time the script runs, and every time thereafter. First time through, things work as expected. I actually managed a solution of my own, which isn't ideal: I append the id of the instance of the Gestures object to the name of the delegate class being created by the call to objc_util.create_objc_class() so each Gestures instance gets a unique delegate class and instance of that class, which will be sure to have self as a reference to that particular Gestures instance. This appears to solve the problem, though it means I'll have a new ObjC class definition for each Gesture instance, which in my case means one for each control that gets instantiated...and I don't know the performance or memory implications of that.

              I really wish I knew why I was having that strange "undefinition" issue, since your solution only creates one class, and registers the instances of the Gesture object, it seems much more efficient. I'll see if I can figure out a way to do something similar to your way of finding a reference to the Gestures instance without holding an explicit self reference, without triggering my other problem.

              1 Reply Last reply Reply Quote 0
              • shinyformica
                shinyformica last edited by

                @mikael a little more fiddling around, and looking at how other people have used objc_util and ObjCInstance's, it seems I can just stash a reference to the Gestures object instance for my control in the actual delegate instance created for that control:

                self._delegate = PythonistaGestureDelegate.new()
                self._delegate._gestures = self
                

                and then I can safely access that reference in the objc callbacks:

                def gestureRecognizer_shouldRecognizeSimultaneouslyWithGestureRecognizer_(_self, _sel, gr, other_gr):
                    import objc_util
                    delegate_instance = objc_util.ObjCInstance(_self)
                    gestures_instance = delegate_instance._gestures
                    return gestures_instance.objc_should_recognize_simultaneously(self.recognize_simultaneously, gr, other_gr)
                

                so now only one actual delegate class definition is created in objective-c, and the delegate instances for each control are all separately able to access the Gestures instance they use. I'll have to make sure I break the cyclic reference between the gesture delegate instance and the Gestures instance, but I can do that via a weakref or explicitly removing the cycle when I get rid of the control.

                mikael 1 Reply Last reply Reply Quote 0
                • mikael
                  mikael @shinyformica last edited by

                  @shinyformica, thanks, I will implement this. Does this option suffer from the vanishing Gestures class?

                  I will also check this with an iPad.

                  1 Reply Last reply Reply Quote 0
                  • shinyformica
                    shinyformica last edited by

                    @mikael thankfully it does not suffer from that strange vanishing-definitions thing. Though I am now locally importing objc_util anyplace I try to use ObjCInstance() or ObjCClass(), which appears to prevent it from showing up and causing trouble.

                    I tested the above technique using weakref to hold the reference to the Gestures object and it worked fine, so that will prevent a cyclic-reference:

                    self._delegate = PythonistaGestureDelegate.new()
                    
                    import weakref
                    self._delegate._gestures = weakref.ref(self)
                    
                    mikael 1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB last edited by

                      hey, have you copies objc_util and made edits? objc_util should actually never be cleared after it is loaded, so unless you are manually del'ing sys.modules['objc_util'] it should not be getting cleared out from under you. But, if you had a shadow copy on your path, it would.

                      import objc_util
                      print(objc_util.__file__)
                      

                      i have an older version of gestures, but i also could not reproduce your issues

                      1 Reply Last reply Reply Quote 0
                      • mikael
                        mikael @shinyformica last edited by

                        @shinyformica, thanks. I will include the weakref version.

                        I was also wondering if you had some globals-clearing code somewhere. Remembering some previous threads where the goal was to make Pythonista act like a fresh interpreter starting every time you run a script.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post
                        Powered by NodeBB Forums | Contributors