netreconlab / Parse-Swift

The original (OG) Swift SDK for Parse Platform (iOS, macOS, watchOS, tvOS, visionOS, Linux, Android, Windows). This repo is maintained by Parse-Swift's original developer and all new features and bug fixes will occur here.
https://swiftpackageindex.com/netreconlab/Parse-Swift/documentation
Apache License 2.0
61 stars 7 forks source link

Getting "Swift runtime failure: Unexpectedly found nil while implicitly unwrapping an Optional value" error #94

Closed jaysonng closed 1 year ago

jaysonng commented 1 year ago

New Issue Checklist

Issue Description

"Thread 4: Swift runtime failure: Unexpectedly found nil while implicitly unwrapping an Optional value"

when running query with .includeAll() on my @main app file.

The error specifically comes from the firstCommand() function in Query.swift

Screenshot 2023-05-01 at 3 47 45 PM

Steps to reproduce

Call some query on the same view where you run ParseSwift.initialize()

In my case, ParseSwift.initilize is called within init() in a Task { } Screenshot 2023-05-01 at 4 51 01 PM

I receive this warning in the console for the few times that it does not crash

nw_resolver_start_query_timer_block_invoke [C1.1.1] Query fired: did not receive all answers in time for xxxxxxxxx:443

Actual Outcome

A lot of the times it crashes with the said error. only some of the time does it work as expected.

Expected Outcome

There should be no crash and the query should be called after initialization of Parse.

Environment

Client

Server

Database

Logs

cbaker6 commented 1 year ago

Call some query on the same view where you run ParseSwift.initialize()

You are calling your initializer way too late if you are calling it in a view. In addition, placing the Swift SDK init in a view may cause it to be called multiple times, which can lead to a ton of other issues.

The Swift SDK should be called once, and it should be called early when the app opens, which is why it has historically been in the AppDelegate.

I can try to submit a partial fix, but your current design will continue to lead to issues as views can be regenerated, causing the SDK to be reinitialized.

jaysonng commented 1 year ago

I see. I just moved it to the @main view.

I guess ill copy how you have it setup on carekit with the appdelegate. :) thanks again

cbaker6 commented 1 year ago

The linked PR and version should fix the direct issue you are facing.

I guess ill copy how you have it setup on carekit with the appdelegate.

If you are looking through the CareKit example, this branch is cleaner and will be merged in the near future: https://github.com/netreconlab/CareKitSample-ParseCareKit/tree/newCareKit

You will want to setup your AppDelegate in a similar way: