Hello, I’m trying to implement this abstract func...
# multiplatform
m
Hello, I’m trying to implement this abstract function in my shared code to swift
Copy code
abstract class FacebookAnalytics {

  @NativeCoroutines
  abstract suspend fun logEvent(event: String, properties: Map<String, String>)
}
Copy code
public class TestFacebookAnalyticsWriter : FacebookAnalytics {
   
  public override func logEvent(event: String, properties: [String : String]) -> (@escaping (KotlinUnit, KotlinUnit) -> KotlinUnit, @escaping (Error, KotlinUnit) -> KotlinUnit, @escaping (Error, KotlinUnit) -> KotlinUnit) -> () -> KotlinUnit {
    print("FacebookAnalytics: logEvent()")
    AppEvents.shared.logEvent(AppEvents.Name(event), parameters: properties)
  } 
}
And then I want to call this Analytics as such:
Copy code
let analytics = TestFacebookAnalyticsWriter()

      try! await asyncFunction(for: analytics.logEvent(event: "", properties: ["" : ""]))
But I don’t know what to return from the TestFacebookAnalyticsWriter logEvent function
Screenshot 2023-08-11 at 12.18.49 pm.png
r
Hi! Unfortunately KMP-NativeCoroutines doesn't support such Swift to Kotlin cases (yet). Please keep an eye on this issue: https://github.com/rickclephas/KMP-NativeCoroutines/issues/42
m
Got it, thank you!
Also, the other thing is even if I don't use
asyncFunction
I should still be able to override the logEvent method right?
r
I am not sure if you can, but even if you can override the
logEvent
function it won’t have the desired effect as long as the function is annotated with
@NativeCoroutines
. When you override the
logEvent
function from your Swift application you would actually be overriding the generated extension from KMP-NativeCoroutines.
You could try to remove the annotation and specify the extension yourself, I guess that should work:
Copy code
fun FacebookAnalytics.logEventNative(event: String, properties: Map<String, String>) = nativeSuspend {
    logEvent(event, properties)
}
Which should allow you to override
logEvent
and use
logEventNative
with the
asyncFunction(for:)
helper. However please keep in mind that just overriding
logEvent
won’t support cancellation of the suspend/async function.