We have discussed the relationship of QML with JavaScript in previous blogs, and are seeing more and more Esri web developers build native apps with the QML and Qt framework. Many of these developers have given us feedback about how much they enjoy Qt and QML, and have stated that the transition from building HTML5/JS based web apps to QML/JS based native apps was very smooth. One of the common question we get asked by JS devs interested in QML is if QML supports JavaScript promises. While this pattern is not directly built into QML, it is easily doable with a QML library called QuickPromise, which is available project on GitHub.
For those of you not familiar with JavaScript Promises, the patterns and specification are explained in great detail here. The important thing to know is that it is a very productive way to work with asynchronous methods, and can be especially helpful when you are running several async methods concurrently, and you want some action to occur once all of those async methods are complete. A great example in the case of ArcGIS Runtime is when you are wanting to take your data offline. Imagine that you have an app that does editing in an online and offline mode. Initially, your data is online, and your user can edit directly against the service. You also have a button to take the data offline, in which case all of your feature services will generate geodatabases, and your tiled service will export all of the tiles into a local tile package. When your user presses the button, you want to display a screen that indicates the download is in progress, and you don't want that screen to go away until all of the asynchronous operations are complete. Also, once everything is complete, you want to remove the online layers from your map, and add in the offline layers. How would you accomplish this? There may be several ways to do this, but one great way to do this is with promises in QML (QuickPromise).
I set out to try the above scenario to see if I could come up with a clean way of doing this with the QuickPromise library. Here were the steps I took:
This worked wonderfully, and was very simple to setup and use. I found that this particular usage of QuickPromise worked wonderfully for the declarative type QML workflow. Beyond this, there are several other ways you can use QuickPromise, both declaratively, and in imperative JavaScript code. Many of the alternative functions fit more into the traditional pattern of how a JavaScript Promise is used in a web application (e.g. a then() function). The alternative methods of creating a promise in QML are documented here.
If you are interested in giving this a try, I attached my sample project to this blog post. In order to build/run this sample on your machine, you will need to install the Quartz beta 1 release of ArcGIS Runtime SDK for Qt, install QuickPromise, and update the path to the pri file in the project's pro file.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.