AsyncTask

API Reference: AsyncTask

AsyncTask can be used in cases where UI thread can cause performance issues. AsyncTask creates a different thread to overcome this.

TypeScript code blocks include examples of how to implement, override and components within the theme. You can create page with the UI Editor to make your page compatible with theming and then you can implement themable components programmatically. Once the page is created with the UI Editor, it generates a class under scripts/generated/pages. You can then extend that class with the following TypeScript classes.

Use Cases

  • Downloading large files

  • Accessing DB

  • Parsing JSON

  • And other heavy operations such as searching in large text

AsyncTask is used for non UI operations. Any UI operation performed in task method will result in unexpected errors.

In the following example, assume we have performed a request that will response with large data. That large data is parsed using AsyncTask.

TypeScript
JavaScript
TypeScript
import PageSampleDesign from 'generated/pages/pageSample';
import FlexLayout = require('sf-core/ui/flexlayout');
import Application = require('sf-core/application');
import Http = require("sf-core/net/http");
import Blob = require("sf-core/global/blob");
import AsyncTask = require("sf-core/asynctask");
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
http: Http = new Http();
constructor() {
super();
// Overrides super.onShow method
this.onShow = onShow.bind(this, this.onShow.bind(this));
// Overrides super.onLoad method
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
this.layout.flexDirection = FlexLayout.FlexDirection.ROW;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
}
}
/**
* @event onShow
* This event is called when a page appears on the screen (everytime).
* @param {function} superOnShow super onShow function
* @param {Object} parameters passed from Router.go function
*/
function onShow(superOnShow: () => void) {
const { headerBar } = this;
superOnShow();
Application.statusBar.visible = false;
headerBar.visible = false;
}
/**
* @event onLoad
* This event is called once when page is created.
* @param {function} superOnLoad super onLoad function
*/
function onLoad(superOnLoad: () => void) {
superOnLoad();
this.http.request({
"url": "https://api.github.com/repos/smartface/contxjs",
"method": "GET",
onLoad: (response: {
statusCode: number,
headers: { [key: string]: string },
body: Blob
}): void => {
let result = response.body.toString();
let parsedObject;
// Run new task
var asyncTask = new AsyncTask();
asyncTask.task = function () {
parsedObject = JSON.parse(result);
};
asyncTask.onComplete = function () {
console.log("parsedObject " , parsedObject);
};
asyncTask.run();
},
onError: (e: {
message: string;
body: any;
statusCode: number;
headers: { [key: string]: string };
}): void => {
// Handle error
}
});
}
JavaScript
const AsyncTask = require("sf-core/asynctask");
const extend = require('js-base/core/extend');
const Page = require('sf-core/ui/page');
const Http = require("sf-core/net/http");
var http = new Http();
var page1 = extend(Page)(
function(_super, params) {
_super(this, params);
this.onLoad = function() {
http.request({
"url": "https://api.github.com/repos/smartface/contxjs",
"method": "GET",
onLoad: function(response) {
var result = response.body.toString();
var parsedObject;
// Run new task
var asyncTask = new AsyncTask();
asyncTask.task = function() {
parsedObject = JSON.parse(result);
};
asyncTask.onComplete = function() {
// Do something with parsedObject
};
asyncTask.run();
},
onError: function(e) {
// Handle error
}
});
};
}
);
module.exports = page1;