I have very big boolean array data.
How can i save memory and load this data quickly in android?
I have very big boolean array data.
How can i save memory and load this data quickly in android?
150 items of boolean are small amount of data for conventional mobile devices. You can save more than 1000 items at once within 1 second.
Method 1
Realm already supports byte[]
as a datatype. You can use it by converting boolean array into byte array.
Method 2
As you know, boolean
is just 1-bit(binary) data. There's lots of case to handle such bits, but for me, I love to handle it in naive manner.
Let say you have 160 items of boolean.
true
, false
, ..., true
, false
1
or 0
: 1010101....1010110
1 hex string contains 4 bits.
1010 1110 0110 1100
can be expressed into AE6C
Likewise, 160
bits can be converted into 160/4=40
strings.
Just save 40 length strings to Realm or SharedPreference
.
Want to load? no problem. Convert 40 length strings into 160 length boolean array.
I think it could consume less than 100ms as those converting process is pretty fast in modern mobile CPUs.
Here are 4 possible soluzions:
1.) You can serialize an Object and save it to a file.
2.) You can use Gson library and convert the object to JSON and save it to a file or in preferences.
3.) You can use Realm library which is fast. They even claim that on some operations they are faster than SQLite.
For solution 1 see https://stackoverflow.com/a/33896724/1502079
For solution 2 see https://stackoverflow.com/a/16436363/1502079