Created
November 19, 2016 17:25
-
-
Save ShawnBaek/0c4060d483f0b04e3195a5e3ed19d7ed to your computer and use it in GitHub Desktop.
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 Parse | |
import ParseLiveQuery | |
import SDWebImage | |
import ActiveLabel | |
import MXParallaxHeader | |
class FeedVC: UITableViewController, ShopDetailDelegate { | |
var subscription: Subscription<Post>! | |
var postsQuery: PFQuery<Post> { | |
return (Post.query()? | |
.order(byAscending: "createdAt")) as! PFQuery<Post> | |
} | |
var client: Client { | |
return ParseLiveQuery.Client.shared | |
} | |
var results = [Post]() | |
//page size | |
var page : Int = 10 | |
//Default func | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
tableView?.backgroundColor = UIColor(red: 0.0 / 255.0, green: 0.0 / 255.0, blue: 0.0 / 255.0, alpha: 1) | |
// Automatic row height | |
tableView.estimatedRowHeight = 450 | |
tableView.rowHeight = UITableViewAutomaticDimension | |
// Start the subscription | |
subscription = client.subscribe(postsQuery).handleSubscribe { [weak self] (_) in | |
// Fetch the objects on subscription to not miss any | |
self?.fetchObjects() | |
}.handleEvent { [weak self] (_, event) in | |
self?.handleEvent(event: event) | |
} | |
} | |
func index(ofResult result: Post) -> Array<Post>.Index? { | |
return results.index(where: { | |
$0.objectId == result.objectId | |
}) | |
} | |
func handleEvent(event: Event<Post>) { | |
// Make sure we're on main thread | |
if Thread.current != Thread.main { | |
return DispatchQueue.main.async { [weak self] _ in | |
self?.handleEvent(event: event) | |
} | |
} | |
switch event { | |
case .created(let obj), | |
.entered(let obj): | |
results.insert(obj, at: 0) | |
tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic) | |
case .updated(let obj): | |
guard let index = index(ofResult: obj) else { break } | |
results.remove(at: index) | |
results.insert(obj, at: 0) | |
tableView.moveRow(at: IndexPath(row: index, section: 0), to: IndexPath(row: 0, section: 0)) | |
tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .automatic) | |
case .deleted(let obj), | |
.left(let obj): | |
guard let index = index(ofResult: obj) else { break } | |
results.remove(at: index) | |
tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic) | |
} | |
} | |
func fetchObjects() { | |
print("fetchObjects()") | |
postsQuery.findObjectsInBackground().continue(with: BFExecutor.mainThread(), with: { (task) -> Any? in | |
guard let objects = task.result as? [Post] else { | |
return nil | |
} | |
self.results = objects | |
self.tableView.reloadData() | |
print("Got it!!!") | |
print(objects) | |
return nil | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment