[IOS] callback mselector: withobject: afterdelay: problems that do not work and Solutions

Source: Internet
Author: User
This problem has been found for nearly one day. Baidu and Google have been around for a long time, so no one has encountered the same problem?
Both mselector: withobject: afterdelay: And [nst1_timerwithtimeinterval: Invocation: repeats:] have this problem: after the call, after the delay (the same as the delay of 0), the specified @ selecter () is not entered (). Repeated verification found that this phenomenon occurs in the Child thread and can be used normally in the main thread. Therefore, make sure that the callback mselector: withobject: afterdelay: is called in the main thread.

However, we didn't test whether to create an nstimer in the Child thread for latency. It should be okay.
[Nstimer timerwithtimeinterval: Invocation: repeats.
Special implementation
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay
Method to ensure successful calls in the main thread

// Nsobject_performselectorex.h

@ Interface nsobject (performselectorex)

-(Void) implements mselecw.mainthread :( SEL) aselector withobject :( ID) anargument afterdelay :( nstimeinterval) delay;

@ End

// Nsobject_performselectorex.m

# Import
"Nsobject_performselectorex.h"

@ Implementation nsobject (effecmselectorex)

-(Void) implements mselecw.mainthread :( SEL) aselector withobject :( ID) anargument afterdelay :( nstimeinterval) Delay

{


Nsmethodsignature * Sig = [self
Methodsignatureforselector: aselector];


If (! SIG)

{


Return;

}


Nsinvocation * invo = [nsinvocation
Invocationwithmethodsignature: SIG];

[Invo
Settarget: Self];

[Invo
Setselector: aselector];

[Invo
Setargument: & anargument atindex: 2];

[Invo
Retainarguments];


Nsmethodsignature * sigmt = [invo methodsignatureforselector: @ selector (performselector: withobject: afterdelay :)];


Nsinvocation * invomt = [nsinvocation
Invocationwithmethodsignature: sigmt];

[Invomt
Settarget: invo];

[Invomt
Setselector: @ selector (effecmselector: withobject: afterdelay :)];


Sel arg1 = @ selector (invoke );


Void * arg2 = nil;


Nstimeinterval arg3 = delay;

[Invomt
Setargument: & arg1 atindex: 2];

[Invomt
Setargument: & arg2 atindex: 3];

[Invomt
Setargument: & arg3 atindex: 4];

[Invomt
Retainarguments];

[Invomt
Performselectoronmainthread: @ selector (invoke)
Withobject: Nil
Waituntildone: No];

}

@ End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.