sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ›£οΈ Road Profile Builder | Generate roads, curbs and pavements easily Download

    Deleting an observer

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 3 Posters 365 Views 3 Watching
    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.
    • K Offline
      kiesewetter
      last edited by

      Hallo!

      Is it possible to deactivate or delete an observer after the observer has been invoken? Ideally in the observer itself.

      Thanks for your time.

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        I think an observer killing itself is a recipe for failure. 😞

        When you start an observer you couldn give it a global reference like $kieserwetter_observer that is not likely to clash with another global variable.
        Then when you apply it to objects you can always attach the same observer [making a new observer each time will be impossible to keep track of].
        Later if you want to remove an observer from an object you now have a handle [$kieserwetter_observer] to it... in any code at all you might run later...

        TIG

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          You should always be running your code within your OWN namespace, so using a global variable is NEVER needed. I use @@ module vars to hold references to observers.

          YES you can write a method within your observer class that can remove itself. I do this sometimes, and it is really just a wrapper for the remove_observer() method.

          This example assumes your using your Observer in a Singleton manner:

          class MySelectionObs < Sketchup;;SelectionObserver
          
            @@watched = []
          
            def attach(selection)
              raise(TypeError,"argument is not a Sketchup;;Selection object.") unless selection.class==(Sketchup;;Selection)
              if selection.class==(Sketchup;;Selection) &&
                 (not self.watching?(selection))
                selection.add_observer(self)
                @@watched << selection
              end
            end
          
            def dettach(selection)
              raise(TypeError,"argument is not a Sketchup;;Selection object.") unless selection.class==(Sketchup;;Selection)
              if selection.class==(Sketchup;;Selection) &&
                 self.watching?(selection)
                selection.remover_observer(self)
                @@watched.delete(selection)
              end
            end
          
            def verify_watchlist()
              unless @@watched.empty?
                @@watched.delete_if {|e| e.class != Sketchup;;Selection }
                @@watched.delete_if {|e| !e.model.valid? }
              end
            end
          
            def watching()
              verify_watchlist()
              return @@watched.dup
            end
          
            def watching?(selection)
              raise(TypeError,"argument is not a Sketchup;;Selection object.") unless selection.class==(Sketchup;;Selection)
              verify_watchlist()
              return @@watched.include?(selection)
            end
          
            ### define your other callback methods ###
          
          end # class
          

          ONE BIG EXCEPTION : NEVER dettach (remove) observers within the onQuit() callback of an AppObserver subclass instance !! A BugSplat! will occur.

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement