3
\$\begingroup\$

I have a Db table:

'aid' INTEGER,
'tid' INTEGER,

Which represents connection between activity and task, so multiple tasks could be connected to one activity using their ids:

aid tid
1   5
1   9
1   11

So activity with id=1 is connected with tasks with ids=5,9,11.

I load all this data during the launch of the app and put it in SparseArray, where keys of this array are activity ids (aid), and values of this array are arrays with task ids (long[]).

The function looks like this:

public final void setRawCursor(final Cursor cursor) {
        if (cursor != null) {
            long aid, aidPrev = INVALID;
            final FastLongKeySparseArray<long[]> map = mTaskPinMap.getMap();
            final FastLongStack stack = new FastLongStack(25);
            try {
                while (cursor.moveToNext()) {
                    aid = cursor.getLong(TaskPin.AID_);
                    if (aidPrev != INVALID
                            && aid != aidPrev) {
                        map.append(aidPrev, stack.toArray(new long[stack.size()]));
                        stack.clear();
                    }
                    stack.push(cursor.getLong(TaskPin.TID_));
                    aidPrev = aid;
                }
                if (aidPrev != INVALID) map.append(aidPrev, stack.toArray(new long[stack.size()]));
            } finally {
                cursor.close();
            }
        }
        mIsDataReady = true;
    }

My purpose to save as much memories as possible, and do not make pressure on GC. I use long[] instead of ArrayList because this data could be modified in rear cases, so it is not hard to add new ids to long[] by coping the array, and I think to save memory with such turn.

What do you think? Is it possible to eliminate put to the map after the loop?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.