https://kotlinlang.org logo
#compose-ios
Title
# compose-ios
s

Stefan Oltmann

10/05/2023, 7:06 AM
I'm investigating why my app suddenly freezes on iPhone 12 Mini and iPhone SE 3. CPU & Memory usage are looking good to me, but I get a "Hang Risk" warning in XCode. When it freezes CPU usage drops to 0%, memory is still allocating its 500 MB. Did someone ever see this? It points to
skiko/src/commonMain/kotlin/org/jetbrains/skia/Canvas.kt:469
Compose 1.5.2 uses Skiko 0.7.77. So this should be the code: https://github.com/JetBrains/skiko/blob/75555658e1735315df02a7b72d9b8b74ee031713/skiko/src/commonMain/kotlin/org/jetbrains/skia/Canvas.kt#L469 Unfortunately this tells me nothing about what I do wrong.
What I do is using a produceState() to load my Bitmap on a Background thread (
Dispatchers.Default
).
Copy code
@Composable
private fun produceBitmapPainter(
    photo: Photo?,
    imageLoader: ImageLoader
) = produceState<BitmapPainter?>(initialValue = null, photo) {

    value = withContext(Dispatchers.Default) {

        try {

            if (photo == null)
                return@withContext null

            val image = imageLoader.loadThumbnailImage(photo)
                ?: return@withContext null

            return@withContext BitmapPainter(
                image = image,
                filterQuality = FilterQuality.Low // <-- to render thumbnails faster
            )

        } catch (ex: Exception) {

            Log.error("ImageLoader.loadThumbnailImage() for ${photo?.thumbnailFileName} failed.", ex)

            return@withContext null
        }
    }
}
This way my ImageCard Composable can display a placeholder while off the main thread my image gets loaded. This works very well for JVM and Android... Is this a bad idea for Compose for iOS? Can this result in freezes? 🤔 Log in XCode:
Copy code
98 SwiftUI               0x0000000Thread Performance Checker: Thread running at User-interactive quality-of-service class waiting on a lower QoS thread running at Default quality-of-service class. Investigate ways to avoid priority inversions
PID: 3153, TID: 316405
Backtrace
=================================================================
3  Ashampoo Photos           0x0000000101291d58 _ZN15SkResourceCache13checkMessagesEv + 80
4  Ashampoo Photos           0x0000000101291bb4 _ZN15SkResourceCache4findERKNS_3KeyEPFbRKNS_3RecEPvES6_ + 48
5  Ashampoo Photos           0x0000000101292750 _ZN15SkResourceCache4FindERKNS_3KeyEPFbRKNS_3RecEPvES6_ + 200
6  Ashampoo Photos           0x00000001011f8598 _ZN13SkBitmapCache4FindERK17SkBitmapCacheDescP8SkBitmap + 92
7  Ashampoo Photos           0x00000001012d9874 _ZNK12SkImage_Lazy11getROPixelsEP15GrDirectContextP8SkBitmapN7SkImage11CachingHintE + 60
8  Ashampoo Photos           0x00000001011f9898 _ZN14SkBitmapDevice13drawImageRectEPK7SkImagePK6SkRectRS4_RK17SkSamplingOptionsRK7SkPaintN8SkCanvas17SrcRectConstraintE + 116
9  Ashampoo Photos           0x0000000101215404 _ZN8SkCanvas16onDrawImageRect2EPK7SkImageRK6SkRectS5_RK17SkSamplingOptionsPK7SkPaintNS_17SrcRectConstraintE + 384
10 Ashampoo Photos           0x000000010175737c org_jetbrains_skia_Canvas__1nDrawImageRect + 104
11 Ashampoo Photos           0x000000010046cec0 kfun:org.jetbrains.skia.Canvas#drawImageRect(org.jetbrains.skia.Image;org.jetbrains.skia.Rect;org.jetbrains.skia.Rect;org.jetbrains.skia.SamplingMode;org.jetbrains.skia.Paint?;kotlin.Boolean){}org.jetbrains.skia.Canvas + 812
12 Ashampoo Photos           0x00000001004ee33c kfun:androidx.compose.ui.graphics#toComposeImageBitmap__at__org.jetbrains.skia.Image(){}androidx.compose.ui.graphics.ImageBitmap + 660
13 Ashampoo Photos           0x0000000100889274 kfun:org.jetbrains.compose.resources#toImageBitmap__at__kotlin.ByteArray(){}androidx.compose.ui.graphics.ImageBitmap + 176
14 Ashampoo Photos           0x0000000100881b28 kfun:org.jetbrains.compose.resources.$painterResource$lambda$2$FUNCTION_REFERENCE$4.invoke#internal + 1420
15 Ashampoo Photos           0x000000010087c5e0 kfun:org.jetbrains.compose.resources#painterResource(kotlin.String;androidx.compose.runtime.Composer?;kotlin.Int){}androidx.compose.ui.graphics.painter.Painter + 1868
16 Ashampoo Photos           0x0000000100aaa9b4 kfun:com.ashampoo.photos.compose.platform#painter(com.ashampoo.photos.compose.BitmapRes;androidx.compose.runtime.Composer?;kotlin.Int){}androidx.compose.ui.graphics.painter.Painter + 436
17 Ashampoo Photos           0x0000000100bba7e4 kfun:com.ashampoo.photos.compose.ui.gallery#PhotoRatingImage(com.ashampoo.kim.model.PhotoRating;androidx.compose.runtime.Composer?;kotlin.Int){} + 1348
18 Ashampoo Photos           0x0000000100b93b10 kfun:com.ashampoo.photos.compose.ui.gallery.GalleryPhotoBox#internal + 13164