From 7ad14f4dcd53cf25ceca2d976cb0f282c3e89d33 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Tue, 15 Sep 2015 21:48:10 +0800 Subject: [PATCH 1/5] better fakeID of conversation --- Yep/Realm/Models.swift | 4 ++-- Yep/Services/YepServiceSync.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index dda97c80..519224d0 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -365,11 +365,11 @@ class Conversation: Object { switch type { case ConversationType.OneToOne.rawValue: if let withFriend = withFriend { - return withFriend.userID + return "user" + withFriend.userID } case ConversationType.Group.rawValue: if let withGroup = withGroup { - return withGroup.groupID + return "group" + withGroup.groupID } default: break diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index 215f0525..603d5461 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -895,7 +895,7 @@ func syncMessageWithMessageInfo(messageInfo: JSONDictionary, inRealm realm: Real // Do furtherAction after sync - if let sectionDateMessageID = sectionDateMessageID{ + if let sectionDateMessageID = sectionDateMessageID { furtherAction?(messageIDs: [sectionDateMessageID, messageID]) } else { furtherAction?(messageIDs: [messageID]) From 10ce183e399c32561b0256bc3519204c81c7d7f5 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Wed, 16 Sep 2015 10:54:57 +0800 Subject: [PATCH 2/5] fix may crash when update introduction for cell (not on main thread) --- .../EditProfile/EditProfileViewController.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Yep/ViewControllers/EditProfile/EditProfileViewController.swift b/Yep/ViewControllers/EditProfile/EditProfileViewController.swift index d8fbf388..038a42b3 100644 --- a/Yep/ViewControllers/EditProfile/EditProfileViewController.swift +++ b/Yep/ViewControllers/EditProfile/EditProfileViewController.swift @@ -245,7 +245,9 @@ extension EditProfileViewController: UITableViewDataSource, UITableViewDelegate cell.annotationLabel.text = NSLocalizedString("Introduction", comment: "") YepUserDefaults.introduction.bindAndFireListener(Listener.Introduction) { [weak cell] introduction in - cell?.infoTextView.text = introduction ?? NSLocalizedString("Introduce yourself here.", comment: "") + dispatch_async(dispatch_get_main_queue()) { + cell?.infoTextView.text = introduction ?? NSLocalizedString("Introduce yourself here.", comment: "") + } } cell.infoTextViewDidEndEditingAction = { [weak self] newIntroduction in From 4398f013e01bc0e58ad9c3aacf7687fdc063fd4f Mon Sep 17 00:00:00 2001 From: nixzhu Date: Wed, 16 Sep 2015 11:03:41 +0800 Subject: [PATCH 3/5] make sure all bindListener & bindAndFireListener run closure on main thread --- .../Contacts/ContactsViewController.swift | 8 +++++-- .../ConversationViewController.swift | 4 +++- .../ConversationsViewController.swift | 8 +++++-- .../EditProfileViewController.swift | 21 +++++++++++-------- .../Profile/ProfileViewController.swift | 12 +++++++---- .../Settings/SettingsViewController.swift | 4 +++- .../ProfileHeader/ProfileHeaderCell.swift | 8 ++++--- .../Cells/SettingsUser/SettingsUserCell.swift | 12 ++++++++--- 8 files changed, 52 insertions(+), 25 deletions(-) diff --git a/Yep/ViewControllers/Contacts/ContactsViewController.swift b/Yep/ViewControllers/Contacts/ContactsViewController.swift index 2d280b7e..766c5cb7 100644 --- a/Yep/ViewControllers/Contacts/ContactsViewController.swift +++ b/Yep/ViewControllers/Contacts/ContactsViewController.swift @@ -78,11 +78,15 @@ class ContactsViewController: BaseViewController { contactsTableView.tableFooterView = UIView() YepUserDefaults.nickname.bindListener(Listener.Nickname) { [weak self] _ in - self?.updateContactsTableView() + dispatch_async(dispatch_get_main_queue()) { + self?.updateContactsTableView() + } } YepUserDefaults.avatarURLString.bindListener(Listener.Avatar) { [weak self] _ in - self?.updateContactsTableView() + dispatch_async(dispatch_get_main_queue()) { + self?.updateContactsTableView() + } } } diff --git a/Yep/ViewControllers/Conversation/ConversationViewController.swift b/Yep/ViewControllers/Conversation/ConversationViewController.swift index afd3285f..650a63bf 100644 --- a/Yep/ViewControllers/Conversation/ConversationViewController.swift +++ b/Yep/ViewControllers/Conversation/ConversationViewController.swift @@ -245,7 +245,9 @@ class ConversationViewController: BaseViewController { NSNotificationCenter.defaultCenter().addObserver(self, selector: "tryInsertInActiveNewMessages:", name: AppDelegate.Notification.applicationDidBecomeActive, object: nil) YepUserDefaults.avatarURLString.bindListener(Listener.Avatar) { [weak self] _ in - self?.reloadConversationCollectionView() + dispatch_async(dispatch_get_main_queue()) { + self?.reloadConversationCollectionView() + } } swipeUpView.hidden = true diff --git a/Yep/ViewControllers/Conversations/ConversationsViewController.swift b/Yep/ViewControllers/Conversations/ConversationsViewController.swift index 18a2c57a..7b4caa0e 100644 --- a/Yep/ViewControllers/Conversations/ConversationsViewController.swift +++ b/Yep/ViewControllers/Conversations/ConversationsViewController.swift @@ -59,11 +59,15 @@ class ConversationsViewController: UIViewController { NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadConversationsTableView", name: YepNewMessagesReceivedNotification, object: nil) YepUserDefaults.nickname.bindListener(Listener.Nickname) { [weak self] _ in - self?.reloadConversationsTableView() + dispatch_async(dispatch_get_main_queue()) { + self?.reloadConversationsTableView() + } } YepUserDefaults.avatarURLString.bindListener(Listener.Avatar) { [weak self] _ in - self?.reloadConversationsTableView() + dispatch_async(dispatch_get_main_queue()) { + self?.reloadConversationsTableView() + } } view.backgroundColor = UIColor.whiteColor() diff --git a/Yep/ViewControllers/EditProfile/EditProfileViewController.swift b/Yep/ViewControllers/EditProfile/EditProfileViewController.swift index 038a42b3..7460e30e 100644 --- a/Yep/ViewControllers/EditProfile/EditProfileViewController.swift +++ b/Yep/ViewControllers/EditProfile/EditProfileViewController.swift @@ -220,19 +220,22 @@ extension EditProfileViewController: UITableViewDataSource, UITableViewDelegate cell.annotationLabel.text = NSLocalizedString("Nickname", comment: "") YepUserDefaults.nickname.bindAndFireListener(Listener.Nickname) { [weak cell] nickname in - cell?.infoLabel.text = nickname + dispatch_async(dispatch_get_main_queue()) { + cell?.infoLabel.text = nickname + } } YepUserDefaults.badge.bindAndFireListener(Listener.Badge) { [weak cell] badgeName in + dispatch_async(dispatch_get_main_queue()) { + if let badgeName = badgeName, badge = BadgeView.Badge(rawValue: badgeName) { + cell?.badgeImageView.image = badge.image + cell?.badgeImageView.tintColor = badge.color + cell?.infoLabelTrailingConstraint.constant = EditProfileLessInfoCell.ConstraintConstant.normalInfoLabelTrailing - if let badgeName = badgeName, badge = BadgeView.Badge(rawValue: badgeName) { - cell?.badgeImageView.image = badge.image - cell?.badgeImageView.tintColor = badge.color - cell?.infoLabelTrailingConstraint.constant = EditProfileLessInfoCell.ConstraintConstant.normalInfoLabelTrailing - - } else { - cell?.badgeImageView.image = nil - cell?.infoLabelTrailingConstraint.constant = EditProfileLessInfoCell.ConstraintConstant.minInfoLabelTrailing + } else { + cell?.badgeImageView.image = nil + cell?.infoLabelTrailingConstraint.constant = EditProfileLessInfoCell.ConstraintConstant.minInfoLabelTrailing + } } } diff --git a/Yep/ViewControllers/Profile/ProfileViewController.swift b/Yep/ViewControllers/Profile/ProfileViewController.swift index 73f08d25..b1ba4c96 100644 --- a/Yep/ViewControllers/Profile/ProfileViewController.swift +++ b/Yep/ViewControllers/Profile/ProfileViewController.swift @@ -215,9 +215,11 @@ class ProfileViewController: UIViewController { if user.friendState == UserFriendState.Me.rawValue { YepUserDefaults.introduction.bindListener(Listener.Introduction) { [weak self] introduction in - if let introduction = introduction { - self?.introductionText = introduction - self?.updateProfileCollectionView() + dispatch_async(dispatch_get_main_queue()) { + if let introduction = introduction { + self?.introductionText = introduction + self?.updateProfileCollectionView() + } } } } @@ -416,7 +418,9 @@ class ProfileViewController: UIViewController { if user.friendState == UserFriendState.Me.rawValue { YepUserDefaults.nickname.bindListener(Listener.Nickname) { [weak self] nickname in - self?.customNavigationItem.title = nickname + dispatch_async(dispatch_get_main_queue()) { + self?.customNavigationItem.title = nickname + } } } } diff --git a/Yep/ViewControllers/Settings/SettingsViewController.swift b/Yep/ViewControllers/Settings/SettingsViewController.swift index cd42a275..4de4967b 100644 --- a/Yep/ViewControllers/Settings/SettingsViewController.swift +++ b/Yep/ViewControllers/Settings/SettingsViewController.swift @@ -59,7 +59,9 @@ class SettingsViewController: BaseViewController { settingsTableView.registerNib(UINib(nibName: settingsMoreCellIdentifier, bundle: nil), forCellReuseIdentifier: settingsMoreCellIdentifier) YepUserDefaults.introduction.bindAndFireListener(Listener.Introduction) { [weak self] introduction in - self?.settingsTableView.reloadData() + dispatch_async(dispatch_get_main_queue()) { + self?.settingsTableView.reloadData() + } } if let gestures = navigationController?.view.gestureRecognizers { diff --git a/Yep/Views/Cells/ProfileHeader/ProfileHeaderCell.swift b/Yep/Views/Cells/ProfileHeader/ProfileHeaderCell.swift index 6b1b49a9..97cd76dc 100644 --- a/Yep/Views/Cells/ProfileHeader/ProfileHeaderCell.swift +++ b/Yep/Views/Cells/ProfileHeader/ProfileHeaderCell.swift @@ -81,9 +81,11 @@ class ProfileHeaderCell: UICollectionViewCell { if user.friendState == UserFriendState.Me.rawValue { YepUserDefaults.avatarURLString.bindListener(Listener.Avatar) { [weak self] avatarURLString in - if let avatarURLString = avatarURLString { - self?.blurredAvatarImage = nil // need reblur - self?.updateAvatarWithAvatarURLString(avatarURLString) + dispatch_async(dispatch_get_main_queue()) { + if let avatarURLString = avatarURLString { + self?.blurredAvatarImage = nil // need reblur + self?.updateAvatarWithAvatarURLString(avatarURLString) + } } } diff --git a/Yep/Views/Cells/SettingsUser/SettingsUserCell.swift b/Yep/Views/Cells/SettingsUser/SettingsUserCell.swift index 07717b70..77a3de6b 100644 --- a/Yep/Views/Cells/SettingsUser/SettingsUserCell.swift +++ b/Yep/Views/Cells/SettingsUser/SettingsUserCell.swift @@ -38,15 +38,21 @@ class SettingsUserCell: UITableViewCell { avatarImageViewWidthConstraint.constant = avatarSize YepUserDefaults.avatarURLString.bindAndFireListener(Listener.Avatar) { [weak self] _ in - self?.updateAvatar() + dispatch_async(dispatch_get_main_queue()) { + self?.updateAvatar() + } } YepUserDefaults.nickname.bindAndFireListener(Listener.Nickname) { [weak self] nickname in - self?.nameLabel.text = nickname + dispatch_async(dispatch_get_main_queue()) { + self?.nameLabel.text = nickname + } } YepUserDefaults.introduction.bindAndFireListener(Listener.Introduction) { [weak self] introduction in - self?.introLabel.text = introduction + dispatch_async(dispatch_get_main_queue()) { + self?.introLabel.text = introduction + } } introLabel.font = YepConfig.Settings.introFont From f53418b7d96fbe86c626997d0c381ec024b3b9a0 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Wed, 16 Sep 2015 11:08:50 +0800 Subject: [PATCH 4/5] do syncMessageWithMessageInfo on main thread --- Yep/Services/FayeService.swift | 14 ++++++++------ Yep/Services/YepServiceSync.swift | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Yep/Services/FayeService.swift b/Yep/Services/FayeService.swift index bd2bac48..244fbf88 100644 --- a/Yep/Services/FayeService.swift +++ b/Yep/Services/FayeService.swift @@ -160,12 +160,14 @@ class FayeService: NSObject, MZFayeClientDelegate { private func saveMessageWithMessageInfo(messageInfo: JSONDictionary) { //这里不用 realmQueue 是为了下面的通知同步,用了 realmQueue 可能导致数据更新慢于通知 - let realm = Realm() - - syncMessageWithMessageInfo(messageInfo, inRealm: realm) { messageIDs in - dispatch_async(dispatch_get_main_queue()) { - let object = ["messageIDs": messageIDs] - NSNotificationCenter.defaultCenter().postNotificationName(YepNewMessagesReceivedNotification, object: object) + dispatch_async(dispatch_get_main_queue()) { + let realm = Realm() + + syncMessageWithMessageInfo(messageInfo, inRealm: realm) { messageIDs in + dispatch_async(dispatch_get_main_queue()) { + let object = ["messageIDs": messageIDs] + NSNotificationCenter.defaultCenter().postNotificationName(YepNewMessagesReceivedNotification, object: object) + } } } } diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index 603d5461..c8a71693 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -607,10 +607,11 @@ private func syncGroupWithGroupInfo(groupInfo: JSONDictionary, inRealm realm: Re func syncUnreadMessagesAndDoFurtherAction(furtherAction: (messageIDs: [String]) -> Void) { unreadMessages { allUnreadMessages in + //println("\n allUnreadMessages: \(allUnreadMessages)") - println("Got unread message \(allUnreadMessages.count)") + println("Got unread message: \(allUnreadMessages.count)") - dispatch_async(realmQueue) { + dispatch_async(dispatch_get_main_queue()) { let realm = Realm() @@ -623,7 +624,6 @@ func syncUnreadMessagesAndDoFurtherAction(furtherAction: (messageIDs: [String]) } // do futher action - println("加个打印,希望能等到 Realm 在线程间同步好") furtherAction(messageIDs: messageIDs) } } From 50044a3678249b683a6c7011e51ad380af2e7980 Mon Sep 17 00:00:00 2001 From: nixzhu Date: Wed, 16 Sep 2015 11:19:18 +0800 Subject: [PATCH 5/5] better logic for create new SectionDate Message --- Yep/Realm/Models.swift | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index 519224d0..3ad03156 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -585,15 +585,25 @@ func tryCreateSectionDateMessageInConversation(conversation: Conversation, befor if message.createdUnixTime - prevMessage.createdUnixTime > 180 { // TODO: Time Section - // insert a new SectionDate Message - let newSectionDateMessage = Message() - newSectionDateMessage.conversation = conversation - newSectionDateMessage.mediaType = MessageMediaType.SectionDate.rawValue - newSectionDateMessage.createdUnixTime = message.createdUnixTime - YepConfig.Message.sectionOlderTimeInterval // 比新消息早一点点即可 - newSectionDateMessage.arrivalUnixTime = message.arrivalUnixTime - YepConfig.Message.sectionOlderTimeInterval // 比新消息早一点点即可 - newSectionDateMessage.messageID = "sectionDate-\(newSectionDateMessage.createdUnixTime)" + // 比新消息早一点点即可 + let sectionDateMessageCreatedUnixTime = message.createdUnixTime - YepConfig.Message.sectionOlderTimeInterval + let sectionDateMessageID = "sectionDate-\(sectionDateMessageCreatedUnixTime)" - success(newSectionDateMessage) + if let _ = messageWithMessageID(sectionDateMessageID, inRealm: realm) { + // do nothing + } else { + // create a new SectionDate Message + let newSectionDateMessage = Message() + newSectionDateMessage.messageID = sectionDateMessageID + + newSectionDateMessage.conversation = conversation + newSectionDateMessage.mediaType = MessageMediaType.SectionDate.rawValue + + newSectionDateMessage.createdUnixTime = sectionDateMessageCreatedUnixTime + newSectionDateMessage.arrivalUnixTime = sectionDateMessageCreatedUnixTime + + success(newSectionDateMessage) + } } } }