Last active
December 8, 2016 17:14
-
-
Save sstadelman/8829c6b8c66a8d48b6b9905d78a5e5de to your computer and use it in GitHub Desktop.
Using the DataService streaming API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class DefaultContainer<Provider: DataServiceProvider>: DataService<Provider> { | |
public override init(provider: Provider) { | |
super.init(provider: provider) | |
self.provider.metadata = DefaultContainerMetadata.document | |
} | |
// ... | |
open func photos(query: DataQuery = DataQuery()) throws -> Array<Photo> { | |
return try Photo.array(from: self.executeQuery(query.from(DefaultContainerMetadata.EntitySets.photos)).entityList()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) | |
let object = objects[indexPath.row] | |
cell.textLabel!.text = object.name | |
return cell | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
if segue.identifier == "showDetail" { | |
if let indexPath = self.tableView.indexPathForSelectedRow { | |
let object = objects[indexPath.row] | |
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController | |
controller.detailItem = object | |
OperationQueue.main.addOperation { | |
let stream = try! self.ds.downloadMedia(entity: object) | |
let imageData = try! stream.readAndClose() | |
guard let image = UIImage(data: imageData) else { return } | |
OperationQueue.main.addOperation { | |
controller.detailImageView.image = image | |
} | |
} | |
controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem | |
controller.navigationItem.leftItemsSupplementBackButton = true | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import TripPin | |
import HCPOData | |
import HCPFoundation | |
class MasterViewController: UITableViewController, URLSessionDelegate { | |
var detailViewController: DetailViewController? = nil | |
var objects = [Photo]() { | |
didSet { | |
self.tableView.reloadData() | |
} | |
} | |
var session: HCPURLSession! | |
var ds: DefaultContainer<OnlineODataProvider>! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
session = HCPURLSession(configuration: .default, hcpUrlSessionDelegate: nil, urlSessionDelegate: nil) | |
let provider = OnlineODataProvider(serviceRoot: URLs.serviceRoot.url, session: session) | |
ds = DefaultContainer<OnlineODataProvider>(provider: provider) | |
// Do any additional setup after loading the view, typically from a nib. | |
self.navigationItem.leftBarButtonItem = self.editButtonItem | |
if let split = self.splitViewController { | |
let controllers = split.viewControllers | |
self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController | |
} | |
objects = try! ds.photos() | |
} | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment