Ios id Generator

Source: Internet
Author: User

//// IdGenerator. m // Copyright (c) 2014 Qingdao toyu Network Technology Co., Ltd. all rights reserved. // # import "IdGenerator. h "static long time_stamp = 0; static long time_stamp_now = 0; static NSMutableArray * temp = NULL; static NSNumber * random_n = NULL; static NSLock * theLock = NULL; @ implementation IdGenerator/** get the next Id */+ (long) next {if (theLock = NULL) theLock = [[NSLock alloc] init]; if (temp = NULL) temp = [[NSMutableArray alloc] init]; @ synchronized (theLock) {time_stamp_now = [[NSDate date] timeIntervalSince1970]; if (time_stamp_now! = Time_stamp) {// clear the cache and update the timestamp [temp removeAllObjects]; time_stamp = time_stamp_now ;} // determine whether the cache contains the current random number while ([temp containsObject :( random_n = [NSNumber numberWithLong: arc4random () % 8999 + 1000]); if ([temp containsObject: random_n]) {return-1;} [temp addObject: [NSNumber numberWithLong: [random_n longValue];} return (time_stamp * 10000) + [random_n longValue];} @ end

In theory, up to 8999 IDS can be generated every second. In fact, about 6000 IDS can be generated every second.

Reprinted please indicate the source: http://www.cnblogs.com/anxin1225/p/3660506.html

Links: http://c.jinhusns.com/

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.