{"id":653,"date":"2019-02-12T11:15:40","date_gmt":"2019-02-12T11:15:40","guid":{"rendered":"http:\/\/fluentreports.com\/blog\/?p=653"},"modified":"2019-02-12T11:15:41","modified_gmt":"2019-02-12T11:15:41","slug":"nativescript-sqlite-multi-threading","status":"publish","type":"post","link":"http:\/\/fluentreports.com\/blog\/?p=653","title":{"rendered":"NativeScript-SQLite Multi-Threading!"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The first release of <a href=\"https:\/\/plugins.nativescript.rocks\/plugin\/nativescript-sqlite\">NativeScript-SQLite<\/a> was April 19th, 2015 on GitHub, and April 26th to NPM.   I am happy to say this plugin is now almost 4 years old, and it was one of the first plugins available to NativeScript.  I believe there was two or three others released before, like NativeScript-Flashlight.     By June of 2015 we had a grand total of 18 (eighteen) plugins in the NativeScript eco-system, where today we are at almost <a href=\"https:\/\/plugins.nativescript.rocks\/plugins\">1,000 plugins<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">NativeScript-Sqlite has had a lot of upgrades like encryption, prepared queries, and several bug fixes over the last 4 years.  But the newest feature; I'm probably the most proud of.   Without your app changing a thing (besides setting a simple option flag); NativeScript-Sqlite can now be fully multithreaded.    What do I mean by that?    In a nutshell all SQLite calls can now happen in a background thread.   This means that if you are inserting or reading 20 records the UI won't freeze.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A couple years ago I did a session on <a href=\"https:\/\/github.com\/NathanaelA\/PerformanceFromTheTrenches\">Performance and NativeScript<\/a>; my sample app showed how doing several different things could freeze the UI easily; and the best way to deal with this was to move as much of your work to background threads.<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"408\" style=\"aspect-ratio: 376 \/ 408;\" width=\"376\" controls src=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2019\/02\/SQLite-Freeze.mp4\"><\/video><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now if you watch this image; every time I hit the SQLite button, the \"n\" freezes for a few seconds.  This is because the work loading and processing the records is all done on the main thread which is the UI thread, causing the Animation to stop and\/or stutter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since NativeScript-Sqlite has always been Promise (and\/or Callback) based ASYNC code; it ended up that I could preserve the entire API as is; when allowing you to use multithreading.   You just need to set a flag when opening up the connection and it will become multithreaded.<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"408\" style=\"aspect-ratio: 376 \/ 408;\" width=\"376\" controls src=\"http:\/\/fluentreports.com\/blog\/wp-content\/uploads\/2019\/02\/SQLite-Worker.mp4\"><\/video><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And this is what it looks like when sqlite is doing everything in a background thread.  Notice how smooth it is.   This is because the main thread is just waiting for the acknowledgement back that everything has been loaded.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you check out the <a href=\"https:\/\/github.com\/NathanaelA\/nativescript-sqlite\/blob\/master\/demo\/app\/main-page.js\">demo app<\/a>; the code change is just this:<\/p>\n\n\n\n<pre class=\"wp-block-code\">[[code]]czoxMDk6XCJuZXcgc3FsaXRlKGRibmFtZSwge2tleTogXFxcJ3Rlc3RpbmdcXFwnLCBtdWx0aXRocmVhZGluZzogISFzcWxpdGUuSEFTX0NPe1smKiZdfU1NRVJDSUFMfSwgZnVuY3Rpb24oZXJyLCBkYkNvbm5lY3Rpb24pIHtcIjt7WyYqJl19[[\/code]]<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Basically if the commercial plugin has been loaded; we flip on the multithreading flag.  Otherwise we leave it off.   (This way the app can test, Encryption, Multithreading, Commercial features, and no extra features all in the same codebase.)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The open source version of the plugin can be easily installed by typing <code>tns plugin install nativescript-sqlite@latest<\/code>, the open source repo can be seen at <a href=\"https:\/\/github.com\/NathanaelA\/nativescript-sqlite\">https:\/\/github.com\/NathanaelA\/nativescript-sqlite<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The commercial version which includes Encryption, transactions, prepared queries, and now multithreading can be <a href=\"https:\/\/nativescript.tools\/product\/10\">purchased<\/a> from my site at <a href=\"https:\/\/nativescript.tools\/product\/10\">nativescript.tools<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first release of NativeScript-SQLite was April 19th, 2015 on GitHub, and April 26th to NPM. I am happy to say this plugin is now almost 4 years old, and it was one of the first plugins available to NativeScript. I believe there was two or three others released before, like NativeScript-Flashlight. By June of&hellip; <a class=\"more-link\" href=\"http:\/\/fluentreports.com\/blog\/?p=653\">Continue reading <span class=\"screen-reader-text\">NativeScript-SQLite Multi-Threading!<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[15,7,51],"tags":[125,16,124,84],"class_list":["post-653","post","type-post","status-publish","format-standard","hentry","category-nativescript","category-performance","category-plugins","tag-database","tag-nativescript","tag-sqlite","tag-webworkers","entry"],"_links":{"self":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/653","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=653"}],"version-history":[{"count":1,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions"}],"predecessor-version":[{"id":659,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/653\/revisions\/659"}],"wp:attachment":[{"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=653"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/fluentreports.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}