skip to Main Content

I want to save user data
e.g- Id : Long ,
name : String,
url : String, .

There are more than 50k entries.

Which is better saving data inside a data class or JSON file ?

Just want to know in terms of performance, apk size. is there any better or both are the same?

this is offline Appliaction

@Immutable
data class Person(
    val id :Long,
    val name :String,
    val imageUrl:String,
    )


fun getPersonList(): List<Person> {
    return listOf(
        Person(
            id = 1L,
            name = "ram",
            imageUrl = "https://i.kitroka.com/564x/e4/51/07/e45107f14a7c5c.jpg",
        ),
        Person(
            id = 2L,
            name = "sam",
            imageUrl = "https://i.kitroka.com/564x/e4/51/07/e451077c5c.jpg",
        ),
        Person(
            id = 3L,
            name = "jam",
            imageUrl = "https://i.kitroka.com/564x/e4/51/07/e451467c5c.jpg",
        ),
        Person(
            id = 4L,
            name = "k5am",
            imageUrl = "https://i.kitroka.com/564x/e4/51/07/e451467c5c.jpg",
        )
    )
}

2

Answers


  1. In an offline Android application where you need to save and manage a large dataset with more than 50,000 entries, it’s essential to consider the trade-offs between saving data inside a data class (e.g., using a Room database or some other data persistence library) and using a JSON file. Each approach has its own advantages and disadvantages, which affect performance and APK size.

    Data Class (e.g., Room Database):

    Advantages:

    Efficient query and retrieval: Databases are optimized for data retrieval, making it faster and more efficient to query specific records.

    Scalability: As your dataset grows, databases like Room can handle large datasets efficiently.

    Flexibility: You can structure your data with complex relationships and query it with SQL-like queries.

    Data integrity and consistency: Databases offer mechanisms for ensuring data consistency and integrity.

    Disadvantages:

    Slightly larger APK size: Using a database library like Room increases the APK size due to library dependencies.

    Initial setup: Setting up a database and defining a data model requires more code compared to a JSON file.

    JSON File:

    Advantages:

    Smaller APK size: Storing data in a JSON file may lead to a smaller APK size compared to including a database library.

    Simplicity: Managing data in a JSON file is relatively simple and doesn’t require additional dependencies.

    Easier to implement for simple use cases.

    Disadvantages:

    Slower data retrieval: Reading data from a JSON file is generally slower than querying a database, especially for large datasets.

    Limited querying capabilities: JSON files don’t support complex querying or data relationships without additional coding.

    Maintenance challenges: As your dataset grows, it may become harder to manage and maintain in a single JSON file.

    Conclusion:

    In terms of performance, a database (e.g., Room) is typically the better choice for managing large datasets, especially when you need efficient querying and scalability. It’s designed to handle large volumes of data while maintaining performance.

    However, if APK size is a significant concern and your data structure is relatively simple, using a JSON file might be a viable option. Just be aware that data retrieval and querying might not be as efficient, and you may need to handle data splitting or pagination to avoid loading the entire dataset into memory at once.

    Consider the specific needs of your application, such as performance, APK size, and complexity of data, to make an informed decision. In many cases, the balance between these factors will guide your choice.

    Login or Signup to reply.
  2. In terms of performance, I recommend storing big data in cloud storage services. (Firebase cloud storage , realm)

    At the point where you need to use it offline, you can cache with the room database.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search