2016-01-24 54 views
0

最近我一直在我的ios Messenger的一些麻煩。起初我只提取短信,一切都很完美。當我試圖從解析中獲取圖像時,我成功了;但是,Feed沒有按照正確的順序排列。 好像它無視「query.orderByAscending」完全...Parse Messenger聊天Swift:信息有問題

fetchMessages() 
{ 
    currentUser = PFUser.currentUser()! 
    let query = PFQuery(className:"Messages") 
    query.whereKey("convoid", equalTo:convoid) 
    query.orderByAscending("createdAt") 
    query.cachePolicy = .NetworkElseCache 
    query.findObjectsInBackgroundWithBlock { 
     (objects: [PFObject]?, error: NSError?) -> Void in 
     if error == nil { 
      dispatch_async(dispatch_get_main_queue()) { 
      if let objects = objects { 
       for object in objects { 
       if(object["fileType"] as? String == "photo"){ 
        if(object["senderId"] as? String == self.currentUser.objectId!){ 
         let userImageFile = object["file"] as! PFFile 
         userImageFile.getDataInBackgroundWithBlock { 
          (imageData: NSData?, error: NSError?) -> Void in 
          if error == nil { 
           let imageddata = UIImage(data:imageData!) 
           let chatBubbleData = ChatBubbleData(text: "", image:imageddata, date: object.createdAt, type: .Mine) 
           self.addChatBubble(chatBubbleData) 
           self.chatBubbleDatas.append(chatBubbleData) 
          } 
         } 
        }else{ 
         let userImagefile = object["file"] as! PFFile 
         userImagefile.getDataInBackgroundWithBlock { 
          (imageData: NSData?, error: NSError?) -> Void in 
          if error == nil { 
           let imageddata = UIImage(data:imageData!) 
           let chatBubbleData = ChatBubbleData(text: "", image:imageddata, date: object.createdAt, type: .Opponent) 
           self.addChatBubble(chatBubbleData) 
           self.chatBubbleDatas.append(chatBubbleData) 

          } 
         } 
        } 
       }else{ 
        if(object["senderId"] as? String == self.currentUser.objectId!){ 
         let chatBubbleData = ChatBubbleData(text: object["text"] as? String, image:nil, date: object.createdAt, type: .Mine) 
          self.addChatBubble(chatBubbleData) 
          self.chatBubbleDatas.append(chatBubbleData) 
        }else{ 
         let chatBubbleData = ChatBubbleData(text: object["text"] as? String, image:nil, date: object.createdAt, type: .Opponent) 
          self.addChatBubble(chatBubbleData) 
          self.chatBubbleDatas.append(chatBubbleData) 
        } 

       } 
       } 
       } 
      } 
     } else { 
      print("Error: \(error!) \(error!.userInfo)") 
     } 
    } 
    self.messageCointainerScroll.contentSize = CGSizeMake(CGRectGetWidth(messageCointainerScroll.frame), lastChatBubbleY + internalPadding) 
    self.addKeyboardNotifications() 
} 

工作一切良好,除了事實的消息視圖不按照正確的順序列出的所有消息。實際上,所有的短信都是按照正確的順序排列的,但無論情況如何,無論創建的日期如何,消息圖像總是會出現。我認爲它必須加載一些東西;但我知道迅速,我不完全確定。任何關於修復或參考的見解請分享!

回答

0

我的第一個猜測是,它與您的通話安置辦主隊列操作做: dispatch_async(dispatch_get_main_queue()) { 你可能只是想這樣做,當你需要更新的最終用戶界面。問題是,花費較長時間的操作可能會在整個塊在後臺運行時花費較少時間(在不同的隊列上,因此您不知道調度是什麼主隊列...)。

經過進一步的檢查看來你有一大堆的其他背景的塊例如爲: userImagefile.getDataInBackgroundWithBlock 我寫這樣的代碼使用解析和JSQMessagesViewController我以爲是你在做什麼內部調用。我強烈建議您找到解決方案,通過網絡將您的用戶界面更新與模型更新分離。你現在所擁有的不僅難以調試,而且可能導致你的問題難以檢測異步方式。

+0

感謝您的建議,真的很感激!我一定會嘗試通過網絡玩弄UI更新和模型更新 –

0

它實際上使用query.orderByAscending("createdAt")來獲取對象。只需在query.findObjectsInBackgroundWithBlock的內部添加print(objects),您就可以看到以正確順序打印的所有對象。

不正確的順序是加載圖像的結果。當你得到一個文本對象時,你立即追加數據。但是,當對象是圖像時,您可以調用userImageFile.getDataInBackgroundWithBlock,它在調用附加數據的塊之前下載圖像。

你需要做的是爲圖像添加一個佔位符,使其位於正確的位置,然後在圖像完成下載時進行更新。


作爲一個側面說明,你可能想看看subclassing PFObject,這將擺脫所有as? if語句和擺脫Pyramid of Doom的。

+0

有趣但佔位符會在哪裏發揮作用?會在之前? userImageFile.getDataInBackgroundWithBlock肯定會嘗試玩這個謝謝! –

+0

是的,'userImageFile.getDataInBackgroundWithBlock'增加了一個延遲,這會擾亂訂單。在此之前添加佔位符不會有任何延遲。 –

+0

謝謝你非常感謝生病嘗試看看它是否工作 –