Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // TestLiveTableViewController.swift
- // TestLive
- //
- // Created by Fred van Rijswijk on 31-05-16.
- // Copyright © 2016 HalloBezorger. All rights reserved.
- //
- import UIKit
- import Parse
- import ParseUI
- import ParseLiveQuery
- class TestLiveTableViewController: UITableViewController {
- //Store chat rooms of type PFObject
- var rooms = [PFObject]()
- //Store users of type PFUser
- var users = [PFUser]()
- //-------------
- var messageObjects = [PFObject]()
- private var subscription: Subscription<Message>?
- var myQuery: PFQuery {
- return (Message.query()?
- .whereKey("roomName", equalTo: "test")
- .orderByAscending("createdAt"))!
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- let username = "Tes"
- let password = "testtest"
- PFUser.logInWithUsernameInBackground(username, password: password).continueWithBlock { task in
- return nil
- }
- self.printPriorMessages()
- self.subscribeToUpdates()
- }
- override func viewWillAppear(animated: Bool) {
- super.viewWillAppear(true)
- //If there is a user logged in, load data.
- // if PFUser.currentUser() != nil {
- // loadData()
- // }
- }
- func printPriorMessages() {
- myQuery.findObjectsInBackground().continueWithBlock() { task in
- (task.result as? [Message])?.forEach(self.printMessage)
- print("printPrioMessages")
- self.tableView.reloadData()
- return nil
- }
- }
- func subscribeToUpdates() {
- subscription = liveQueryClient
- .subscribe(myQuery)
- .handle(Event.Created) { _, message in
- self.printMessage(message)
- print("subscribe updates")
- //self.messageObjects.append(message)
- }
- }
- private func printMessage(message: Message) {
- let createdAt = message.createdAt ?? NSDate()
- //print("printMessage")
- self.messageObjects.append(message)
- print("\(createdAt) \(message.authorName ?? "unknown"): \(message.message ?? "")")
- }
- // DESCRIPTION: TABLE VIEW DATA SOURCE
- override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
- //Return the number of sections
- return 1
- }
- override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- // Return the number of rows in the section
- return messageObjects.count
- }
- override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
- return 80
- }
- func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject!) -> PFTableViewCell? {
- let cell: PFTableViewCell! = tableView.dequeueReusableCellWithIdentifier("cellChat", forIndexPath: indexPath) as? PFTableViewCell
- //let targetUser = users[indexPath.row]
- //cell.nameLabel.text =
- cell.textLabel?.text = "test adding"
- return cell
- }
- override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement