Offline
ในคลาสนี้จะใช้ Library จาก SwiftyJSON สำหรับการสร้าง Model ที่ใช้ในการ Sync
LayerOption
Model ของ Condition สำหรับการ Generate Geodatabase
struct LayerOption {
let layerID: Int
let whereClause: String
public init(layerID: Int,
whereClause: String) {
self.layerID = layerID
self.whereClause = whereClause
}
}Properties
| Name | Description | Type |
|---|---|---|
| layerID | ID ของ Layer ที่ต้องการ Generate Geodatabase | Int |
| whereClause | WhereClause ของ Layer ที่ต้องการ Generate Geodatabase | String |
TransactionType
ประเภทของ Transaction ที่จะทำการ Sync
enum TransactionType: String, Codable {
case sp = "SP"
case file = "FILE"
}| Name | Description |
|---|---|
| sp | Transaction สำหรับการเรียก Store Procedure |
| file | Transaction สำหรับการ Upload File |
Transaction
Model ของ Transaction ที่จะทำการ Sync โดย Transaction หมายถึง รายการงานของแต่ละใบงาน
struct Transaction<T: Codable>: Codable {
public var transactionID: Int
public var transactionType: TransactionType
public var parameters: T
enum CodingKeys: String, CodingKey {
case transactionID = "TRANSACTION_ID"
case transactionType = "TRANSACTION_TYPE"
case parameters = "PARAMETERS"
}
public init(transactionID: Int,
transactionType: TransactionType,
parameters: T) {
self.transactionID = transactionID
self.transactionType = transactionType
self.parameters = parameters
}
}Properties
| Name | Description | Type |
|---|---|---|
| T | Class ของ Parameters ที่จะทำการ Sync | Class |
| transactionID | ID ของ Transaction ที่จะทำการ Sync | Integer |
| transactionType | ประเภทของ Transaction | TransactionType |
| parameters | Parameters ที่จะทำการ Sync | T |
FileTransaction
Model ของ Transaction ประเภท File ที่จะทำการ Sync
struct FileTransaction: Codable {
let sourcePath: String
let sourceFile: String
let destinationPath: String
let destinationFile: String
enum CodingKeys: String, CodingKey {
case sourcePath = "SOURCE_PATH"
case sourceFile = "SOURCE_FILE"
case destinationPath = "DESTINATION_PATH"
case destinationFile = "DESTINATION_FILE"
}
public init(sourcePath: String, sourceFile: String, destinationPath: String, destinationFile: String) {
self.sourcePath = sourcePath
self.sourceFile = sourceFile
self.destinationPath = destinationPath
self.destinationFile = destinationFile
}
}Properties
| Name | Description | Type |
|---|---|---|
| sourcePath | Path ของไฟล์ที่จะทำการบันทึก | String |
| sourceFile | ชื่อของไฟล์ที่จะทำการบันทึก | String |
| destinationPath | Path ที่จะทำการบันทึกไฟล์ | String |
| destinationFile | ชื่อของไฟล์ใหม่ที่จะทำการบันทึก | String |
FileData
Model ของ File ที่จะทำการ Sync
struct FileData {
let data: Data
let fileName: String
let fileDirectory: String?
public init(data: Data, fileName: String, fileDirectory: String? = nil) {
self.data = data
self.fileName = fileName
self.fileDirectory = fileDirectory
}
}Properties
| Name | Description | Type |
|---|---|---|
| data | ไฟล์ที่ต้องการ Sync | Data |
| fileName | ชื่อของไฟล์ที่ต้องการ Sync | String |
| fileDirectory | Directory ของไฟล์ที่ต้องการนำไปวาง | Optional(String) |
Work
Model ของ Work ที่จะทำการ Sync โดย Work หมายถึง ใบงานแต่ละใบงาน
struct Work: Codable {
public var workID: Int
public var transactions: [JSON]
enum CodingKeys: String, CodingKey {
case workID = "WORK_ID"
case transactions = "TRANSACTIONS"
}
public init(workID: Int,
transactions: [JSON]) {
self.workID = workID
self.transactions = transactions
}
}Properties
| Name | Description | Type |
|---|---|---|
| workID | ID ของ Work ที่จะทำการ Sync | Int |
| transactions | Transaction ทั้งหมดของ Work ที่จะทำการ Sync | Array(JSON) |
Class Offline
Method Overview
| Name | Description | Return |
|---|---|---|
| generateGeodatabase() | การ Generate Geodatabase แล้วบันทึกไว้ในเครื่อง | void |
| clearGeodatabase() | การ Clear Geodatabase ทั้งหมดภายในเครื่อง | void |
| syncData() | การ Sync ข้อมูล MIS ทั้งหมดไปยัง Web Service | void |
| generateWork() | การสร้าง Object ของ Work สำหรับการ Sync | Optional(Work) |
| getFeatureLayers() | การดึง FeatureLayer ทั้งหมดจาก Geodatabase | void |
Method Details
static func generateGeodatabase(layerOptions layers: [LayerOption],
extent: AGSEnvelope,
url: URL,
nameOfGeodatabase: String = UUID().uuidString,
completion: @escaping (Error?, AGSGeodatabase?) -> Void)การ Generate Geodatabase แล้วบันทึกไว้ในเครื่อง
Parameters
| Name | Description | Type |
|---|---|---|
| layerOptions | Array ของ Condition สำหรับการ Generate Geodatabase | Array(LayerOption) |
| extent | Extent ของ Layer ที่ต้องการสร้าง Geodatabase | AGSEnvelope |
| url | URL ของ Map Service | URL |
| nameOfGeodatabase | ชื่อของ Geodatabase ที่บันทึก | String |
| completion | Callback เมื่อสร้าง Geodatabase สำเร็จหรือไม่สำเร็จ | Completion |
static func clearGeodatabase()การ Clear Geodatabase ทั้งหมดภายในเครื่อง
static func syncData(works: [Work],
files: [FileData] = [],
parameters: [String: Any] = [:],
userID: String,
appFileSource: String = "Default",
appFilePath: String = "Sync",
completion: @escaping (Error?, [String: Any]?) -> Void)การ Sync ข้อมูล MIS ทั้งหมดไปยัง Web Service
Parameters
| Name | Description | Type |
|---|---|---|
| works | Work ทั้งหมดที่ต้องการ Sync | Array(Work) |
| files | File ทั้งหมดที่ต้องการ Sync | Array(FileData) |
| parameters | Parameter ที่ต้องการส่งไปยัง Web Service | Dictionary |
| userID | ID ของผู้ใช้งาน | String |
| appFileSource | Source ของไฟล์ | String |
| appFilePath | Path ของไฟล์ | String |
| completion | Callback เมื่อทำการ Sync ข้อมูลสำเร็จหรือไม่สำเร็จ | Completion |
static func generateWork(workID: Int, transactions: [Any]) -> Work?การสร้าง Object ของ Work สำหรับการ Sync
Parameters
| Name | Description | Type |
|---|---|---|
| workID | ID ของใบงาน | Integer |
| transactions | รายการงานทั้งหมดของใบงาน | Array(Any) |
Returns
| Type | Description |
|---|---|
| Optional(Work) | Return ใบงาน |
static func getFeatureLayers(geodatabase: AGSGeodatabase, completion: @escaping (Error?, [AGSFeatureLayer]?) -> Void)การดึง FeatureLayer ทั้งหมดจาก Geodatabase
Parameters
| Name | Description | Type |
|---|---|---|
| geodatabase | Geodatabase ที่ต้องการดึง FeatureLayer | AGSGeodatabase |
| completion | Callback เมื่อการดึง FeatureLayer สำเร็จหรือไม่สำเร็จ | Completion |
Example
<em>// Your Store Procedure Model</em>
struct DataModel: Codable {
let appDataProcudure: String = "TEST_SYNC_OFFLINE_I"
let name: String
let value: String
enum CodingKeys: String, CodingKey {
case appDataProcudure = "APP_DATA_PROCEDURE"
case name = "NAME"
case value = "VALUE"
}
}
<em>// Initial Your Store Procedure Model</em>
let dataModel = DataModel(name: "Sira", value: "test")
<em>// Initial Transaction for Your Store Procedure Model</em>
let transaction = Transaction(transactionID: 1,
transactionType: .sp,
parameters: dataModel)
<em>// Initial File Transaction Model</em>
let file = FileTransaction(sourcePath: "Sync/Files/work_1",
sourceFile: "Person.jpg",
destinationPath: "Work/work_1",
destinationFile: "Person.jpg")
<em>// Initial Transaction for File</em>
let transactionFile = Transaction(transactionID: 2,
transactionType: .file,
parameters: file)
<em>// Get File data for sync</em>
var fileDatas: [FileData] = []
if let image = UIImage(systemName: "person")?.jpgData() {
let fileData = FileData(data: image,
fileName: "Person.jpg",
fileDirectory: "work_1")
fileDatas.append(fileData)
}
<em>// Initial Work Model </em>
if let work = Offline.generateWork(workID: 1, transactions: [transaction, transactionFile]) {
<em>// Sync Data with works, files, userID and parameters</em>
Offline.syncData(works: [work], files: fileDatas, parameters: [:], userID: "005875") { (error, response) in
<em>// Callback return response or error</em>
print(error, response)
}
}