This guide contains example code for you to get started with writing your own queries for the Travel Expense service.
The dataService in each example is expected to be the sample Travel Expense service. In an app generated by the Assistant, this is exposed as a property on the AppDelegate class. To access this more conveniently, the following lines can be placed at the top of your view controller classes:
lazy var appDelegate = UIApplication.shared.delegate as! AppDelegate
lazy var dataService = appDelegate.travelexpense!The code in each of these examples can be used with any of the proxy classes. The data service provides fetch methods for each type, and any proxy class instance can be passed into the createEntity, updateEntity, and/or deleteEntity methods.
- Assume a property called
reportsof type[ExpenseReportItem].
// Order the results by the end date of the expense report, descending.
let query = DataQuery().orderBy(ExpenseReportItem.reportend, .descending)
// Fetch all expense reports, using the query parameters defined above.
dataService.fetchExpenseReports(matching: query) { [weak self] reports, error in
// Make sure no errors occurred.
guard let reports = reports else {
NSLog("Error: %@", error!.localizedDescription)
return
}
// Set the `reports` property and re-display the list.
self?.reports = reports
self?.tableView.reloadData()
}When displaying the details for a selected report, you will likely want to display the list of associated expense items.
- Assume a property called
reportof typeExpenseReportItem. - Assume a property called
expensesof type[ExpenseItem].
// Filter the results by the selected report ID, automatically retrieving
// associated records for the payment type and attachments, and ordering
// by the expense date, ascending.
let reportID = report.reportid!
let query = DataQuery().filter(ExpenseItem.reportid == reportID)
.expand(ExpenseItem.paymentType, ExpenseItem.attachments)
.orderBy(ExpenseItem.itemdate)
// Fetch all expense items, using the query parameters defined above.
dataService.fetchExpenseItems(matching: query) { [weak self] expenses, error in
// Make sure no errors occurred.
guard let expenses = expenses else {
NSLog("Error: %@", error!.localizedDescription)
return
}
// Set the `expenses` property and re-display the list.
self?.expenses = expenses
self?.tableView.reloadData()
}- Assume a property called
reportof typeExpenseReportItem. - Assume a property called
newExpenseof typeExpenseItem. - Assume the properties of
newExpensehave been filled in from the UI.
// Associate the expense with the parent report.
newExpense.reportid = report.reportid
// Create a new record for the expense.
dataService.createEntity(newExpense) { error in
// Make sure no errors occurred.
if let error = error {
NSLog("Error: %@", error.localizedDescription)
}
}- Assume a property called
existingExpenseof typeExpenseItem.
dataService.updateEntity(existingExpense) { error in
// Make sure no errors occurred.
if let error = error {
NSLog("Error: %@", error.localizedDescription)
}
}- Assume a property called
existingExpenseof typeExpenseItem.
dataService.deleteEntity(existingExpense) { error in
// Make sure no errors occurred.
if let error = error {
NSLog("Error: %@", error.localizedDescription)
}
}For reference, here are the OData entities that are part of the Travel Expense service data model, with the names of the proxy classes that have been generated.
| OData Entity | Proxy Class name |
|---|---|
| Expense Report | ExpenseReportItem |
| Report Status | ReportStatus |
| Expense Item | ExpenseItem |
| Currency | Currency |
| Expense Type | Expense |
| Payment Type | Payment |
| OData Entity | Proxy Class name |
|---|---|
| Trip | TripItem |
| Reservation Item | ReservationItem |
| Reservation Type | Reservation |