桑心啊,List<T>.FindAll()的效率竟然比for循环还差。

本文通过实验对比了C#中List<T>.FindAll()方法与For循环在查找符合条件数据项时的效率。结果显示,在小规模数据集上,For循环的效率远高于FindAll()方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自从认识了FindAll(),一般List中查找符合条件的数据项我都用List<T>.Find()或List<T>.FindAll().

代码简洁,自己看了赏心悦目,况且是微软内部的方法,效率应该不会差。

然而,今天心血来潮,写代码测试了一下,确发现FindAll()比For循环效率差了好多,集合比较小的时候这差异就更明显了。

测试代码:

Stopwatch watch = new Stopwatch();
			Random rand = new Random();
			List<int> list = new List<int>();
			//构造一个LIST
			for (int i = 0; i < 100; i++)
			{
				list.Add(rand.Next(10));
			}
			//用FindAll
			watch.Start();
			List<int> resultFindAll = list.FindAll(delegate(int i) { return i == 5; });
			watch.Stop();
			Console.WriteLine("FindAll:" + watch.Elapsed.Ticks);
			Console.WriteLine("resultFindAll:" + resultFindAll.Count);
			//用For循环
			List<int> resultFor = new List<int>();
			watch.Reset();
			watch.Start();
			for (int i = 0; i < list.Count; i++)
			{
				if (list[i] == 5)
				{
					resultFor.Add(list[i]);
				}
			}
			watch.Stop();
			Console.WriteLine("For:"+watch.Elapsed.Ticks);
			Console.WriteLine("resultFor:" + resultFor.Count);

  List长度为100时的测试结果,即上面的代码:

哎,集合小的时候差距最为明显,想不到差了五六十倍。

看来以后List<T>.FindAll()还是少用为好。

转载于:https://www.cnblogs.com/Gyoung/archive/2012/04/24/2467846.html

每次启动项目就报错tclass=binder permissive=0 2025-06-16 01:23:27.122 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197160, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.123 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197166, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.124 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197184, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.125 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197188, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.125 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197192, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.125 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197198, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.126 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197203, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.126 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197207, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.126 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197212, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.126 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197217, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.126 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197221, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.126 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197225, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.127 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197229, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.127 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197234, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.127 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197238, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.127 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197242, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.128 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197251, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.128 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197255, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.129 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197259, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.131 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197311, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.132 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197323, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.132 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197328, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.132 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197338, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.133 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197342, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.133 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197346, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.133 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197350, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.133 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197355, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.134 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197362, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.134 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197366, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.134 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197370, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.134 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197375, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.135 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197380, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.135 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197384, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.140 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197500, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.140 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197504, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.141 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197508, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.141 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197513, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.141 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197517, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.141 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197521, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.142 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197525, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.142 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197531, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.142 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197536, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.142 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197540, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.142 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197544, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.143 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197549, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.143 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197555, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.143 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197559, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.143 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197564, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.144 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197568, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.144 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197572, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.144 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197576, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.144 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197580, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.145 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197589, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.145 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197593, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.145 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197597, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.146 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197602, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.146 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197606, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.146 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197610, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.147 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197614, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.147 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197623, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.147 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197627, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.148 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197631, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.148 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197643, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.148 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197653, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.149 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197662, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.149 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197668, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.150 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197683, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.150 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197688, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.150 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197693, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.151 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197698, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.151 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197703, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.151 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197707, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.151 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197712, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.151 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197716, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.152 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197720, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.152 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197725, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.152 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197729, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.152 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197733, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.153 18989-18989 IPCThreadState service E Binder transaction failure. id: 152197738, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:23:27.169 18989-18989 IPCThreadState service E Binder transaction failure. id: 152198037, BR_: 29201, error: -1 (Operation not permitted) 1970-01-01 08:00:00.000 0-0 <no-tag> I ---------------------------- PROCESS STARTED (30531) for package com.example.kucun2 ---------------------------- 2025-06-16 01:23:27.698 2479-2931 ActivityTaskManager system_server I Displayed com.example.kucun2/.MainActivity for user 0: +471ms 1970-01-01 08:00:00.000 0-0 <no-tag> I ---------------------------- PROCESS ENDED (30531) for package com.example.kucun2 ---------------------------- 2025-06-16 01:48:24.319 20671-20671 top top W type=1400 audit(0.0:1625176): avc: denied { open } for path=“/proc/stat” dev=“proc” ino=4026532011 scontext=u:r:vivo_daemon:s0 tcontext=u:object_r:proc_stat:s0 tclass=file permissive=0 app=com.taptap 2025-06-16 01:48:24.643 20671-20671 top top W type=1400 audit(0.0:1625177): avc: denied { open } for path=“/proc/stat” dev=“proc” ino=4026532011 scontext=u:r:vivo_daemon:s0 tcontext=u:object_r:proc_stat:s0 tclass=file permissive=0 app=com.taptap 2025-06-16 01:53:05.612 20890-20890 cmd . E BBinder_init Processname cmd 2025-06-16 01:53:05.613 20890-20890 cmd . E BBinder_init hasGetProcessName cmd 2025-06-16 01:53:06.094 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.106 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463268, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.106 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463275, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.107 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463282, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.107 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463288, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.108 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463292, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.108 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463297, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.108 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463304, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.109 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463309, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.109 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463313, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.109 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463317, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.110 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463321, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.110 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463325, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.111 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463329, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.111 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463334, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.112 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463339, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.115 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463386, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.116 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463391, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.118 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463413, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.118 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463418, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.119 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463422, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.121 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463448, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.122 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463452, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.122 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463456, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.131 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463589, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.131 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463600, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.133 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463620, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.133 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463628, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.134 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463635, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.134 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463641, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.134 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463646, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.135 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463652, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.135 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463657, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.135 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463661, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.136 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463665, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.136 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463674, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.137 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463680, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.142 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463734, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.142 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463740, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.143 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463750, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.144 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463757, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.144 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.150 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.152 20903-20903 IPCThreadState service E Binder transaction failure. id: 152463894, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.152 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.154 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.161 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.163 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.165 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.167 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.168 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.172 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.178 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.191 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.193 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.202 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.208 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.233 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.244 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.261 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.265 20903-20903 Parcel service E Reading a NULL string not supported here. 2025-06-16 01:53:06.286 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465315, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.287 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465319, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.288 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465323, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.289 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465327, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.292 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465341, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.293 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465345, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.293 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465349, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.294 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465353, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.295 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465357, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.295 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465361, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.296 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465365, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.296 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465369, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.297 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465373, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.297 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465377, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.298 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465381, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.299 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465385, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.299 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465389, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.300 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465393, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.301 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465402, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.302 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465406, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.303 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465410, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.310 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465449, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.311 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465458, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.312 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465462, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.314 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465471, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.315 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465475, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.315 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465479, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.316 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465483, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.317 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465487, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.318 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465491, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.319 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465495, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.320 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465499, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.321 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465503, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.321 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465507, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.322 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465511, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.337 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465610, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.338 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465614, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.339 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465618, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.340 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465622, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.340 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465626, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.341 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465630, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.342 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465634, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.343 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465638, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.344 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465642, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.345 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465646, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.345 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465650, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.346 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465654, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.347 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465658, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.347 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465662, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.348 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465666, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.349 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465670, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.350 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465674, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.350 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465678, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.351 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465682, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.352 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465691, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.353 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465695, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.354 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465699, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.354 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465703, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.355 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465707, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.356 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465711, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.357 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465715, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.358 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465724, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.359 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465728, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.359 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465732, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.362 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465741, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.363 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465745, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.363 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465749, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.364 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465753, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.366 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465767, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.367 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465771, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.368 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465775, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.369 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465779, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.369 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465783, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.370 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465787, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.371 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465791, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.372 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465795, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.372 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465799, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.373 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465803, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.374 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465807, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.375 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465811, BR_: 29201, error: -1 (Operation not permitted) 2025-06-16 01:53:06.375 20903-20903 IPCThreadState service E Binder transaction failure. id: 152465815, BR_: 29201, error: -1 (Operation not permitted)package com.example.kucun2.entity.data; import android.content.Context; import android.content.SharedPreferences; import android.os.Handler; import android.os.Looper; import android.util.Log; import com.example.kucun2.entity.; import com.example.kucun2.function.MyAppFnction; import com.example.kucun2.function.SafeLogger; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.reflect.TypeToken; import java.io.IOException; import java.lang.reflect.; import java.util.; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; import okhttp3.; /** 核数据管理中 职责: 管理所有实体类的全局存储列表 处理网络数据加载与本地持久化 维护实体间关联关系 管理数据同步状态 优化点: 使用ConcurrentHashMap提升线程安全 重构关联逻辑避免反射开销 增强序列化/反序列化处理 添加详细方法级注释 */ public class Data { // ====================== 实体存储区 ====================== // 所有实体列表使用线程安全的SynchronizedList // ====================== 静态数据列表声明 ====================== public static final SynchronizedList<Bancai> bancais = new SynchronizedList<>(Bancai.class); public static final SynchronizedList<Caizhi> caizhis = new SynchronizedList<>(Caizhi.class); public static final SynchronizedList<Mupi> mupis = new SynchronizedList<>(Mupi.class); public static final SynchronizedList<Chanpin> chanpins = new SynchronizedList<>(Chanpin.class); public static final SynchronizedList<Chanpin_Zujian> chanpin_zujians = new SynchronizedList<>(Chanpin_Zujian.class); public static final SynchronizedList<Dingdan> dingdans = new SynchronizedList<>(Dingdan.class); public static final SynchronizedList<Dingdan_Chanpin> dingdan_chanpins = new SynchronizedList<>(Dingdan_Chanpin.class); public static final SynchronizedList<Dingdan_chanpin_zujian> Dingdan_chanpin_zujians = new SynchronizedList<>(Dingdan_chanpin_zujian.class); public static final SynchronizedList<Kucun> kucuns = new SynchronizedList<>(Kucun.class); public static final SynchronizedList<Zujian> zujians = new SynchronizedList<>(Zujian.class); public static final SynchronizedList<User> users = new SynchronizedList<>(User.class); public static final SynchronizedList<Jinhuo> jinhuos = new SynchronizedList<>(Jinhuo.class); private static User user; // 实体类型与列表的映射表 <实体类, 对应的同步列表> public static final Map<Class, SynchronizedList<SynchronizableEntity>> dataCollectionMap = new ConcurrentHashMap<>();; private static final Gson gson = GsonFactory.createGson(); private static OkHttpClient client; private static final String TAG = “DataLoader”; // 静态初始化:建立实体类型与列表的映射关系 static { try { // 通过反射获取所有SynchronizedList字段 for (Field field : Data.class.getDeclaredFields()) { if (SynchronizedList.class.equals(field.getType())) { SynchronizedList<?> list = (SynchronizedList<?>) field.get(null); if (list != null) { // 将实体类型与列表关联 dataCollectionMap.put(list.getEntityType(), (SynchronizedList<SynchronizableEntity>) list); } } } } catch (IllegalAccessException e) { throw new RuntimeException(“初始化dataCollectionMap失败”, e); } } // ====================== 核数据加载方法 ====================== /** 从服务器加载全量数据 @param context Android上下文 @param callback 加载结果回调 */ public static void loadAllData(Context context, LoadDataCallback callback) { // 主线程检查 if (Looper.myLooper() != Looper.getMainLooper()) { throw new IllegalStateException(“必须在主线程调用Data.loadAllData”); } ensurePreservedObjects(); // 确保存在预置对象 // 使用传入的 Context 获取主线程 Handler Handler mainHandler = new Handler(context.getMainLooper()); // 确保使用安全的客户端 if (client == null) { client = MyAppFnction.getClient(); } SynchronizableEntity.setSyncEnabled(false); String url = MyAppFnction.getStringResource(“string”, “url”) + MyAppFnction.getStringResource(“string”, “url_all”); Request request = new Request.Builder().url(url).build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e(TAG, “Failed to load data”, e); SynchronizableEntity.setSyncEnabled(true); mainHandler.post(() -> { if (callback != null) callback.onFailure(); }); } @Override public void onResponse(Call call, Response response) throws IOException { if (!response.isSuccessful()) { Log.e(TAG, "Unexpected response code: " + response.code()); SynchronizableEntity.setSyncEnabled(true); mainHandler.post(() -> { if (callback != null) callback.onFailure(); }); return; } String responseData = response.body().string(); SynchronizableEntity.setSyncEnabled(true); ensurePreservedObjects(); // 在主线程处理解析 mainHandler.post(() -> { parseAndAssignData(responseData, context, callback); }); } }); } // ====================== 数据处理私有方法 ====================== /** 解析JSON数据并更新到实体列表 */ private static void parseAndAssignData(String jsonData, Context context, LoadDataCallback callback) { try { // 解析顶层响应结构 Type responseType = new TypeToken<Information<AllDataResponse>>() {}.getType(); Information<AllDataResponse> info = gson.fromJson(jsonData, responseType); Log.d(TAG, "parseAndAssignData: "+jsonData); // 验证响应有效性 if (info == null || info.getData() == null || info.getStatus() != 200) { throw new IllegalStateException(“无效服务器响应”); } AllDataResponse allData = info.getData(); SafeLogger.d("data", "原始数据: " + gson.toJson(allData)); // 更新所有数据列表 updateAllLists(allData); automaticAssociation(); // 建立实体关联 setAllEntitiesState(SynchronizableEntity.SyncState.MODIFIED); // 设置状态 safeCallback(callback, true); // 成功回调 } catch (Exception e) { Log.e(TAG, "数据处理异常: " + e.getMessage()); safeCallback(callback, false); } finally { SynchronizableEntity.setSyncEnabled(true); // 重新启用同步 } } /** 批量更新所有实体列表 */ private static void updateAllLists(AllDataResponse allData) { updateList(bancais, allData.bancais); updateList(caizhis, allData.caizhis); updateList(mupis, allData.mupis); updateList(chanpins, allData.chanpins); updateList(chanpin_zujians, allData.chanpin_zujians); updateList(dingdans, allData.dingdans); updateList(dingdan_chanpins, allData.dingdan_chanpins); updateList(Dingdan_chanpin_zujians, allData.Dingdan_chanpin_zujians); updateList(kucuns, allData.kucuns); updateList(zujians, allData.zujians); updateList(users, allData.users); updateList(jinhuos, allData.jinhuos); } /** 安全更新单个列表(保留预置对象) */ private static <T extends SynchronizableEntity> void updateList( List<T> existingList, List<T> newList ) { if (newList == null) return; // 保留现有列表中的预置对象 List<T> preservedItems = existingList.stream() .filter(item -> item != null && item.isPreservedObject()) .collect(Collectors.toList()); // 清空后重新添加(预置对象 + 有效新数据) existingList.clear(); existingList.addAll(preservedItems); existingList.addAll(newList.stream() .filter(item -> item != null && item.getId() != null && item.getId() != -1) .collect(Collectors.toList()) ); } /** 确保每个列表都有预置对象(用于表示空值/默认值) */ private static void ensurePreservedObjects() { // 为每个实体类型检查并添加预置对象 addIfMissing(bancais, Bancai.class); addIfMissing(caizhis, Caizhi.class); addIfMissing(mupis, Mupi.class); addIfMissing(chanpins, Chanpin.class); addIfMissing(chanpin_zujians, Chanpin_Zujian.class); addIfMissing(dingdans, Dingdan.class); addIfMissing(kucuns, Kucun.class); addIfMissing(zujians, Zujian.class); addIfMissing(Dingdan_chanpin_zujians, Dingdan_chanpin_zujian.class); addIfMissing(dingdan_chanpins, Dingdan_Chanpin.class); addIfMissing(jinhuos, Jinhuo.class); addIfMissing(users, User.class); } private static <T extends SynchronizableEntity> void addIfMissing( List<T> list, Class<T> clazz ) { if (!containsPreservedObject(list)) { list.add(createInstance(clazz)); } } /** 检查列表是否包含预置对象 @param list 目标实体列表 @return 是否包含预置对象 / private static <T extends SynchronizableEntity> boolean containsPreservedObject(List<T> list) { return list.stream().anyMatch(item -> item != null && item.isPreservedObject() ); } /* 自动建立实体间关联关系(通过反射实现) */ private static void automaticAssociation() { for (Class<?> entityClass : dataCollectionMap.keySet()) { try { associateEntities(dataCollectionMap.get(entityClass)); } catch (Exception e) { Log.e(TAG, entityClass.getSimpleName() + " 关联失败", e); } } } private static <T extends SynchronizableEntity> void associateEntities( SynchronizedList<T> list ) throws IllegalAccessException, ClassNotFoundException { for (T entity : list) { if (entity == null) continue; for (Field field : entity.getClass().getDeclaredFields()) { field.setAccessible(true); Class<?> fieldType = field.getType(); // 处理实体引用字段 if (SynchronizableEntity.class.isAssignableFrom(fieldType)) { associateSingleReference(entity, field); } // 处理实体列表字段 else if (List.class.isAssignableFrom(fieldType)) { associateReferenceList(entity, field); } // 处理基础类型字段 else { setDefaultPrimitiveValue(entity, field); } } } } // ====================== 关联辅助方法 ====================== private static void associateSingleReference( SynchronizableEntity entity, Field field ) throws IllegalAccessException { SynchronizableEntity ref = (SynchronizableEntity) field.get(entity); Class<?> targetType = field.getType(); // 查找目标实体 SynchronizableEntity target = findTargetEntity(ref, targetType); field.set(entity, target); } private static void associateReferenceList( SynchronizableEntity entity, Field field ) throws IllegalAccessException, ClassNotFoundException { // 获取列表泛型类型 Type genericType = field.getGenericType(); if (!(genericType instanceof ParameterizedType)) return; Class<?> itemType = Class.forName( ((ParameterizedType) genericType).getActualTypeArguments()[0].getTypeName() ); // 只处理实体列表 if (!SynchronizableEntity.class.isAssignableFrom(itemType)) return; List<SynchronizableEntity> refList = (List<SynchronizableEntity>) field.get(entity); if (refList == null) { refList = new ArrayList<>(); field.set(entity, refList); } // 清理空值并重建引用 refList.removeAll(Collections.singleton(null)); for (int i = 0; i < refList.size(); i++) { refList.set(i, findTargetEntity(refList.get(i), itemType)); } } /** 查找关联实体(优先匹配ID,找不到则使用预置对象) */ private static SynchronizableEntity findTargetEntity( SynchronizableEntity ref, Class<?> targetType ) { SynchronizedList<SynchronizableEntity> targetList = dataCollectionMap.get(targetType); if (targetList == null) return null; // 无效引用时返回预置对象 if (ref == null || ref.getId() == null || ref.getId() < 0) { return targetList.stream() .filter(SynchronizableEntity::isPreservedObject) .findFirst().orElse(null); } // 按ID查找目标实体 return targetList.stream() .filter(e -> ref.getId().equals(e.getId())) .findFirst() .orElseGet(() -> targetList.stream() // 找不到时回退到预置对象 .filter(SynchronizableEntity::isPreservedObject) .findFirst().orElse(null) ); } // ====================== 工具方法 ====================== /** 创建带默认值的实体实例(用作预置对象) */ public static <T> T createInstance(Class<T> clazz) { try { T instance = clazz.getDeclaredConstructor().newInstance(); // 设置基础字段默认值 for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); setDefaultFieldValue(instance, field); } // 设置特殊字段 clazz.getMethod("setId", Integer.class).invoke(instance, -1); clazz.getMethod("setState", SynchronizableEntity.SyncState.class) .invoke(instance, SynchronizableEntity.SyncState.PRESERVED); return instance; } catch (Exception e) { Log.e(“Data”, "创建实例失败: " + clazz.getName(), e); try { return clazz.newInstance(); // 回退创建 } catch (Exception ex) { throw new RuntimeException(“无法创建实例”, ex); } } } private static <T> void setDefaultFieldValue(T instance, Field field) throws IllegalAccessException { Class<?> type = field.getType(); if (type == String.class) field.set(instance, "无"); else if (type == Integer.class || type == int.class) field.set(instance, -1); else if (type == Double.class || type == double.class) field.set(instance, -1.0); else if (type == Boolean.class || type == boolean.class) field.set(instance, false); else if (type == Date.class) field.set(instance, new Date()); else if (SynchronizableEntity.class.isAssignableFrom(type)) { field.set(instance, getPreservedEntity((Class<?>) type)); } else if (List.class.isAssignableFrom(type)) { field.set(instance, new ArrayList<>()); } } private static SynchronizableEntity getPreservedEntity(Class<?> type) { return dataCollectionMap.get(type).stream() .filter(SynchronizableEntity::isPreservedObject) .findFirst().orElse(null); } private static void setDefaultPrimitiveValue( SynchronizableEntity entity, Field field ) throws IllegalAccessException { if (field.get(entity) != null) return; Class<?> type = field.getType(); if (type == String.class) field.set(entity, "无"); else if (type == Integer.class || type == int.class) field.set(entity, -1); else if (type == Double.class || type == double.class) field.set(entity, -1.0); else if (type == Boolean.class || type == boolean.class) field.set(entity, false); else if (type == Date.class) field.set(entity, new Date()); } /** 主线程安全回调 */ private static void safeCallback(LoadDataCallback callback, boolean success) { new Handler(Looper.getMainLooper()).post(() -> { if (callback == null) return; if (success) callback.onSuccess(); else callback.onFailure(); }); } /** 设置所有实体同步状态 */ private static void setAllEntitiesState(SynchronizableEntity.SyncState state) { dataCollectionMap.values().forEach(list -> list.forEach(entity -> { if (entity != null) entity.setState(state); }) ); } public static String exportToJson() { ExportData exportData = new ExportData(); exportData.bancais = new ArrayList<>(bancais); exportData.caizhis = new ArrayList<>(caizhis); // 初始化其他列表… Gson gson = new GsonBuilder() .setPrettyPrinting() .registerTypeAdapter(SynchronizableEntity.class, new EntitySerializer()) .create(); return gson.toJson(exportData); } public static void importFromJson(String json, Context context) { Gson gson = new GsonBuilder() .registerTypeAdapter(SynchronizableEntity.class, new EntityDeserializer()) .create(); Type exportType = new TypeToken<ExportData>(){}.getType(); ExportData importData = gson.fromJson(json, exportType); // 更新数据列表 updateList(bancais, importData.bancais); updateList(caizhis, importData.caizhis); // 更新其他列表... automaticAssociation(); setAllEntitiesState(SynchronizableEntity.SyncState.MODIFIED); // 保存到SharedPreferences saveToPreferences(context, json); } private static void saveToPreferences(Context context, String json) { SharedPreferences prefs = context.getSharedPreferences(“DataStore”, Context.MODE_PRIVATE); prefs.edit().putString(“jsonData”, json).apply(); } public static void loadFromPreferences(Context context) { SharedPreferences prefs = context.getSharedPreferences(“DataStore”, Context.MODE_PRIVATE); String json = prefs.getString(“jsonData”, null); if (json != null) { importFromJson(json, context); } } // ====================== 内部类/接口 ====================== public interface LoadDataCallback { void onSuccess(); void onFailure(); } /** JSON响应数据结构 */ public static class AllDataResponse { public List<Bancai> bancais; public List<Caizhi> caizhis; public List<Mupi> mupis; public List<Chanpin> chanpins; public List<Chanpin_Zujian> chanpin_zujians; public List<Dingdan> dingdans; public List<Dingdan_Chanpin> dingdan_chanpins; public List<Dingdan_chanpin_zujian> Dingdan_chanpin_zujians; public List<Kucun> kucuns; public List<Zujian> zujians; public List<User> users; public List<Jinhuo> jinhuos; } // 在Data.java中添加 public static class ExportData { public List<Bancai> bancais; public List<Caizhi> caizhis; public List<Mupi> mupis; public List<Chanpin> chanpins; public List<Chanpin_Zujian> chanpin_zujians; public List<Dingdan> dingdans; public List<Dingdan_Chanpin> dingdan_chanpins; public List<Dingdan_chanpin_zujian> Dingdan_chanpin_zujians; public List<Kucun> kucuns; public List<Zujian> zujians; public List<User> users; public List<Jinhuo> jinhuos; } private static class EntityDeserializer implements JsonDeserializer<SynchronizableEntity> { @Override public SynchronizableEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject obj = json.getAsJsonObject(); String entityType = obj.get(“entityType”).getAsString(); Integer id = obj.get(“id”).getAsInt(); // 创建临时实体(只包含ID) try { Class<?> clazz = Class.forName("com.example.kucun2.entity." + entityType); SynchronizableEntity entity = (SynchronizableEntity) clazz.newInstance(); entity.setId(id); return entity; } catch (Exception e) { return null; } } } private static class EntitySerializer implements JsonSerializer<SynchronizableEntity> { @Override public JsonElement serialize(SynchronizableEntity src, Type typeOfSrc, JsonSerializationContext context) { JsonObject obj = new JsonObject(); obj.addProperty(“id”, src.getId()); obj.addProperty(“entityType”, src.getClass().getSimpleName()); return obj; } } }
最新发布
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值