Skip to content

Commit 9ff49c5

Browse files
author
Yury Shubin
committed
optimization #2
1 parent be24d26 commit 9ff49c5

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/com/activeandroid/Model.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import java.lang.reflect.Field;
3131
import java.util.ArrayList;
3232
import java.util.Arrays;
33+
import java.util.HashMap;
3334
import java.util.List;
35+
import java.util.Map;
3436

3537
@SuppressWarnings("unchecked")
3638
public abstract class Model {
@@ -46,6 +48,8 @@ public abstract class Model {
4648

4749
private final TableInfo mTableInfo;
4850
private final String idName;
51+
private static Map<String, List<Integer>> columnIndexesCache = new HashMap<String, List<Integer>>();
52+
4953
//////////////////////////////////////////////////////////////////////////////////////
5054
// CONSTRUCTORS
5155
//////////////////////////////////////////////////////////////////////////////////////
@@ -185,10 +189,23 @@ public final void loadFromCursor(Cursor cursor) {
185189
* when the cursor have multiple columns with same name obtained from join tables.
186190
*/
187191
List<String> columnsOrdered = new ArrayList<String>(Arrays.asList(cursor.getColumnNames()));
192+
List<Integer> columnIndexes = columnIndexesCache.get(mTableInfo.getTableName());
193+
if (columnIndexes == null) {
194+
columnIndexes = new ArrayList<Integer>();
195+
columnIndexesCache.put(mTableInfo.getTableName(), columnIndexes);
196+
}
197+
int counter = 0;
188198
for (Field field : mTableInfo.getFields()) {
189-
final String fieldName = mTableInfo.getColumnName(field);
190199
Class<?> fieldType = field.getType();
191-
final int columnIndex = columnsOrdered.indexOf(fieldName);
200+
201+
final int columnIndex;
202+
if (columnIndexes.size() <= counter) {
203+
String fieldName = mTableInfo.getColumnName(field);
204+
columnIndex = columnsOrdered.indexOf(fieldName);
205+
columnIndexes.add(columnIndex);
206+
} else {
207+
columnIndex = columnIndexes.get(counter);
208+
}
192209

193210
if (columnIndex < 0) {
194211
continue;
@@ -268,6 +285,8 @@ else if (ReflectionUtils.isSubclassOf(fieldType, Enum.class)) {
268285
if (value != null) {
269286
field.set(this, value);
270287
}
288+
289+
counter++;
271290
}
272291
catch (IllegalArgumentException e) {
273292
Log.e(e.getClass().getName(), e);

0 commit comments

Comments
 (0)