Coroutines
Understanding Coroutines and their implementation in Android
Introduction to Coroutines
Coroutines Kotlin का एक powerful feature है जो asynchronous programming को simple और efficient बनाता है। यह background tasks को handle करने का एक modern way provide करता है।
Coroutines are a powerful feature of Kotlin that makes asynchronous programming simple and efficient. They provide a modern way to handle background tasks.
Key Features (मुख्य विशेषताएं):
- Coroutines lightweight होते हैं
- Coroutines are lightweight
- Coroutines structured concurrency provide करते हैं
- Coroutines provide structured concurrency
- Coroutines cancellation support करते हैं
- Coroutines support cancellation
- Coroutines error handling provide करते हैं
- Coroutines provide error handling
Coroutine Components (कोरोटीन कंपोनेंट्स)
Coroutines के main components:
Main components of Coroutines:
Component (कंपोनेंट) | Description (विवरण) |
---|---|
CoroutineScope | Coroutines का lifecycle manage करता है |
CoroutineScope | Manages the lifecycle of coroutines |
CoroutineContext | Coroutine execution के लिए context provide करता है |
CoroutineContext | Provides context for coroutine execution |
Dispatcher | Coroutine execution के लिए thread determine करता है |
Dispatcher | Determines thread for coroutine execution |
Implementation Example (इम्प्लीमेंटेशन उदाहरण)
Coroutines को implement करने का example:
Example of implementing Coroutines:
// Basic coroutine example
class UserRepository {
suspend fun fetchUser(userId: String): User {
return withContext(Dispatchers.IO) {
// Simulate network call
delay(1000)
User(userId, "John Doe")
}
}
}
// ViewModel with coroutines
class UserViewModel : ViewModel() {
private val repository = UserRepository()
private val _user = MutableLiveData()
val user: LiveData = _user
fun loadUser(userId: String) {
viewModelScope.launch {
try {
_user.value = repository.fetchUser(userId)
} catch (e: Exception) {
// Handle error
}
}
}
}
// Activity 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.loadUser("123")
}
}
Coroutine Dispatchers (कोरोटीन डिस्पैचर्स)
Coroutine dispatchers के types:
Types of coroutine dispatchers:
// Different dispatchers
viewModelScope.launch(Dispatchers.Main) {
// UI operations
}
viewModelScope.launch(Dispatchers.IO) {
// Network or database operations
}
viewModelScope.launch(Dispatchers.Default) {
// CPU-intensive operations
}
viewModelScope.launch(Dispatchers.Unconfined) {
// Not confined to any specific thread
}
// Switching dispatchers
viewModelScope.launch {
// Start on Main thread
val user = withContext(Dispatchers.IO) {
// Switch to IO thread for network call
repository.fetchUser("123")
}
// Back on Main thread
updateUI(user)
}
Best Practices (सर्वोत्तम प्रथाएं)
Coroutines के साथ काम करने के best practices:
Best practices for working with Coroutines: