LiveData

Understanding LiveData and its implementation

Introduction to LiveData

LiveData Android Architecture Components का एक observable data holder class है। यह lifecycle-aware होता है और UI components को automatically update करता है जब data change होता है।

LiveData is an observable data holder class in Android Architecture Components. It is lifecycle-aware and automatically updates UI components when data changes.

Key Features (मुख्य विशेषताएं):

  • LiveData lifecycle-aware होता है
  • LiveData is lifecycle-aware
  • LiveData UI को automatically update करता है
  • LiveData automatically updates UI
  • LiveData memory leaks को prevent करता है
  • LiveData prevents memory leaks
  • LiveData configuration changes को handle करता है
  • LiveData handles configuration changes

Types of LiveData (लाइवडेटा के प्रकार)

LiveData के main types:

Main types of LiveData:

Type (प्रकार) Description (विवरण)
MutableLiveData Data को modify करने की permission देता है
MutableLiveData Allows modification of data
MediatorLiveData Multiple LiveData sources को observe करता है
MediatorLiveData Observes multiple LiveData sources
Transformations LiveData को transform करने के लिए utility methods provide करता है
Transformations Provides utility methods for transforming LiveData

Implementation Example (इम्प्लीमेंटेशन उदाहरण)

LiveData को implement करने का example:

Example of implementing LiveData:

// ViewModel with LiveData
class UserViewModel : ViewModel() {
    private val _user = MutableLiveData()
    val user: LiveData = _user

    private val _loading = MutableLiveData()
    val loading: LiveData = _loading

    fun loadUser(userId: String) {
        _loading.value = true
        viewModelScope.launch {
            try {
                _user.value = repository.getUser(userId)
            } finally {
                _loading.value = false
            }
        }
    }
}

// Activity/Fragment implementation
class UserActivity : AppCompatActivity() {
    private lateinit var viewModel: UserViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = ViewModelProvider(this).get(UserViewModel::class.java)
        
        viewModel.user.observe(this) { user ->
            // Update UI with user data
        }
        
        viewModel.loading.observe(this) { isLoading ->
            // Show/hide loading indicator
        }
        
        viewModel.loadUser("123")
    }
}

LiveData Transformations (लाइवडेटा ट्रांसफॉर्मेशन्स)

LiveData transformations के examples:

Examples of LiveData transformations:

// Map transformation
val userName: LiveData = Transformations.map(user) { user ->
    "${user.firstName} ${user.lastName}"
}

// SwitchMap transformation
val userPosts: LiveData> = Transformations.switchMap(user) { user ->
    repository.getPosts(user.id)
}

// MediatorLiveData example
val combinedData = MediatorLiveData().apply {
    addSource(user) { user ->
        value = CombinedData(user, posts.value)
    }
    addSource(posts) { posts ->
        value = CombinedData(user.value, posts)
    }
}

Best Practices (सर्वोत्तम प्रथाएं)

LiveData के साथ काम करने के best practices:

Best practices for working with LiveData:

  • Data Exposure: MutableLiveData को private रखें और public LiveData expose करें
  • Data Exposure: Keep MutableLiveData private and expose public LiveData
  • Transformations: Complex data transformations के लिए Transformations का use करें
  • Transformations: Use Transformations for complex data transformations
  • Error Handling: Error states को LiveData के through handle करें
  • Error Handling: Handle error states through LiveData
  • Testing: LiveData को easily testable बनाएं
  • Testing: Make LiveData easily testable