dispatch_once 优化代码性能

本文介绍如何使用dispatch_once技术确保只调用API一次,从而优化变量初始化过程,尤其适用于从文件中读取配置参数或设备型号等场景。通过实例展示其应用方法,提高代码效率。

有些变量只需要初始化一次(如从文件中读取配置参数,读取设备型号等等),可以使用dispatch_once来进行读取优化,保证只调用API一次,以后就只要直接访问变量即可

范例如下:

static BOOL isTestMode;

+ (BOOL)isTestMode

{
    		static dispatch_once_t onceToken;
    		dispatch_once(&onceToken, ^{
       		 	NSNumber* obj = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFResourceTest"];
        		isTestMode = [obj boolValue];
		 });
  	 	 return isTestMode;
	}


D:\PythonProject\.venv\Scripts\python.exe D:\PythonProject\app.py * Serving Flask app 'app' * Debug mode: on WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit * Restarting with stat * Debugger is active! * Debugger PIN: 120-624-777 This runs only once on the first request! 127.0.0.1 - - [10/Jun/2025 16:50:28] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [10/Jun/2025 16:50:28] "GET /static/css/style.css HTTP/1.1" 404 - 127.0.0.1 - - [10/Jun/2025 16:50:28] "GET /static/js/main.js HTTP/1.1" 404 - 127.0.0.1 - - [10/Jun/2025 16:50:30] "GET /doctor_list?department=1&date=2025-06-10 HTTP/1.1" 500 - Traceback (most recent call last): File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 1536, in __call__ return self.wsgi_app(environ, start_response) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 1514, in wsgi_app response = self.handle_exception(e) File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 1511, in wsgi_app response = self.full_dispatch_request() File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 919, in full_dispatch_request rv = self.handle_user_exception(e) File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 917, in full_dispatch_request rv = self.dispatch_request() File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 902, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^ File "D:\PythonProject\app.py", line 64, in doctor_list flash(f'ID为 {department_id} 的科室不存在', 'warning') ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\PythonProject\.venv\Lib\site-packages\flask\helpers.py", line 335, in flash session["_flashes"] = flashes ~~~~~~~^^^^^^^^^^^^ File "D:\PythonProject\.venv\Lib\site-packages\flask\sessions.py", line 104, in _fail raise RuntimeError( ^ RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret. 127.0.0.1 - - [10/Jun/2025 16:50:30] "GET /doctor_list?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 304 - 127.0.0.1 - - [10/Jun/2025 16:50:30] "GET /doctor_list?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 304 - 127.0.0.1 - - [10/Jun/2025 16:50:30] "GET /doctor_list?__debugger__=yes&cmd=resource&f=console.png&s=v8WikeQeBL2PiowtaTJt HTTP/1.1" 200 -
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值