What is inline class in Kotlin and when do we need one? Provide an example. What is inline class in Kotlin and when do we need one? Provide an example. Senior Answer Sometimes it is necessary for business logic to create a wrapper around some type. However, it introduces runtime overhead due to additional heap allocations. Moreover, if the wrapped type is primitive, the performance hit is terrible, because primitive types are usually heavily optimized by the runtime. Inline classes provide us with a way to wrap a type, thus adding functionality and creating a new type by itself. As opposed to regular (non-inlined) wrappers, they will benefit from improved performance. This happens because the data is inlined into its usages, and object instantiation is skipped in the resulting compiled code. inline class Name ( val s : String ) { val length : Int get ( ) = s . length fun greet ( ) { println ( "Hello, $s " ) } } fun ma...
Building scalable Android apps requires clean architecture—and one of the best ways to manage your UI and logic is by combining MVVM with Jetpack Compose . However, API calls can get messy when not structured properly. In this post, I’ll show you how to build a reusable, clean API call structure using MVVM that works for any screen in your app. We'll use: ViewModel to manage UI-related data Repository for business logic and network State management using mutableStateOf Retrofit for networking This is not tied to any specific screen (like sliders, onboarding, etc.)—you can plug in your own model and API. Why a Reusable Pattern? Apps often repeat the same logic: Show loader Fetch data Handle errors Update UI Instead of repeating it everywhere, we'll abstract it once and just extend or plug in what’s needed per screen. 1. Define a Generic API Result Wrapper This wrapper lets us handle success, loading, and error states in a consisten...
When you say your app works offline… does it really? A surprising number of Android developers proudly add “offline-first” to their app descriptions. And sure, the app opens without internet. Maybe it even loads a few screens. But dig a little deeper and it becomes clear: most offline-first implementations fall apart fast. Why? Because building an offline-first experience isn’t about just storing data locally. It’s about doing it intentionally , with the right structure, cleanup, and flow. And the culprit behind most of the silent issues? Room Database. Not because Room is bad — but because it's misunderstood. Let’s walk through some of the most common mistakes developers make when using Room in offline-first apps — and what to do instead. Mistake 1: Saving the Entire API Response into Room Many devs take the raw JSON response from their API and store it directly into Room entities — every field, every nested object, even things the app never uses. Why it’s a probl...
Comments
Post a Comment