Asynchronous DB Operations in QML Applications

Consider QML application where UI has non-stop animation, e.g. video is playing in the background or slides are floating around slowly. Source code of prototype of such application:  svn co https://github.com/kocherovms/develorium.com/trunk/async_db_ops_in_qml_apps/0

Application’s data is stored in database and it’s necessary to implement ordinary function: full text search against this database. So, how do you do that while keeping responsive UI?

1 First, naive and dumb solution

Let’s start with implementing a straightforward solution ( svn co https://github.com/kocherovms/develorium.com/trunk/async_db_ops_in_qml_apps/1). We know all these design patterns of GoF so let’s use facade and hide all the complexity of database interactions behind this facade:

So far so good. We have a class which accepts  Connection  object and uses it to talk to database. Full text search is implemented via  search  method (invoked when you left click somewhere within a window). Using this class initial task could be solved in the following way:

In handler of a “Search” button we instantiate our facade and obtain search results from database. Everything is clear and fine: full text search works quickly and program runs smoothly.

Problems start to arise when database grows and suddenly UI starts to freeze (in our prototype logo stops spinning) when you use a full text search function. Reason is apparent: interaction with a database is a heavy blocking I/O operation without any guarantees on time it will take to complete. So when SQL(s) used in an implementation of a  search  method consumes > 100ms nasty visual effects starts to appear since it blocks QML rendering.

 

 2 Ideal Waiting Cycle

So we need somehow to unblock QML rendering while making necessary SQL(s). Good news is that Qt has a nice tool set for the tasks like that under  QtConcurrent  module. Let’s use  QtConcurrent::run  to execute facade code in a separate thread:

QtConcurrent::run  returns  QFuture  object which allows us to monitor status of the task execution. We then call  waitForFinished  to wait for completion of the task.

So far this code behaves exactly as our first solution: it blocks until facade finishes its jobs. But now we can do the following trick:

We’ve just added a temporal event loop which will process events while waiting for completion of our SQL(s). We’ve also used  QFutureWatcher  which emits signals when watched future object completes its job. Last fw.waitForFinished  is needed to propagate exceptions (if any) occurred during execution of  Facade::search  method.

Important: connect  QFutureWatcher  before calling to setFuture . Otherwise you may loose finished signal due to race conditions: signal may simply be emitted before loop.exec.

So we’ve achieved situation when all animation events in QML keep running while waiting for heavy SQL(s) to complete. Nice!

Source code: svn co https://github.com/kocherovms/develorium.com/trunk/async_db_ops_in_qml_apps/2

3 Generalization

As an application keeps growing new database related functions could emerge, e.g. consider that now we require a function of putting some item into statistics table.  Facade  class is extended like this:

Method  Facade::putIntoStatistics is invoked on right click within program’s window. To secure ourselves from UI freeze during  Facade::putIntoStatistics  we would like to write a code similar to code of Manager::onSearchButtonPressed. This renders problem of code duplication and as such we need a solution for a program-wide asynchronous calls to DB. This solution must be:

  • as simple as possible in its usage. Ideally such asynchronous calls must look like direct calls to facade’s method without any boilerplate code;
  • be extensible so we could cover more and more methods.

Solution which suits our needs is provided below ( svn co https://github.com/kocherovms/develorium.com/trunk/async_db_ops_in_qml_apps/3):

New public method  Facade::asyncCall  was introduced. This is a wrapper method which accepts pointer to a method which must be called as well as a single argument. Couple of tricks were used:

  1. dynamic_cast  is used to handle situation when  Facade is subclassed and one wants to call method of a derived class;
  2. static function  extractResult  is introduced to cope with an absence of  result method for QFuture<void>  specialization. Call to  waitForFinished  is used to propagate possible exceptions (as  result method does).

Facade::asyncCall could be used like this:

As we see this almost resembles our first (blocking) variant. Template parameters could be omitted since compiler would automatically deduce them. Nice and clean.

Presented  Facade::asyncCall  version accepts only 1 argument. One could add several variants of the method which accepts more arguments or use variadic templates if newer version of C++ is used.

With this approach we could even go further ( svn co https://github.com/kocherovms/develorium.com/trunk/async_db_ops_in_qml_apps/4). E.g. consider that we’ve decided to create separate facades for search and statistics business logic. Implementation of Facade::asyncCall allows us to do the following:

I.e. we could asynchronously call methods from  Facade  derived classes. Cool!

 

4 Caveats

Using presented approach one must clearly realize its one big disadvantage: it permits nested and hence out or order events execution (see picture below).

seq_nested_calls

Sequential (normal) calls VS nested calls

This means that some part of your code could get run earlier than you expect it. Even worse – it could run during the course of execution of your event handler, e.g.  Manager::onSearchButtonPressed could implicitly call Manager::onStatisticsButtonPressed. This happens because during Facade::asyncCall another version of event loop (local one) is being ran which could start executing another portion of your code (e.g. another slot). Yes, we can use QEventLoop::ExcludeUserInputEvents but this doesn’t work well on some systems and doesn’t prevent the issue completely – your code could use custom events which could be called in the middle of event handler.

If your code is not resistant to such nested event processing than you must consider another ways of keeping UI alive, e.g move all the business logic in a separate thread and use queued SIGNAL / SLOT to communicate with main thread.

Another problem which could arise due to previous one is concurrent use of single  Connection object. This is due to the fact that  QtConcurrent::run  executes code in a separate thread and as such a nested  Facade::asyncCall  could lead to situation when two threads will content for this shared resource. One needs to introduce proper locking of  Connection  object for this case.

 

 

0 Responses to “Asynchronous DB Operations in QML Applications”

  1. Brucejeme says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    This useful application allows you to gather information from the network in order to generate reports about the detected programs and hardware

    Press the “Download Now” button to download Network Inventory Advisor crack keygen serial.
    The whole process will just take a few moments.

    Mirror Link —> Network Inventory Advisor serial key

    – Build: 5.0.155
    – Developer: ClearApps Software
    – Downloads: 1154
    – Download type: safety (no torrent/no viruses)
    – File status: clean (as of last analysis)
    – File size: na
    – Price: free
    – Special requirements: no
    – Home page: network-inventory-advisor.com
    – User rating:

    Tags:
    Network Inventory Advisor Crack
    Network Inventory Advisor crack install
    Network Inventory Advisor Full Crack Latest
    Network Inventory Advisor With License Key Latest
    Network Inventory Advisor Full Version With Keys Latest

    Popular software:
    AllPlan crack files
    iBackup Viewer license code
    iMyFone iOS System Recovery full crack
    RadiAnt DICOM Viewer serial key
    AllPlan crack hack
    Антиплагиат как обойти бесплатно
    Website 2 APK Builder Pro Licence Keys – Ножевая Барахолка, Покупка-продажа Ножей, Ножевые форумы.
    Convert Google books to PDF, PNG or JPG
    wafer | Nanotech Analisys

  2. Brucejeme says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Press the “Download Now” button to download [b]DSLR Remote Pro key crack[/b].
    The whole process will just take a few moments.

    [url=https://downloadcrackz.com/program/dslr-remote-pro/51054][img]http://i.imgur.com/PrgdxKb.png[/img][/url]

    Mirror Link —> [url=https://downloadcrackz.com/program/dslr-remote-pro/51054][b]DSLR Remote Pro Crack Patch[/b][/url]

    An efficient and reliable software utility that successfully allows you to remotely control your Canon EOS digital SLR from your computer

    · Version: 3.13
    · Developer: Breeze Systems
    · Downloads: 1194
    · Download type: safety (no torrent/no viruses)
    · File status: clean (as of last analysis)
    · File size: undefined
    · Price: free
    · Special requirements: no
    · Rating: [img]http://cdn1.valuationtechnology.com/wp-content/uploads/2018/08/03105541/stars.png[/img]

    [img]https://downloadcrackz.com/Content/Images/Apps/dslr-remote-pro_16.png[/img]

    [b]Tags[/b]:
    dslr remote pro cracked full, dslr remote pro keygen serial, dslr remote pro crack new, dslr remote pro activator, dslr remote pro keygen serial, dslr remote pro crack exe, dslr remote pro full version serial keys latest, dslr remote pro crack, dslr remote pro crack patch, dslr remote pro keygen serial

    [b]Popular software:[/b]
    [url=https://downloadcrackfree.com/crack?s=diskinternals-vmfs-recovery&id=105270]DiskInternals VMFS Recovery how to ctack[/url]
    [url=https://crackreactor.org/serial?s=rar-recovery-toolbox&v=105612]Recovery Toolbox for RAR keygen serial[/url]
    [url=https://crack4windows.com/crack?s=obd-auto-doctor&id=64555]OBD Auto Doctor activate code[/url]
    [url=http://freesmartsoft.com/Home/Details/?name=FSSPlagiator&lang=ru]повысить процент уникальности текста[/url]
    [url=http://glechyk.info/2017/08/29/podatok-na-nerukhome-maino-khto-skilky/?unapproved=8799&moderation-hash=09ff2e14f96294daa9323e377f9fe757#comment-8799]Податок на нерухоме майно: хто, скільки і чому має платити – glechyk.info[/url]
    [url=https://flakegames.com/game/4951/deep-sea-tycoon-divers-paradise]Download Deep Sea Tycoon: Diver’s Paradise Full Crack[/url]
    [url=http://everydaysoft.com/Product/EDSGoogleBooksDownloader/?lang=en]EDS Google Books Downloader – download google books free[/url]
    [url=https://video-box.org]как скачать видео с ok.ru[/url]
    [url=http://www.goodbb.net/uncategorized/hello-world/?unapproved=3821&moderation-hash=a26e587d4b378fc39d2284f2b7fb2979#comment-3821]Hello world! – My blog[/url]
    [url=https://mygdz.net]ГДЗ: готовые домашние задания за 1-11 класс[/url]
    [url=https://www.pinterest.com/downloadwithcrack/windows-software-cracks/]Windows Software Cracks[/url]
    [url=https://dlls.in/dll/gfsdk_shadowlib.win64.dll/9355]How to repair missing or corrupt gfsdk_shadowlib.win64.dll[/url]

  3. Brucejeme says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Press the “Download Now” button to download [b]Virtual Serial Port Driver Cracked[/b].
    The whole process will just take a few moments.

    [url=https://crackpluskeygen.org/software?q=Virtual-Serial-Port-Driver&i=96082][img]http://i.imgur.com/dp4T3mR.png[/img][/url]

    Mirror Link —> [url=https://crackpluskeygen.org/software?q=Virtual-Serial-Port-Driver&i=96082][b]Virtual Serial Port Driver activate code[/b][/url]

    Create virtual serial ports and test applications that use null modem cable with the help of this effective and practical utility

    · Version: 9.0 Build 9.0.572
    · Developer: ELTIMA Software GmbH
    · Downloads: 3090
    · Download type: safety (no torrent/no viruses)
    · Status file: clean (as of last analysis)
    · File size: undefined
    · Price: free
    · Special requirements: no requirements
    · Rating: [img]http://cdn1.valuationtechnology.com/wp-content/uploads/2018/08/03105541/stars.png[/img]

    [img]https://crackpluskeygen.org/Content/Images/Screens/eltima-virtual-serial-ports-driver-xp_1.png[/img]

    [b]Keywords[/b]:
    virtual serial port driver crack hack, virtual serial port driver crack fix, virtual serial port driver activator, virtual serial port driver serial number, virtual serial port driver crack, virtual serial port driver + crack latest, virtual serial port driver keygen free download, virtual serial port driver how to ctack, virtual serial port driver license code, virtual serial port driver how to ctack

    [b]It may be interesting:[/b]
    [url=https://downloado.in/keygen/95815/aster+xp]ASTER V7 how to ctack[/url]
    [url=https://downloadcrackfree.com/crack?s=diskinternals-vmfs-recovery&id=105270]DiskInternals VMFS Recovery serial key[/url]
    [url=https://crack4windows.com/crack?s=elcomsoft-forensic-disk-decryptor&id=94519]Elcomsoft Forensic Disk Decryptor Crack Patch[/url]
    [url=http://freesmartsoft.com/Home/Details/?name=FSSPlagiator&lang=ru]Помощь повышение оригинальности[/url]
    [url=http://sticksandstonespodcast.com/2019/03/25/hello-world/?unapproved=288&moderation-hash=864fe175c1396d610cf16711eb83c4fe#comment-288]Hello world! – Sticks And Stones Podcast[/url]
    [url=https://hotgamesguide.com/games/life-is-strange-episode-3—chaos-theory/1202]Download Life is Strange: Episode 3 – Chaos Theory Cracked Latest[/url]
    [url=http://everydaysoft.com/Product/EDSGoogleBooksDownloader/?lang=en]EDS Google Books Downloader – download google books free[/url]
    [url=https://video-box.org]free video downloader online[/url]
    [url=https://rapidweightlossnow.com/incredible-weight-loss-transformations-motivation/?unapproved=6610&moderation-hash=04773cb90c046b8cb98a2d8b4c2a0565#comment-6610]INCREDIBLE WEIGHT LOSS TRANSFORMATIONS!! //MOTIVATION – Rapid Weight Loss Tips[/url]
    [url=https://mygdz.net]ГДЗ за 1-11 класс[/url]
    [url=https://www.pinterest.com/downloadwithcrack/software-cracks-keygens/]Software Cracks Keygens[/url]
    [url=https://dlls.in/dll/cc3250mt.dll/3748]How to repair missing or corrupt cc3250mt.dll[/url]

  4. Brucejeme says:
    Your comment is awaiting moderation. This is a preview, your comment will be visible after it has been approved.

    Press the “Download Now” button to download [b]PowerMockup activate code[/b].
    The whole process will just take a few seconds.

    [url=https://coolwareznik.net/crack?n=55261&t=powermockup][img]http://i.imgur.com/HGx8tOz.png[/img][/url]

    Make your PowerPoint presentations stand out with an extensive number of custom shapes you downloaded or created with this application

    MIRROR LINK —> [url=https://coolwareznik.net/crack?n=55261&t=powermockup]https://coolwareznik.net/crack?n=55261&t=powermockup[/url]

    · Build: 4.3.3.0
    · Crack date: July 2 2019
    · Developer: Wulfsoft
    · Downloads: 27677
    · Download type: safety (no torrent/no viruses)
    · Status: clean (as of last analysis)
    · File size: unknown
    · Price: free
    · Special requirements: no
    · Supported systems: Win All
    · Rating: [img]http://cdn1.valuationtechnology.com/wp-content/uploads/2018/08/03105541/stars.png[/img]

    [img]https://coolwareznik.net/content/images/pro/powermockup-preview.png[/img]

    [b]Tags cloud[/b]:
    powermockup crack and keygen free download, powermockup how to ctack, powermockup keygen serial, powermockup keygen serial, powermockup crack latest version, powermockup crack for windows free download, powermockup crack serial number, powermockup keygen free download, powermockup patch, powermockup cracked latest

    [b]Popular software:[/b] [url=https://coolwareznik.net]here[/url]

Leave a Reply

Your email address will not be published.