Precise IOS timing

Source: Internet
Author: User

A timer was used in the previous project. It was thought to be very simple. It was finished with a timer and later found that it was not that simple.

Timer may be blocked, leading to inaccurate timing. The delay is still justified. The key is that the events are indeed getting faster and faster. for sellers who promote the second-level ranking, the events have not arrived yet, everything was taken away.

Later, I used a lot of methods to solve the timing problem perfectly. After checking the information, I finally came up with a better solution:

__block TestViewController *blockSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    blockSelf->_timer=[NSTimer scheduledTimerWithTimeInterval:1.0

                                            target:blockSelf

                                        selector:@selector(caculateLeftTimeForTomorrow)

                                          userInfo:nil

                                           repeats:YES] ;

    [[NSRunLoop currentRunLoop] addTimer:blockSelf->_timer forMode:NSDefaultRunLoopMode];

    [[NSRunLoop currentRunLoop] run];

     });

Only a queue is added on the basis of the original and timer is started in backgroud. In this way, the blocking problem is basically solved. (because the block references the self parameter, the self is retain and the page cannot be released. Therefore, _ block converts self to a local variable)

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.