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
62 stars 7 forks source link

Ensuring `initialize` has returned before attempting queries. #169

Open cbaker6 opened 7 months ago

cbaker6 commented 7 months ago

Discussed in https://github.com/netreconlab/Parse-Swift/discussions/167

Originally posted by **project-saf** March 28, 2024 Very infrequently, I run in to a crash along the lines of: ```swift ParseSwift/Parse.swift:106: Fatal error: Unexpectedly found nil while unwrapping an Optional value ``` Here is my AppDelegate: ```swift @main class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { Task { guard let AppID = Secrets.App_ID, let ClientKey = Secrets.Cli_ID, let AppURL, let LiveURL else { fatalError("Incorrect implementation of Secrets") } let config = ParseConfiguration(AppID, ClientKey, AppURL, LiveURL) try await initialize(configuration: config) } return true } } ``` I was hesitant to report it since I can't reproduce it consistently, but I thought I'd flag it anyway just in case you: (a) wanted to know, and/or (b) had any insight as to what might be causing the issue. It definitely _does_ crash with the same error if I comment out the Task in `didFinishLaunching` and then run a query. My guess therefore is that it is some sort of issue with the login task being de-prioritised over some other Parse function like a query. Line 106 is the following: ```swift l104 /// The current `ParseConfiguration` for the ParseSwift client. l105 public var configuration: ParseConfiguration { l106 Parse.configuration l107 } ``` **Questions** 1. Is my AppDelegate code the intended way for Parse to be initialised? 2. Is there a way to guarantee the login task completes either before returning from `didFinishLaunching`, or at least completes first before any other Tasks? 3. Is it wise/possible to 'bake' into ParseSwift a means of ensuring that a query won't run until after the `initialize` function has completed? For example, a flag that makes those other tasks continue waiting until after initialize has returned.
cbaker6 commented 7 months ago

This issue occurs when using the == query constraint mixed with using the old appDelegate` lifecycle. Workarounds for this are described in https://github.com/netreconlab/Parse-Swift/discussions/167#discussioncomment-8974187

A fix requiring the == QueryConstraint to try await will be added to the SDK in 6.0.0