我正在使用带有Meteor / Mongo后端的SwiftDDP / Swift 2.3。从一个视图导航到另一个视图只能获得每隔一段时间的结果。
以下是我的问题的视觉流程:
点击评论按钮/链接
获取结果并响应db的更新!宇!
通过标题中的后退按钮返回评论按钮/链接,然后再次单击
空结果!我可以在不重新启动调试器的情况下重复这些相同的步骤而不重新启动调试器。
我的模特
import Foundation
import SwiftDDP
class Comment: MeteorDocument {
var collection: String = "comments"
var id: String?
var body: String?
var userId: String?
var postId: String?
var createdAt: NSDate?
var userObj: User?
}
的viewController
import UIKit
import CoreData
import SwiftDDP
class CommentViewController: NavigableViewController, UITableViewDelegate, UITableViewDataSource {
//-- via AppDelegate.swift:
//-- let comments = MeteorCollection<Comment>(name: "comments")
var comments:MeteorCollection<Comment> = (UIApplication.sharedApplication().delegate as! AppDelegate).comments
override func viewDidLoad() {
super.viewDidLoad()
//-- connect to "comments" model
comments = MeteorCollection<Comment>(name: "comments")
//-- subscribe to postsComments meteor method on the server
Meteor.subscribe("postsComments", params: [["postId": self.source!.id!]]) {
self.createView()
self.tableView.reloadData()
}
}
ViewControllerUI
import UIKit
import CoreData
import SwiftDDP
extension CommentViewController {
func createView() {
//-- Use CommentTableViewCell for cell class
self.tableView.registerClass(CommentTableViewCell.self, forCellReuseIdentifier: CommentTableViewCell.reuseIdentifier)
self.makeHeader()
self.makeContentArea()
//-- Sign up the view to receive Meteor updates
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentViewController.reloadTableView), name: METEOR_COLLECTION_SET_DID_CHANGE, object: nil)
}
func makeHeader() {
header = UIView()
header.backgroundColor = UIColor.darkGrayColor()
self.view.addSubview(header)
header.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self.view)
make.left.equalTo(self.view)
make.right.equalTo(self.view)
make.height.equalTo(65)
}
let nameLabel = UILabel()
header.addSubview(nameLabel)
nameLabel.text = "Comments".localize()
nameLabel.textColor = UIColor.whiteColor()
nameLabel.font = Font.mediumBold
nameLabel.snp_makeConstraints { (make) -> Void in
make.top.equalTo(header).offset(34)
make.centerX.equalTo(header)
}
let backButton = UIButton()
backButton.tintColor = UIColor.whiteColor()
let backImage = UIImage(named: "ic_back")?.imageWithRenderingMode(.AlwaysTemplate)
backButton.setImage(backImage, forState: .Normal)
header.addSubview(backButton)
backButton.snp_makeConstraints { (make) -> Void in
make.left.equalTo(header).offset(4)
make.centerY.equalTo(nameLabel)
make.height.equalTo(25)
make.width.equalTo(25)
}
backButton.addTarget(self, action: #selector(didTapBack), forControlEvents: .TouchUpInside)
}
func makeContentArea() {
let content = UIView()
self.view.addSubview(content)
content.backgroundColor = Color.vcBackgroundColor
content.snp_makeConstraints { (make) -> Void in
make.top.equalTo(65)
make.left.equalTo(self.view)
make.right.equalTo(self.view)
make.height.equalTo(self.view)
}
//-- TableView for comment list
let contentTableView = UITableView(frame: content.frame)
content.addSubview(contentTableView)
contentTableView.backgroundColor = UIColor.clearColor()
self.tableView = contentTableView
contentTableView.delegate = self
contentTableView.dataSource = self
contentTableView.snp_makeConstraints { (make) -> Void in
make.top.equalTo(content)
make.left.equalTo(content)
make.right.equalTo(content)
make.height.equalTo(content).inset(65)
}
let commentInput = CustomTextField()
self.commentInput = commentInput
commentInput.backgroundColor = UIColor.whiteColor()
commentInput.textField.placeholder = "Write a comment...".localize()
content.addSubview(commentInput)
commentInput.snp_makeConstraints { (make) -> Void in
make.bottom.equalTo(content).inset(75)
make.left.equalTo(content)
make.right.equalTo(content)
make.height.equalTo(35)
}
}
}
表格代表
import UIKit
extension CommentViewController {
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.comments.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(CommentTableViewCell.reuseIdentifier, forIndexPath: indexPath) as UITableViewCell
if let card = cell as? CommentTableViewCell {
let item = self.comments.sorted[indexPath.row]
card.populate(item)
}
return CommentTableViewCell()
}
func reloadTableView() {
self.tableView.reloadData()
}
}
CommentTableViewCell
import UIKit
class CommentTableViewCell: UITableViewCell, ContextLabelDelegate {
var data:Comment!
var profilePicture:UIImageView!
var commentLabel:UILabel!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = UITableViewCellSelectionStyle.None
self.createView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func createView() {
profilePicture = UIImageView()
profilePicture.backgroundColor = Color.darkGrey
self.addSubview(profilePicture)
profilePicture.contentMode = UIViewContentMode.ScaleAspectFill
profilePicture.layer.cornerRadius = CGFloat(45 / 2)
profilePicture.clipsToBounds = true
profilePicture.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self).offset(12)
make.left.equalTo(self).offset(12)
make.width.equalTo(45)
make.height.equalTo(45)
}
commentLabel = UILabel()
commentLabel.textColor = UIColor.blackColor()
commentLabel.backgroundColor = Color.offwhite
commentLabel.font = Font.smallLight
self.addSubview(commentLabel)
commentLabel.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self).offset(12)
make.left.equalTo(65)
make.width.equalTo(self).offset(-80)
make.bottom.equalTo(45)
}
}
func populate(data: Comment) {
self.data = data
MeteorAPI.sharedInstance.getUserProfile(data.userId!) { (user) in
if let profileImage = user.profile?.picture {
let nsUrl = NSURL(string: profileImage)
self.profilePicture.af_setImageWithURL(nsUrl!)
} else {
let nsUrl = NSURL(string: "https://placekitten.com/g/200/300")
self.profilePicture.af_setImageWithURL(nsUrl!)
}
}
commentLabel.text = "This is a test comment. It's going to be yuge! Some other comments like this will be great, but mine will be the best!"
}
static var reuseIdentifier: String {
get {
return "CommentTableViewCell"
}
}
}
在此处包含自定义NavigableViewController。我不认为这是问题的一部分,但不想遗漏。
class NavigableViewController: UIViewController {
func pushView(viewController: UIViewController) {
self.addChildViewController(viewController)
self.view.addSubview(viewController.view)
viewController.view.layer.shadowOpacity = 0.8
viewController.view.layer.shadowOffset = CGSizeMake(5, -4)
self.viewWillDisappear(true)
viewController.viewWillAppear(true)
self.animateFromTheRight(viewController)
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.view.layoutSubviews()
}) { (finished) -> Void in }
}
func popView(viewController: UIViewController) {
viewController.view.layer.shadowOffset = CGSizeMake(-5, -4)
viewController.viewWillDisappear(true)
self.viewWillAppear(true)
self.animateToTheRight(viewController)
UIView.animateWithDuration(0.3, animations: { () -> Void in
self.view.layoutSubviews()
}, completion: { (finished) -> Void in
viewController.view.removeFromSuperview()
viewController.removeFromParentViewController()
})
}
/**
Is Called when the Current VC Index is lower than the VC to be shown index
- parameter vc: The VC To be presented
*/
func animateFromTheRight(vc: UIViewController) {
vc.view.snp_makeConstraints { make in
make.top.equalTo(self.view)
make.left.equalTo(self.view.snp_right)
make.bottom.equalTo(self.view)
make.width.equalTo(UIScreen.mainScreen().bounds.width)
}
vc.didMoveToParentViewController(self)
self.view.layoutSubviews()
vc.view.snp_remakeConstraints { make in
make.top.equalTo(self.view)
make.left.equalTo(self.view.snp_left)
make.bottom.equalTo(self.view)
make.width.equalTo(UIScreen.mainScreen().bounds.width)
}
}
func animateToTheRight(vc: UIViewController) {
/*
vc.view.snp_makeConstraints { (make) -> Void in
make.top.equalTo(self.view)
make.left.equalTo(self.view.snp_left)
make.bottom.equalTo(self.view)
make.width.equalTo(UIScreen.mainScreen().bounds.width)
}
*/
self.view.layoutSubviews()
vc.view.snp_remakeConstraints { (make) -> Void in
make.top.equalTo(self.view)
make.left.equalTo(self.view.snp_right)
make.bottom.equalTo(self.view)
make.width.equalTo(UIScreen.mainScreen().bounds.width)
}
}
}
来自调试器的日志 在第一次评论时点击:
[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x61000025e9c0 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
collection = comments;
fields = {
--fields redacted--
}
[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x60800025daf0 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
collection = comments;
fields = {
--fields redacted--
}
[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x610000442730 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
msg = ready;
subs = (
4799450060338752685
);
}
点击后退按钮
时记录[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x618000447e60 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
collection = comments;
id = 58899570593460ce53ca338c;
msg = removed;
}
[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x60800025fda0 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
collection = comments;
id = 58899577593460ce53ca338d;
msg = removed;
}
[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x610000441aa0 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
id = 4799450060338752685;
msg = nosub;
}
[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x618000445130 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
id = 4799450060338752685;
msg = nosub;
}
登录第二条评论点击
[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x6100004450a0 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
collection = comments;
fields = {
--fields redacted--
};
id = 58899570593460ce53ca338c;
msg = added;
}
[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x60800025db80 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
collection = comments;
fields = {
--fields redacted--
};
id = 58899577593460ce53ca338d;
msg = added;
}
[Debug] [DDP] [DDP Background Data Queue :: NSOperation 0x618000448610 (QOS: BACKGROUND)] [260] ddpMessageHandler > Received message: {
msg = ready;
subs = (
4799450060338752685
);
}