循环内的完成处理程序?

时间:2017-08-24 00:58:45

标签: swift multithreading swift3 queue

我有一个接收消息数组的方法。 它检查消息是否包含媒体项目,如果它确实包含媒体项目,则它将检查媒体是否已经缓存在设备上,如果未保存,它将下载图像并将其保存在设备上。登记/> 在每次迭代结束时,媒体项目或文本将显示在集合视图控制器中。

一切正常但是它们没有按照绝对正常的顺序添加,所以我想知道我能做些什么来克服这个问题?

This is how it should be shown 
------------------------------
Text
Picture -> not cached
Text
Text
Picture -> Cached
Text
Picture -> not cached


This is how it appears
----------------------
Text
Text
Text
Text
Picture -> Cached
Picture -> not cached
Picture -> not cached

更新目前我的代码并不是很漂亮:D

func singleSBDtoJSQ(messages:[SBDBaseMessage]) {
    print("sbmToJSQ")
    for message in messages {



        if let userMessage = message as? SBDUserMessage {

            let jsqMessage = CustomJSQMessage(senderId: userMessage.sender?.userId, senderDisplayName: userMessage.sender?.nickname, date: Date(ticks: UInt64(userMessage.createdAt)), text: userMessage.message)

            if userMessage.sender?.userId == self.senderId {
                jsqMessage?.status = " ✓✓ "
            }
            jsqMessage?.sbdBaseMessage = message

            self.messages.append(jsqMessage!)

        } else {


            let fileMessage = message as! SBDFileMessage
            print("file message")
            let url = URL(string: fileMessage.url)
            let cacheResult = ImageCache.default.isImageCached(forKey: (url?.absoluteString)!)

            if cacheResult.cached {
                ImageCache.default.retrieveImage(forKey: (url?.absoluteString)!, options: nil, completionHandler: { (image, cacheType) in
                    if let cachedImage = image as? UIImage {
                        let mediaItem = JSQPhotoMediaItem()
                        mediaItem.image = cachedImage
                        let jsqMessage = CustomJSQMessage(senderId: fileMessage.sender?.userId, displayName: fileMessage.sender?.nickname, media: mediaItem)

                        print("Image caches")

                        if fileMessage.sender?.userId == self.senderId {
                            jsqMessage?.status = " ✓✓ "
                            mediaItem.appliesMediaViewMaskAsOutgoing = true
                        } else {
                            mediaItem.appliesMediaViewMaskAsOutgoing = false
                        }

                        jsqMessage?.sbdBaseMessage = message

                        self.messages.append(jsqMessage!)
                        return


                    } else {
                        return
                    }

                })

            } else {
                print("image not cached")
                let imageView = UIImageView()
                imageView.kf.setImage(with: url, completionHandler: {
                    (image, error, cacheType, imageUrl) in

                    if error != nil {
                        print("error while downloading image")
                        return
                    } else {
                        print("image downloaded")
                        let mediaItem = JSQPhotoMediaItem()
                        mediaItem.image = image
                        let jsqMessage = CustomJSQMessage(senderId: fileMessage.sender?.userId, displayName: fileMessage.sender?.nickname, media: mediaItem)

                        if fileMessage.sender?.userId == self.senderId {
                            jsqMessage?.status = " ✓✓ "
                            mediaItem.appliesMediaViewMaskAsOutgoing = true
                        } else {
                            mediaItem.appliesMediaViewMaskAsOutgoing = false
                        }

                        jsqMessage?.sbdBaseMessage = message

                        self.messages.append(jsqMessage!)

                    }

                })

            }
        }
    }
    self.finishSendingMessage(animated: true)
}

0 个答案:

没有答案