4

I am trying to build a simple Kotlin Multiplatform app that calls to the internet to fetch some Strings from the internet with ktor. I took some functions from Kotlin conference app which I compiled and it works fine on both Android and iOS.

However, in my sample app, it only works on Android, but on iOS it returns

kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen <object>@c422ffe8

Here is the GitHub repository and below is my code:

// src/commonMain/CoroutinePresenter.kt

open class CoroutinePresenter(
    private val mainContext: CoroutineContext, // TODO: Use Dispatchers.Main instead when it will be supported on iOS
    private val baseView: BaseView
): CoroutineScope {

    private val job = Job()
    private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
        baseView.showError(throwable)
    }

    override val coroutineContext: CoroutineContext
        get() = mainContext + job + exceptionHandler

    open fun onDestroy() {
        job.cancel()
    }
}

--

// src/commonMain/SamplePresenter.kt

class SamplePresenter(
    val uiContext: CoroutineContext,
    baseView: BaseView,
    val sampleView: SampleView
) : CoroutinePresenter(uiContext, baseView) {
    private val client = HttpClient()

    fun callSimpleApi() {
        try {
            GlobalScope.launch(uiContext) {
                getToolString()
            }
        } catch (e: Exception) {
            sampleView.returnString(e.toString())
        }
    }

    suspend fun getToolString() = client.get<String> {
        url("https://tools.ietf.org/rfc/rfc1866.txt")
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}

--

// src/iosMain/SampleIos.kt
object MainLoopDispatcher: CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        NSRunLoop.mainRunLoop().performBlock {
            block.run()
        }
    }
}

--

// iosApp/iosApp/ViewController.swift
import app

class ViewController: UIViewController, SampleView, BaseView {
    private lazy var presenter: SamplePresenter = { SamplePresenter(
        uiContext: MainLoopDispatcher(),
        baseView: self,
        sampleView: self
        )
    }()
    @IBOutlet weak var label: UILabel!

    func showError(error: KotlinThrowable) {
        print(error.message)
    }

    func returnString(result: String) {
        label.text = result
        print(result)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        print("helo")
        presenter.callSimpleApi()
    }
}

How recursive function control flow change for static variable?

I am trying to learn some stuff about recursive function and I try to run this

void recurse()
{
   static int c = 1;
   if(c > 5)
      return ;
   printf("%d ",c);
   c = c + 1;
   recurse();
   printf("%d ",c);
}

int main()
{
   recurse();
   return 0;
}    

I am expecting that it will give me an output like this as similar to any local variable called in a recursive function.

1 2 3 4 5 5 4 3 2 1

But compiler gives me an output like this

1 2 3 4 5 6 6 6 6 6

I can't understand why this kind of output comes. Can anyone explain?

4

1 に答える 1