Post

Replies

Boosts

Views

Activity

Struggling with async/await: Fetching an image off the main thread
Hey everyone, I’m learning async/await and trying to fetch an image from a URL off the main thread to avoid overloading it, while updating the UI afterward. Before starting the fetch, I want to show a loading indicator (UI-related work). I’ve implemented this in two different ways using Task and Task.detached, and I have some doubts: Is using Task { @MainActor the better approach? I added @MainActor because, after await, the resumed execution might not return to the Task's original actor. Is this the right way to ensure UI updates are done safely? Does calling fetchImage() on @MainActor force it to run entirely on the main thread? I used an async data fetch function (not explicitly marked with any actor). If I were to use a completion handler instead, would the function run on the main thread? Is using Task.detached overkill here? I tried Task.detached to ensure the fetch runs on a non-main actor. However, it seems to involve unnecessary actor hopping since I still need to hop back to the main actor for UI updates. Is there any scenario where Task.detached would be a better fit? class ViewController : UIViewController{ override func viewDidLoad() { super.viewDidLoad() //MARK: First approch Task{@MainActor in showLoading() let image = try? await fetchImage() //Will the image fetch happen on main thread? updateImageView(image:image) hideLoading() } //MARK: 2nd approch Task{@MainActor in showLoading() let detachedTask = Task.detached{ try await self.fetchImage() } updateImageView(image:try? await detachedTask.value) hideLoading() } } func fetchImage() async throws -> UIImage { let url = URL(string: "https://via.placeholder.com/600x400.png?text=Example+Image")! //Async data function call let (data, response) = try await URLSession.shared.data(from: url) guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else { throw URLError(.badServerResponse) } guard let image = UIImage(data: data) else { throw URLError(.cannotDecodeContentData) } return image } func showLoading(){ //Show Loader handling } func hideLoading(){ //Hides the loader } func updateImageView(image:UIImage?){ //Image view updated } }
4
0
427
3w
iOS 18.1 beta - App crashes at runtime while using Translation.TranslationError in project
I'm trying to cast the error thrown by TranslationSession.translations(from:) as Translation.TranslationError. However, the app crashes at runtime whenever Translation.TranslationError is used in the project. Environment: iOS Version: 18.1 beta Xcode Version: 16 beta yld[14615]: Symbol not found: _$s11Translation0A5ErrorVMa Referenced from: <3426152D-A738-30C1-8F06-47D2C6A1B75B> /private/var/containers/Bundle/Application/043A25BC-E53E-4B28-B71A-C21F77C0D76D/TranslationAPI.app/TranslationAPI.debug.dylib Expected in: /System/Library/Frameworks/Translation.framework/Translation
1
1
884
Aug ’24
How to Debug Background Assets in macOS?
I attempted to utilize the Background Assets feature for an iOS app. While debugging, I employed the following command to trigger the installation event: xcrun backgroundassets-debug -b <bundleID> -s --app-install -d <Device ID> This command worked flawlessly on an iPhone. However, when I attempted to trigger the installation event on a Mac, I encountered the following error message: The requested device to send simulation events to is not available. Verify that the device is connected to this Mac. Please note that the xcrun backgroundassets-debug -l command only displays a list of connected devices.Mac is not listed in that list.
0
2
744
Oct ’23
Invalid absolute dimension: inf. The dimension must be a finite value
Hi I tried to use compositional layout, while using stage manager rare the app getting crashed. Please refer below error message Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid absolute dimension: inf. The dimension must be a finite value.' let itemSize = NSCollectionLayoutSize(widthDimension: .absolute(cellWidth),heightDimension: .absolute(cellHeight)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(cellHeight)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) group.interItemSpacing = NSCollectionLayoutSpacing.fixed(cellSpacing) let section = NSCollectionLayoutSection(group: group) section.contentInsets = NSDirectionalEdgeInsets(top: 10,leading: inset, bottom: 0,trailing:inset) section.interGroupSpacing = lineSpacing Why am I getting this error after providing exact height and width?
3
0
967
Jul ’23
The requested URL is not permitted to be downloaded until the application is launched error in background assets
Hi , i tried to download image using background assets framework. I tried to use install and update events to test the extension. But the extension always delegate to backgroundDownload(_ :failedWithError :) function with following error Error Domain=BAErrorDomain Code=202 "The requested URL is not permitted to be downloaded until the application is launched." UserInfo={NSLocalizedFailureReason=The requested URL is not permitted to be downloaded until the application is launched.} I used the following url to download the image https://w0.peakpx.com/wallpaper/934/165/HD-wallpaper-avatar-aang-aang-airbender-aire-appa-avatar-legend-leyenda.jpg Why am i getting this error and how to resolve this?
3
0
965
Jun ’23
captureTextFromCamera action is not get triggered in iPad Air (4th gen)
Hi folks, I tried to use apple's text recognition feature by using UIAction.captureTextFromCamera(responder:identifier:). It works fine in iPhone and iPad 3rd Gen. But in iPad 4th Gen the action is not get triggered. Kindly provide assistance . if #available(iOS 15.0, *), self.canPerformAction(#selector(captureTextFromCamera(_:)), withSender: self) {       let cameraAction = UIAction.captureTextFromCamera(responder: self, identifier: nil)       let cameraButton = UIButton(type: .custom)       cameraButton.addAction(cameraAction, for: .touchUpInside)       cameraButton.setTitle("Scan-Text", for: .normal)       view.addSubview(cameraButton)       // Then set constraints     } _Note: The button to trigger the action is visible only the action is not get triggered. _ Device specs: Name: iPad Air 4th Gen Os : iOS 16
0
0
1k
Oct ’22