0

I was implementing the photo media item and noticed some extreme performance issues. When I started debugging I noticed that messageDataForItemAtIndexPath was getting called 4 times for each item, whereas in another sample project I set up it was being called 10 times

Here's a dummy project I set up to test and it's calling each item 10 times even though there is only one message item.

This was causing problems for me as that's the function where I start my async download of media photos and calling the download media request 10 times for each item was causing problems for me.

Am I mis-understanding something basic here?

import UIKit
import JSQMessagesViewController

class ViewController: JSQMessagesViewController {

    var messages = [JSQMessage]()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.senderId = "1"
        self.senderDisplayName = "me"
        let message = JSQMessage(senderId: "admin", displayName: "Admin", text:"hello from admin")
        self.messages.append(message)
    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.messages.count
    }

    override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
        return JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor.blueColor())
    }

    override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
        return nil
    }

    override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {

        //why is this called 10 times?

        let msg = self.messages[indexPath.item];
        let text = msg.text

        print(text + ":" + String(indexPath.item))

        return JSQMessage(senderId: msg.senderId, displayName: msg.senderDisplayName, text: text)

    }

}

This will output in the console

hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
hello from admin:0
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

1 Answers1

0

It looks like this is by design.

Answered here:

https://github.com/jessesquires/JSQMessagesViewController/issues/1211

MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460
  • This is by design of UICollectionView. You should start downloading image if not started already. Just use a simple flag. Better yet use this library https://github.com/rs/SDWebImage. It will download and cache images for you. – oyalhi Sep 29 '16 at 14:15