Incorrect Thumbnail, File Filter broken

Hey there,

My app is having a weird glitch after it is used several times. When the app is first installed, or running from Xcode, there is no glitch or error which is making this even harder to diagnose.

The app stores videos as MP4's and presents them in a tableview. It then creates a thumbnail and stores that as a JPG with a matching name, which is displayed in the tableview. The glitch that's occurring is the thumbnail doesn't match the video! I'll post my code for you guys to break apart.

1. func setJpg(){
2.                
3.         var jpgNames : [String] = []
4.         
5.         for string in jpgSTRINGS{
6.             let url = URL(string: string)
7.             jpgNames.append(url!.lastPathComponent)
8.         }
9.         
10. 
11. func setJpg(){
12.                
13.         var jpgNames : [String] = []
14.         
15.         for string in jpgSTRINGS{
16.             let url = URL(string: string)
17.             jpgNames.append(url!.lastPathComponent)
18.         }
19.         
20.         
21.         let droppedFormat = dropIntLast(string: videoString, int: 4)
22.         let filtered = jpgNames.filter{$0.hasPrefix(droppedFormat + ".jpg")}
23.         if (filtered.isEmpty){
24.             if (jpgMessages.count != videoURLS.count){
25.                 let resultArrays = checkJpgs(jpgSTRINGS: jpgSTRINGS, videoURLS: videoURLS)
26.                 jpgMessages = resultArrays.first! as! [String]
27.             }else{
28.                 print ("this right here maybe?")
29.             }
30.             let videoPosition = videoURLS.firstIndex(of: videoURL!)
31.             if (jpgMessages[videoPosition!] == ("The operation could not be completed")) || (jpgMessages[videoPosition!] == ("Cannot Open"))  {
32.                 firstFrame.image = #imageLiteral(resourceName: "error")
33.             }
34.         }
35.             
36.         //     --------if (filtered.isEmpty == false)--------
37.         else{
38. 
39.             let name = videoURL!.lastPathComponent
40.             let fixedNameOne = name.replacingOccurrences(of: " ", with: "%20", options: .literal)
41.             let fixedNameTwo = fixedNameOne.replacingOccurrences(of: "–", with: "%E2%80%93", options: .literal)
42.             let fixedNameThree = fixedNameTwo.replacingOccurrences(of: "#", with: "%23", options: .literal)
43.             let path = videoURL?.deletingLastPathComponent()
44.             let string = path!.absoluteString + dropIntLast(string: fixedNameThree, int: 4) + ".jpg"
45.             urlJpg = URL(string: string)
46.             do {
47.                 let data = try Data(contentsOf: urlJpg!, options: [.mappedIfSafe, .uncached])
48.                 let photo = UIImage(data: data)
49.                 firstFrame.image = photo
50.             }
51.             catch {
52.                 Swift.print("Error in Data reading ", error)                
53.             }
54.         }
55.     }

Basically, I have references to the JPG's and MP4's as URLs in arrays, as well as String arrays. videoString is the name of the MP4 referenced in the form of a string, which is the comparator for the filter. Line 22 filters though the jpgSTRINGS array to find the matching name there. If it can't find a matching name it will use my function checkJpgs to create one, or it will detect the MP4 is corrupt and a thumbnail can't be created and will use a default Error Icon. If it detects a JPG with a matching name it will get the find the file path and display that.

When the glitch occurs, it either displays the wrong thumbnail or the Error Icon. This makes me think the issue is on line 5 or 22, but I don't know how this is possible. The fact I can't replicate this issue when running from Xcode make it that much more to diagnose and collect data.

I'm sure there's a better way to filter through the JPGs but I'm not away of it. If anyone has any idea how to make this more efficient or less error prone please point me in the right direction.

Incorrect Thumbnail, File Filter broken
 
 
Q