Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. //
  2. // TestLiveTableViewController.swift
  3. // TestLive
  4. //
  5. // Created by Fred van Rijswijk on 31-05-16.
  6. // Copyright © 2016 HalloBezorger. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import Parse
  11. import ParseUI
  12. import ParseLiveQuery
  13.  
  14. class TestLiveTableViewController: UITableViewController {
  15.  
  16.  
  17. //Store chat rooms of type PFObject
  18. var rooms = [PFObject]()
  19. //Store users of type PFUser
  20. var users = [PFUser]()
  21.  
  22. //-------------
  23.  
  24. var messageObjects = [PFObject]()
  25.  
  26.  
  27. private var subscription: Subscription<Message>?
  28.  
  29. var myQuery: PFQuery {
  30. return (Message.query()?
  31. .whereKey("roomName", equalTo: "test")
  32. .orderByAscending("createdAt"))!
  33. }
  34.  
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37.  
  38.  
  39. let username = "Tes"
  40. let password = "testtest"
  41.  
  42. PFUser.logInWithUsernameInBackground(username, password: password).continueWithBlock { task in
  43. return nil
  44. }
  45.  
  46. self.printPriorMessages()
  47. self.subscribeToUpdates()
  48.  
  49. }
  50.  
  51. override func viewWillAppear(animated: Bool) {
  52. super.viewWillAppear(true)
  53.  
  54. //If there is a user logged in, load data.
  55. // if PFUser.currentUser() != nil {
  56. // loadData()
  57. // }
  58. }
  59.  
  60. func printPriorMessages() {
  61. myQuery.findObjectsInBackground().continueWithBlock() { task in
  62. (task.result as? [Message])?.forEach(self.printMessage)
  63. print("printPrioMessages")
  64. self.tableView.reloadData()
  65. return nil
  66. }
  67. }
  68.  
  69. func subscribeToUpdates() {
  70. subscription = liveQueryClient
  71. .subscribe(myQuery)
  72. .handle(Event.Created) { _, message in
  73. self.printMessage(message)
  74. print("subscribe updates")
  75. //self.messageObjects.append(message)
  76.  
  77. }
  78. }
  79.  
  80. private func printMessage(message: Message) {
  81. let createdAt = message.createdAt ?? NSDate()
  82. //print("printMessage")
  83. self.messageObjects.append(message)
  84. print("\(createdAt) \(message.authorName ?? "unknown"): \(message.message ?? "")")
  85. }
  86.  
  87.  
  88.  
  89. // DESCRIPTION: TABLE VIEW DATA SOURCE
  90.  
  91.  
  92. override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
  93. //Return the number of sections
  94. return 1
  95. }
  96.  
  97. override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  98. // Return the number of rows in the section
  99. return messageObjects.count
  100. }
  101.  
  102. override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  103. return 80
  104. }
  105.  
  106. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> PFTableViewCell? {
  107.  
  108. let cell: PFTableViewCell! = tableView.dequeueReusableCellWithIdentifier("cellChat", forIndexPath: indexPath) as? PFTableViewCell
  109. //let targetUser = users[indexPath.row]
  110. //cell.nameLabel.text =
  111. cell.textLabel?.text = "test adding"
  112.  
  113. return cell
  114. }
  115.  
  116. override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
  117.  
  118. }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement