Skip to content

Instantly share code, notes, and snippets.

@yoxisem544
Forked from jenhausu/model.swift
Last active June 22, 2017 01:26
Show Gist options
  • Save yoxisem544/5fd191e6fe269d13cdfb6a7caec28a14 to your computer and use it in GitHub Desktop.
Save yoxisem544/5fd191e6fe269d13cdfb6a7caec28a14 to your computer and use it in GitHub Desktop.
MVC
class func loadMessage(complete: (_ objects: Any, error: Error?) -> Void) {
let managerQuery = UserModel.query()!
let userRole = PFRole(withoutDataWithObjectId: "lEGrVrXO0a") // 角色:球場管理
managerQuery.whereKey("userRole", equalTo: userRole)
let caddy = UserModel.current()!
let courseID = caddy.course!
managerQuery.whereKey("course", equalTo: courseID)
managerQuery.findObjectsInBackground(block: { (objects, error) in
let manager = objects!.first as! UserModel
let sendQuery = iPadMessageTable.query()!
sendQuery.whereKey("sender", equalTo: caddy)
sendQuery.whereKey("receiver", equalTo: manager)
let receiverQuery = iPadMessageTable.query()!
receiverQuery.whereKey("sender", equalTo: manager)
receiverQuery.whereKey("receiver", equalTo: caddy)
let mainQuery = PFQuery.orQuery(withSubqueries: [sendQuery, receiverQuery])
mainQuery.order(byAscending: "createdAt")
mainQuery.whereKey("createdAt", greaterThan: GolfaceUtility.dateAtBeginningOfDay(for: Date()))
mainQuery.findObjectsInBackground { (objects, error) in
complete(objects, error)
}
})
}
NotificationCenter.default.addObserver(forName: Notification.Name(rawValue: "MessageLoadSuccess"), object: nil, queue: nil, using: { [unowned self] (notification) in
self.messages = [JSQMessage]()
for object in notification.object as! [iPadMessageTable] {
let senderID = object.sender!.objectId
let currentID = UserModel.current()!.objectId
let sendID = (senderID == currentID) ? self.user1.id : self.user2.id;
self.messages.append(JSQMessage(senderId: sendID, displayName: "", text: object.message))
}
self.collectionView.reloadData()
self.scrollToBottom(animated: false)
self.progressDelegate.hideProgressView()
for object in notification.object as! [iPadMessageTable] {
if object.read == false {
object.read = true
object.saveEventually()
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment