Why my MoshiConverterFactory is slower then GsonConverterFactory?

63 Views Asked by At

when I post an api, it took too many time to response

Here is my retrofit builder:

fun provideRetrofit(baseUrl: String): Retrofit = Retrofit.Builder()
        .client(provideOkhttpClient())
        .addConverterFactory(MoshiConverterFactory.create(provideMoshi()))
        .baseUrl(baseUrl)
        .build()

private fun provideMoshi(): Moshi = Moshi.Builder()
        .add(KotlinJsonAdapterFactory())
        .build()

And this is my service & respositoy:

@POST(ApiPath.userProfile)
suspend fun getUserProfile(): Response<BaseProfileEntity>


fun getUserProfile(selfId: String, source: RefreshSource) = flow {
        emit(ApiResult.Loading(true))
        val db = DBManager.getInstance()
        if (source == RefreshSource.LOCAL) {
            val profile = db.queryUser(selfId)
            profile?.let {
                emit(ApiResult.Success(it))
            }
        } else {
            val response = contactPersonService.getUserProfile()
            response.body()?.let {
                db.insertUserAndFriends(it)
                val localUserProfile = db.queryUser(selfId)
                localUserProfile.homePagePics = it.homePagePics
                emit(ApiResult.Success(localUserProfile))
            }
        }
    }.flowOn(Dispatchers.IO).catch { e ->
        Log.e(javaClass.simpleName, e.message ?: "")
        emit(ApiResult.Failure(e.message ?: ""))
    }

And this is my viewModel:

fun getSelfProfile(source: RefreshSource) = viewModelScope.launch {
        val selfId = TokenPref.getInstance(application).userId
        contactRepository.getUserProfile(selfId, source).collect {
            when (it) {
                is ApiResult.Loading -> loading.postValue(it.isLoading)
                is ApiResult.Failure -> errorMessage.postValue(it.errorMessage)
                is ApiResult.Success -> selfProfileData.postValue(it.data)
            }
        }
    }

when I uses Moshi, it spent too many time getSelfProfile spent time: 1762

Gosn is faster then Moshi getSelfProfile spent time: 550

Did I miss something to do ?

I try to uses @JsonClass(generateAdapter = true) & @Json in my model, but still not working, or just post a request and receive a response with doing nothing, still not working

UPDATE: Here is simple JSON body & Entity

@JsonClass(generateAdapter = true)
data class BaseProfileEntity(
    @Json(name = "id") var id: String? = null,
    @Json(name = "nickName") val nickName: String? = null,
    @Json(name = "userType") val userType: UserType = UserType.CONTACT,
    @Json(name = "openId") val openId: String? = null,
    @Json(name = "serviceNumberIds") val serviceNumberIds: List<String>? = null,
    @Json(name = "status") val status: String? = null,
    @Json(name = "mobile") val mobile: Long = 0,
    @Json(name = "bindAile") val bindAile: Boolean,
    @Json(name = "joinAile") val joinAile: Boolean,
    @Json(name = "collectInfo") val collectInfo: Boolean,
    @Json(name = "inviterOpenId") val inviterOpenId: String,
    @Json(name = "tenantCode") val tenantCode: String,
    @Json(name = "invitationCreateTime") val invitationCreateTime: Long,
    @Json(name = "inviterAccountId") val inviterAccountId: String,
    @Json(name = "password") val password: String,
    @Json(name = "countryCode") val countryCode: String,
    @Json(name = "from") val from: String,
    @Json(name = "invitationUpdateTime") val invitationUpdateTime: Long,
    @Json(name = "invitationCode") val invitationCode: String,
    @Json(name = "userBusinessCards") val userBusinessCards: List<Any>,
    @Json(name = "systemRoomId") val systemRoomId: String,
    @Json(name = "userNO") val userNO: String,
    @Json(name = "updateTime") val updateTime: Long,
    @Json(name = "createTime") val createTime: Long,
    @Json(name = "joinType") val joinType: String,
    @Json(name = "tenantId") val tenantId: String,
    @Json(name = "personalSet") val personalSet: String,
    @Json(name = "personRoomId") val personRoomId: String,
    @Json(name = "homePagePics") val homePagePics: List<UserProfileEntity.HomePagePic>,
)
{
    "joinAile": true,
    "_header_": {
        "success": true,
        "timeCost": 8
    },
    "openId": "654c7071633861f5b262cb84",
    "bindAile": false,
    "collectInfo": false,
    "inviterOpenId": "654b3a92633861f5b262c828",
    "tenantCode": "20211216-0001",
    "invitationCreateTime": 1699508316220,
    "inviterAccountId": "654b3a56633861f5b262c81d",
    "password": "PU8r8H3BvjiyDNbkaUmhBx+dDj0=",
    "countryCode": "+886",
    "from": "APP",
    "id": "934debdd-d905-4507-b32e-1f1598c25e5c",
    "invitationUpdateTime": 1699508337461,
    "invitationCode": "915694",
    "userBusinessCards": [],
    "systemRoomId": "725ded82-d495-4815-9b05-95509db94e50",
    "nickName": " tyy",
    "userNO": "9927853427",
    "mobile": "0909090900",
    "updateTime": 1701312343617,
    "serviceNumberIds": [
        "18aff361-d100-01d4-6591-027bdd6dc9f8",
        "188bdf95-9ac0-06d9-60c4-027bdd6dc9f8",
        "1862f849-38a0-0490-6bea-027bdd6dc9f8",
        "1857faa3-3260-0410-3016-027bdd6dc9f8",
        "184603de-dc20-081e-4e03-027bdd6dc9f8",
        "18460322-3370-081e-4e03-027bdd6dc9f8",
        "184602dd-81b0-081e-4e03-027bdd6dc9f8",
        "18455140-f6f0-081e-4e03-027bdd6dc9f8",
        "18450e18-cec0-081e-4e03-027bdd6dc9f8",
        "18450e0a-1c70-081e-4e03-027bdd6dc9f8",
        "dd2fdebb-1565-4850-addd-2698882d3e7a",
        "17dc1550-f270-0b75-51b5-060b79b7ea50",
        "18bc681b-fb00-0612-49a9-027bdd6dc9f8",
        "184517d4-0510-081e-4e03-027bdd6dc9f8",
        "184517ae-2600-081e-4e03-027bdd6dc9f8"
    ],
    "createTime": 1699508337486,
    "joinType": "Invite",
    "tenantId": "17dc1550-db80-0363-0c64-068cc5ce62c0",
    "personalSet": "{\"aileClientConfig\":{\"captureKey1\":\"Ctrl+Shift+A\",\"captureKey2\":\"Ctrl+Shift+Alt+A\",\"hidekey\":\"Ctrl+Alt+C\",\"sendKey\":\"Enter\",\"useTwoRow\":0,\"useTwoRowInBusiness\":0,\"useAutoMultiView\":1,\"useInnerBrowser\":1,\"fontSize\":1,\"call\":false,\"messagePop\":false,\"messageS\":\"01_s\",\"ringS\":\"11_c\",\"ringBackS\":\"13_c\",\"language\":\"zh_TW\",\"autoService\":true,\"autoServiceNumber\":1}}",
    "personRoomId": "f7066a1c-d110-4245-b8bd-88c8817d6bb9",
    "userType": "employee",
    "homePagePics": [],
    "status": "Enable"
}
0

There are 0 best solutions below