简体   繁体   中英

Im using very large data. And my app crashes. I need a solution that dont consumes a lot of memory and its still fast

So, im using android studio to develop and app, and the most important data, have arrays with like 100 000 objects. What should i use to store this data. I get this data from a get request when user logs in. And i need to store that that while user is on the app. Ive tried using a global variable and then i tried with bundle&serializable. Booth didnt work, i had a problem out of memory.

What should i use instead?

My object type is somethin like this, and i need to store up to 200 000 of them.

String ID;
String Name;
String PVM;
String FinalName;
String SN;
Defect defect;

where defect is another class object.

Room Database is what you want, it is sqlite underneath, and very simple to use.

@Entity
data class MyModel(
@PrimaryKey
val ID: String,
val name: String,.....) {

@Dao
interface DaoMyModel {
//You put all methods to handle data here
// Some examples

@Insert
fun insertOne(item: MyModel)
@Insert
fun insertMultiple(items: List<MyModel>)

//Get all
@Query("SELECT * FROM MyModel")
fun getAll() : List<MyModel>

//Observe all with kotlin Flow
@Query("SELECT * FROM MyModel")
fun flowAll() : Flow<List<MyModel>>

}
} 


//Your database class
@Database(entities = [MyModel::class], version = 1)
abstract class MyDatabase: RoomDatabase(){
abstract fun getMyModelDao() : MyModel.DaoMyModel

}

//Db Init

fun provideDatabase(applicationContext: Context) = Room.databaseBuilder(
            applicationContext, MyDb::class.java , "MyDatabaseName")
        .build()



//handling the data

//get the database
val myDatabase = provideDatabase(context)
//get dao
val myModelDao = myDatabase.getMyModelDao()

//insert new item
myModelDao.insert(model)

I would suggest that you go tho with this 1 hour codelab tutorial https://developer.android.com/codelabs/android-room-with-a-view-kotlin#0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM