The pitfalls we have stepped on in those years-NSTimer, pitfall-NSTimer

Source: Internet
Author: User

The pitfalls we have stepped on in those years-NSTimer, pitfall-NSTimer

When I was working yesterday afternoon, I met this requirement. When a network request fails, I saved the request data to my local computer and resend it three times automatically. The interval was 10 seconds, if the interface fails after three times, the new data and local data must be sent during the next time you start the interface. At the beginning, it was not difficult, instead of sending data through network requests, the first response in my mind is to use a timer, initialize the timer, trigger the corresponding method, set the number of requests, and stop the timer more than three times. Facts prove that I have not understood the timer ......

Because it is an old interface, it cannot be modified. Because the product has been launched and too many services are involved in the modification, the client can only find a solution. The problem is that new data cannot be sent together with old data, and the score is sent twice. Okay, let's go. I'm confident.

Initialize the timer and traverse the local data. Create a timer and load it to the timer array using the following method.

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

Then run the fire command. OK.

Bi ......

Clean, the timer is all messy, there is no problem with the timer within 10 s, after 10 s, so the timer is alternating... Isn't that a trap ....

I took a breath, drank a cup of water, and glanced at the timer code. It was a flash. Will it be wrong for the fire? Do not execute it immediately during initialization, wait until the initialization is complete and take out the timer from the array. After the request is successful or fails three times, take out the second timer request. Hahahahahahahahahaha, it should not be wrong.

Bi ........

I went there. It was a little better. The data in 20 seconds was normal, and the timer in the back continued to work .... Clay coal ah, Gan have... But it has been improved. At least 20 seconds is correct. You can change the code. So you can immediately think about the timer execution process, later I found out that there is a difference between the running of multiple timers and one timer? This is because you can basically create a timer before, which is used by fire and invalidate. No way. Go to SOF. Later, I learned that the timer initialized by the two methods will be executed after NSTimeInterval even if the fire is not used. The fire just asks them to execute immediately and advance the start time to the current time, just like a concert was intended to start in 10 minutes, now it's because the lead singer arrived at the venue 10 minutes in advance and saw fans start in advance.

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

But the problem comes again. Since there is no way to control the timer execution, I cannot do this function. Is there any way to control the timer and execute it when I want to execute it, if you don't want to execute it, you will lose it ....

Several Methods for initializing the timer are also found during the data search process: two class methods, one instance method.

+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;- (id)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(id)ui repeats:(BOOL)rep;

What is the difference between this and the above initialization method? Then the two class methods and instance methods should be manually added to the nsunloop code for execution:

[[NSRunLoop currentRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];

Hahaha, this is not what I want (please wait), yo check now!

Modify the timer method, manually add the nsunloop for execution, and then the network requests remain unchanged .... OK ..

Bi ......

No pressure .... Test A for loop for 1000 times without any errors... Let's make a summary.

I have always been used to initializing the timer using the top two methods, and then the fire is only used to advance the timer time, which was not considered before .. This kind of stuff won't have any problem under a timer, but it is basically a tragedy if there are multiple timers .. However, I do not know how to use multiple timers in the same place for the time being. I may also encounter unexpected errors, such as soaring memory and performance impact, if you have a better solution, you can exchange ideas.

 




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.