Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix/ios-multiple-image-selection-concurrency] Resolve Concurrency Issue in iOS Multiple Image Picker #13

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import platform.PhotosUI.PHPickerViewController
import platform.PhotosUI.PHPickerViewControllerDelegateProtocol
import platform.UIKit.UIApplication
import platform.darwin.NSObject
import platform.darwin.dispatch_group_create
import platform.darwin.dispatch_group_enter
import platform.darwin.dispatch_group_leave
import platform.darwin.dispatch_group_notify
import platform.posix.memcpy

@Composable
Expand All @@ -34,21 +38,30 @@ actual fun rememberImagePickerLauncher(
@Suppress("UNCHECKED_CAST")
val results = didFinishPicking as List<PHPickerResult>

val dispatchGroup = dispatch_group_create()
val imageData = mutableListOf<ByteArray>()

for (result in results) {
dispatch_group_enter(dispatchGroup)
result.itemProvider.loadDataRepresentationForTypeIdentifier(
typeIdentifier = "public.image",
) { nsData, _ ->
scope?.launch(Dispatchers.Main) {
val data = mutableListOf<ByteArray>()
nsData?.let {
val bytes = ByteArray(it.length.toInt())
memcpy(bytes.refTo(0), it.bytes, it.length)
data.add(bytes)
imageData.add(bytes)
}
onResult(data.toList())
dispatch_group_leave(dispatchGroup)
}
}
}

dispatch_group_notify(dispatchGroup, platform.darwin.dispatch_get_main_queue()) {
scope?.launch(Dispatchers.Main) {
onResult(imageData)
}
}
}
}

Expand Down