A request can be executed synchronously and asynchronously:
Synchronous requests. They return values after method execution.
Asynchronous requests. They return no value and the application moves to the next method. The response is generated as soon as available.
To get table data without loading to database using synchronous or asynchronous request, create the HHFWTableRetry application that uses the framework methods:
Method name | Brief description |
initWithCredentials (_: host: environment: project: device:) | The method initializes the framework. |
auth (_: password:) | The method authenticates the user by login and password. |
setDownloadPath(_:) | The method sets the path to the folder in the mobile application directory, which stores files with the current resource downloading state. |
useDownload(_:) | The method determines whether to resume resource downloading. NOTE. The application code uses the tableStream/tableStreamAsync method, for which the TableCallParams auxiliary class is determined with the retryCount and retryIntervalSec properties. |
table (_: transactionID: tableCallParams: handler:) | The method performs synchronous request and returns resource table data without loading to database. |
tableAsync (_: transactionID: tableCallParams: handler:) | The method performs asynchronous request and returns resource table data without loading to database. |
Auxiliary method:
Method name | Brief description |
getURL (forFolder:) | The method returns URL of the folder with specified name in the Documents folder. Type of returned data: URL |
The HHFWTableRetry application includes one screen, the UITextView text view, and the UIButton button:
Request Table. Synchronous or asynchronous request for getting table data without loading to database.
NOTE. When the application is initialized, authentication is executed, the button is not available. If the authentication is executed successfully, the button is available and the text box display the text "Authentication success".
To execute the example, click the Request Table button. Clicking the button initializes a request to resource. A mobile platform server returns corresponding table data without loading to database.
Application code with synchronous request execution:
import UIKit
class ViewController: UIViewController {
// Determine output variables
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var tableRequestButton: UIButton!
// Redetermine method
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
self.activateButtons()
self.resultTextView.text = "Authentication success"
}
}
// Set method that will be executed on button click
@IBAction func tableRequestPressed(_ sender: UIButton) {
self.tableRequest { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
// Initialize framework
private func initializeFramework() {
let apiVersion: String = "v1"
let host: String = "http://testmasterfmp.fsight.cloud/"
let environment: String = "Leonid_environment"
let project: String = "Leonid_project"
let device: String = (UIDevice.current.identifierForVendor?.uuidString)!
HHFWController.sharedInstance().initWithCredentials(
apiVersion,
host: host,
environment: environment,
project: project,
device: device
)
}
// Authenticate
private func auth(completion: @escaping ()->()) {
let username: String = "Leonid"
let password: String = "123123"
HHFWController.sharedInstance().auth(username, password: password){ (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Auth success")
completion()
} else {
print("Auth error")
}
}
}
private func setResumingDownload(_ value: Bool) {
let path = self.getURL(forFolder: "DownloadPath").path
HHFWController.sharedInstance().setDownloadPath(path)
HHFWController.sharedInstance().useDownload(value)
}
// Perform the Table request
private func tableRequest(completion: @escaping (NSDictionary)->()) {
let resourceName = "test_resource"
let tableCallParams = TableCallParams(defaultProperty: ())
tableCallParams?.retryCount = 10
tableCallParams?.retryIntervalSec = 1
HHFWController.sharedInstance().table(resourceName, transactionID: nil, tableCallParams: tableCallParams) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Table request success")
completion(jsonDict)
} else {
print("Table request error")
}
}
}
// Set methods to display application user interface elements
private func deactivateButtons() {
self.tableRequestButton.isEnabled = false
}
private func activateButtons() {
self.tableRequestButton.isEnabled = true
}
// Call auxiliary classes
private func getURL(forFolder folderName: String) -> URL {
let paths: [URL] = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let folderURL: URL = paths[0].appendingPathComponent(folderName, isDirectory: true)
return folderURL
}
}
Application code with asynchronous request execution:
import UIKit
class ViewController: UIViewController {
// Determine output variables
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var tableRequestButton: UIButton!
// Redetermine method
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
self.activateButtons()
self.resultTextView.text = "Authentication success"
}
}
// Set method that will be executed on button click
@IBAction func tableRequestPressed(_ sender: UIButton) {
self.tableRequest { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
// Initialize framework
private func initializeFramework() {
let apiVersion: String = "v1"
let host: String = "http://testmasterfmp.fsight.cloud/"
let environment: String = "Leonid_environment"
let project: String = "Leonid_project"
let device: String = (UIDevice.current.identifierForVendor?.uuidString)!
HHFWController.sharedInstance().initWithCredentials(
apiVersion,
host: host,
environment: environment,
project: project,
device: device
)
}
// Authenticate
private func auth(completion: @escaping ()->()) {
let username: String = "Leonid"
let password: String = "123123"
HHFWController.sharedInstance().auth(username, password: password){ (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Auth success")
completion()
} else {
print("Auth error")
}
}
}
private func setResumingDownload(_ value: Bool) {
let path = self.getURL(forFolder: "DownloadPath").path
HHFWController.sharedInstance().setDownloadPath(path)
HHFWController.sharedInstance().useDownload(value)
}
// Perform the TableAsync request
private func tableRequest(completion: @escaping (NSDictionary)->()) {
let resourceName = "test_resource"
let tableCallParams = TableCallParams(defaultProperty: ())
tableCallParams?.retryCount = 10
tableCallParams?.retryIntervalSec = 1
HHFWController.sharedInstance().tableAsync(resourceName, transactionID: nil, tableCallParams: tableCallParams) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Table request success")
completion(jsonDict)
} else {
print("Table request error")
}
}
}
// Set methods to display application user interface elements
private func deactivateButtons() {
self.tableRequestButton.isEnabled = false
}
private func activateButtons() {
self.tableRequestButton.isEnabled = true
}
// Call auxiliary classes
private func getURL(forFolder folderName: String) -> URL {
let paths: [URL] = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let folderURL: URL = paths[0].appendingPathComponent(folderName, isDirectory: true)
return folderURL
}
}
See also:
Examples of iOS Framework Use | Examples of Working with Resources