IOS NSDateFormatter not thread processing

Source: Internet
Author: User

Remember the last time we opened the Codereview assembly someone raised " nsdateformatter *dateformatter = [[NSDateFormatter alloc] init]; Go to get NSDate or nsstring (date) "Long or high frequency call this method will appear the page operation committed card eg rolling sliding switch."

Viewing the CPU can clearly see that dateformatter creation is obviously an operation that consumes memory. At first I would like to use simple interest or static processing here, but there will be occasional flash back. Apple's official explanation is that NSDateFormatter is a non-thread class

The correct way to do this is to refer to the online explanation "when multiple threads access 1 NSDateFormatter objects at the same time, it is possible to crash.
So what? Don't be discouraged, let's prepare 1 nsdateformatter for each thread, and we won't get beaten up. "

My understanding is to prevent deadlocks------resource competition is equivalent to creating shadow data

Then the same thread is created once the other is called to re-create the different threads so that there is no competition between threads of resources.

Adding breakpoints makes it easy to understand the logic of the mechanism

Use this method where you need to improve efficiency to ensure thread safety

IOS NSDateFormatter not thread processing

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.