IOS Nsrunloop those things

Source: Internet
Author: User

Timer-related delay calls in iOS, common in NSObject PerformSelector:withObject:afterDelay: This method sets the timer in the current runloop when it is called, There is also a delay to configure the task directly using Nstimer .

Both of these methods have a common premise, that is, the current thread needs to have a running runloop and the Runloop has a timer inside.

We know: Only the main thread will be created by default automatically run a runloop, and have a timer, normal child threads are not these. This brings up a problem, sometimes we are not sure whether our module will be called asynchronously, and we write such a delay call is generally not to check the runtime environment, so that in the child thread is called, our code in the delay call code will wait for the timer to dispatch, But actually there is no such timer in the child thread, so our code will never be transferred.

The following code shows the differences between the performselector and the Dispatch_time

  1. /*
  2. Add to queue with GCD delay
  3. */
  4. -(void) testdispatch_after{
  5. dispatch_time_t time = Dispatch_time (Dispatch_time_now, 3*nsec_per_sec);
  6. dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
  7. Dispatch_after (time, queue, ^{
  8. NSLog (@ "added to queue after 3 seconds");
  9. });
  10. Dispatch_release (queue);
  11. }
  12. -(void) testdelay{
  13. NSLog (@ "Testdelay is executed");
  14. }
  15. /*
  16. The role of the Dispatch_barrier_async fence
  17. */
  18. -(void) testdispatch_barrier{
  19. //dispatch_queue_t gcd = Dispatch_queue_create ("This is a sequence queue", NULL);
  20. dispatch_queue_t gcd = dispatch_queue_create ("This is a concurrent queue", dispatch_queue_concurrent);
  21. Dispatch_async (GCD, ^{
  22. NSLog (@ "B0");
  23. //This selector will not execute because there is no runloop in the thread
  24. [self performselector:@selector (testdelay) withobject: nil Afterdelay:3];
  25. //code will execute because the GCD method is used
  26. [self testdispatch_after];
  27. });
  28. Dispatch_release (GCD);
  29. }

In a multi-threaded environment, such performselector delay calls are in fact a lack of security . We can solve this problem with another set of solutions, that is, using the dispatch_after in GCD to achieve a single time delay call

There is also a solution:

Performselector is not without the means to ensure thread safety. For example, the following code can be run:

    1. [self performselector:@selector (testdelay) onthread:[nsthread Mainthread] withobject:  Nil Waituntildone:NO];

The selector is specified to run in the main thread.

There is also a solution:
    1. [self performselector:@selector (testdelay) withobject: nil afterdelay:3 inmodes:[  Nsarray Arraywithobject:nsdefaultrunloopmode]];
    2. [[Nsrunloop Currentrunloop] runmode:nsdefaultrunloopmode beforedate:[nsdate Distantfuture]];

Start the thread in Runloop, because each thread has a default Runloop

IOS Nsrunloop those things

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.