onAppear handler will be called again

Asked by larryli on 2020-02-20

Now, I use 2.0.2 version
as the following code, I already stopObserver in handler1, but when open app2, it also have the image1, as the log, handler1 will be executed again. whether stopObserver in handler1 just stop scan, it should clear image1 from onAppear?
if yes, is there any method?

def handler1(event):
     event.stopObserver()
     print "handler1 is ready"
     global process
     process = false

openApp("application path")
global process
process = True
onAppear("image1", hander1)
observe(FOREVER)

while True:
       wait(2)
      if process == False: break

def handler2(event):
     event.stopObserver()
     print "handler2is ready"

openApp("app2 path")
onAppear("image2", hander2)
observe(FOREVER)

thank you very much

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2020-02-20
Last query:
2020-02-20
Last reply:
2020-02-20
larryli (larryli2020) said : #1

for this issue, I have try it the following modification, it can control observe.
it can fix the issue, I have one propsal, whether it can provide API to set observe name, because, as the code, it can use name to add and remove. just check master branch code, it seems not have.

if it already have, sorry for my less check.

def handler1(event):
     event.stopObserver()
     event.getRegion().getObserver().removeObserver(event.getName())
     print "handler1 is ready"
     global process
     process = false

openApp("application path")
global process
process = True
onAppear("image1", hander1)
observe(FOREVER)

while True:
       wait(2)
      if process == False: break

def handler2(event):
     event.stopObserver()
     event.getRegion().getObserver().removeObserver(event.getName())
     print "handler2is ready"

openApp("app2 path")
onAppear("image2", hander2)
observe(FOREVER)

Manfred Hampl (m-hampl) said : #2

Is there a typo error handlerX vs. handerX ?

onAppear("image1", hander1)
def handler1(event):

RaiMan (raimund-hocke) said : #3

onXXX events are registered per Region (the screen object SCREEN in your case - the main screen).

When using observe(), ALL registered events are activitated, which are in your case.

You already tried to deactivate the first onAppear, but it is done in the handler, which is too late (observe already running) and the deactivation would not run anyways, since you did not store the name returned by onAppear().

What I do not understand:
Why don't you juste use the normal wait()?

In your case using observe(someTime) is nothing else than simply waiting for image1/image2 to appear:

if exists("image1", FOREVER):
    #do something

if exists("image2", FOREVER):
    #do something else

larryli (larryli2020) said : #4

because, image2 will include same image like image1, so this is the reason I want to remove image1's observer,
and also, maybe my image is not good.

in fact, I want onAppear some image together, so I select onAppear, so I have this question.

if I use If exists forever, it will wait continuely, both image1 and image2 are come out once, and then it can exit, is it right?

anyway, thanks for your answer.

RaiMan (raimund-hocke) said : #5

yes, exists exits, when the given image appears.

larryli (larryli2020) said : #6

is there any API or method to control remove onAppear content
for example, if image2 is appear, it can remove image1's observe?

larryli (larryli2020) said : #8

Thanks RaiMan, that solved my question.