0
votes

I am trying to find and use the one CKRecord that corresponds to the current logged-in user from iCloud throughout my application.

I am fetching the user and saving it from AppDelegate (in a seemingly unnecessarily complicated manner, at that), and then retrieving the record from the AppDelegate when I need it throughout the app.

However, the retrieval of the user takes more time than the app takes to load, and so when the root view controller has loaded, the text label that I have set to be the user's name is empty because the user has not yet been retrieved. This then causes the app to crash because of a "fatal error: unexpectedly found nil while unwrapping an Optional value", and the nil value is the user record.

There must be an easier as well as faster way to retrieve the record of the current user from CloudKit. Any help would be greatly appreciated! :)

1

1 Answers

0
votes

A CloudKit query will be an asynchronous call. You will never be sure at what moment the callback with the data will be made. So you should not depend on the data to be there. If you have any code that depends on that data, then call that code from inside the CloudKit callback. If you do need to access that data from somewhere else, then make sure you add a nil check before accessing it.