Home [TIL] 2022-11-02
Post
Cancel

[TIL] 2022-11-02

๐Ÿ˜ฒ ์˜ค๋Š˜์˜ ๊ฐœ๋ฐœ ์ผ์ง€


  • async์™€ withContext๋Š” ๊ธฐ๋Šฅ๋ฉด์—์„œ๋Š” ๋™์ผํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์กฐ๊ธˆ์˜ ์ฐจ์ด์ ์ด ์žˆ๋‹ค.

    • async๋Š” ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ณ  withContext๋Š” ๊ทธ๋Ÿฌ์ง€ ๋ชปํ•œ๋‹ค.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    suspend fun exampleSuspend() {
        withContext(Dispatchers.IO) {
            delay(1000)
        }
    
        withContext(Dispatchers.IO) {
            delay(1000)
        }
    
        withContext(Dispatchers.IO) {
            delay(1000)
        }
    }
    

    withContext๋Š” ์œ„ ์ฝ”๋“œ๊ฐ€ ๋ชจ๋‘ ์‹คํ–‰๋˜๋Š”๋ฐ 3์ดˆ๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    suspend fun test() {
        CoroutineScope(Dispatchers.IO).async {
            delay(1000)
        }
    
        CoroutineScope(Dispatchers.IO).async {
            delay(1000)
        }
    
        CoroutineScope(Dispatchers.IO).async {
            delay(1000)
        }
    }
    

    async๋Š” ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ธฐ์— ์ฝ”๋“œ๊ฐ€ ๋ชจ๋‘ ์‹คํ–‰๋˜๋Š”๋ฐ๊นŒ์ง€ 1์ดˆ๊ฐ€ ๊ฑธ๋ฆฐ๋‹ค.

    • async๋Š” try-catch๋ฌธ์ด ์˜ˆ์™ธ๋ฅผ ์žก์ง€ ๋ชปํ•˜์ง€๋งŒ withContext๋Š” ์˜ˆ์™ธ๋ฅผ ์žก์„ ์ˆ˜ ์žˆ๋‹ค.

      async๋Š” exception์ด ๋ฐœ์ƒํ•˜๋Š” ๋ถ€๋ถ„์—์„œ try-catch๋ฌธ์„ ์‚ฌ์šฉํ•ด๋„ ํ•ด๋‹น ์ฝ”๋ฃจํ‹ด์„ ์‹คํ–‰์‹œํ‚จ CoroutineScope์—๋„ ์˜ˆ์™ธ๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    CoroutineScope(Dispatchers.IO).launch { // 2. ์—ฌ๊ธฐ๊นŒ์ง€ ์—๋Ÿฌ๊ฐ€ ์ „๋‹ฌ๋œ๋‹ค.
            try {
                async {
                    throw Exception() // 1. ์—ฌ๊ธฐ์„œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ฉด
                }
            } catch (e: Exception) {
                println("error catch")
            }
        }
    

    ๊ทธ๋ž˜์„œ ์œ„ ์ฝ”๋“œ๋Š” ์‹คํ–‰์‹œํ‚ค์ง€ ๋ชปํ•˜๊ณ  ์•ฑ์ด ์ฃฝ์–ด๋ฒ„๋ฆฐ๋‹ค.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    CoroutineScope(Dispatchers.IO).launch {
            try {
                withContext(Dispatchers.Default) {
                    throw Exception()
                }
            } catch (e: Exception) {
                println("error catch")
            }
        }
    

    ๋ฐ˜๋ฉด withContext๋Š” ์˜ˆ์™ธ๊ฐ€ ์ „๋‹ฌ๋˜์ง€ ์•Š์•„ Exception์„ ์žก์•„๋‚ธ๋‹ค.

  • ์ฝ”ํ‹€๋ฆฐ์—๋Š” invoke๋ผ๋Š” ์—ฐ์‚ฐ์ž๊ฐ€ ์กด์žฌํ•œ๋‹ค.

    invoke ์—ฐ์‚ฐ์ž๋Š” ์ด๋ฆ„ ์—†์ด ํ˜ธ์ถœ ๋  ์ˆ˜ ์žˆ๋‹ค.

    1
    2
    3
    4
    5
    6
    7
    
    object MyFunction {
    operator fun invoke(str: String): String {
        return str.toUpperCase() // ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์คŒ
        }
    }
    
    MyFunction.invoke("hello")
    
  • flow ๋„ˆ๋ฌด ์–ด๋ ต๋‹คใ… ใ… ใ… 

    flow๋Š” ์™œ ์“ฐ๋Š”๊ฑธใ„ฒrโ€ฆ.

  • ์•ˆ๋“œ๋กœ์ด๋“œ ๋‚ด๋ถ€ ํ•จ์ˆ˜์— ์ด๋ฉ”์ผ ํ˜•์‹์„ ์ฒดํฌํ•ด์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

    ์•„์ด๋””๋ฅผ ์ด๋ฉ”์ผ ํ˜•์‹์œผ๋กœ ๋ฐ›์•„์•ผํ•ด์„œ ์ •๊ทœ์‹์„ ๊ฒ€์ƒ‰ํ•˜๋˜ ์ค‘ ์•Œ๊ฒŒ ๋๋‹ค.

    1
    2
    3
    4
    5
    6
    
    private fun isCorrectEmailPattern(name: String): Boolean {
        if (name.isEmpty())
            return false
    
        return Patterns.EMAIL_ADDRESS.matcher(name).matches()
    }
    
This post is licensed under CC BY 4.0 by the author.

-

[Android] ๊ฐค๋Ÿฌ๋ฆฌ์—์„œ ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ