Async / await with ParseSwift Query sample?

Hi all!

I just started converting my code to async await with swift 5.5. I was wondering though if anyone has samples or can point me to one that shows how i can use ParseSwift’s current query functions using completion handlers with async await.

To be more specific, I’m trying to convert my current completion handler function which has a query.first { } inside it, to an async function and keep the same query.first { } method inside it intact.

Thanks in advance

A sample repo I created, SnapCat, shows off a lot of the Parse Swift SDK capabilities such as: SwiftUI compatibility (Models & ViewModels), LiveQuery, async/await, etc.

For comparison, you can look at the iOS14 branch which only uses completion handlers. A fetch example is below (query and everything else is very similar):

The same code written using async/await is below:

There are tons of examples how to use the SDK with MVVM and SwiftUI. To highlight a one I think is super useful, is creating custom view models that automatically subscribe to live queries along with fetch on first call. This allows you to make dynamic timelines that automatically reflect updates in a social media app. The custom view model to do this is below:

Then you use it to create a timeline view like this:

Basically, any new posts from people you follow, new comments or likes, automatically are reflected on the timeline without having to refresh.

The Swift SDK playgrounds(completion handler) and test cases(completion handler and async/await) provide a number of examples. From the test cases, a completion handler query cane be done:

The async/await version looks like (there’s also a Combine version):

In terms of simple MVVM with SwiftUI (my first post shows how to do a more sophisticated one), the Swift SDK provides a number of items out-of-the-box. The assumptions are:

  • Model (ParseObject)
  • ViewModel (Query/LiveQuerySubscription/CloudCodeResultViewModel/Custom)
  • View (developers design their own views).

The documentation states that every Query has a viewModel property automatically. This allows you to be able to create views that displays your ParseObjects easily. The playgrounds demonstrates this below:

1 Like

this is awesome sir @cbaker6 . I’ll look into all of these.

thanks again for your hard work on ParseSwift!

1 Like