iosswiftfirebasefirebase-realtime-databasedata-handling

How to prepare content before it is even viewed?


I have an app like snapchat. On one table VC I fetch all the media 1 (first media) then when you tap on one of the media, I take you to table VC 2 where I format the data that came with the snapshot in table VC 1 (I save a snapshot to a post object).

Then when the user taps on the only cell that there is in table 2 he will be taken to a table VC 3 with all the media filling up the screen like snapchat.

My question: How can I make it so that I prepare the first media so it is instantly available when you go to the table 3?

Currently I do the following:

                    //numberMedia is always "media1" here
                if self.selectedPost?.media[0].imageURLString != nil {
                    print("thsi is an image")
                    let media = InterimMediaObject(imageURLString: (self.selectedPost?.media[0].imageURLString)!, timeStamp: (self.selectedPost?.media[0].timeStamp)!, numMedia: numberMedia)
                    media.postID = self.selectedPost?.media[0].postID
                    self.selectedPost?.interimMedia.append(media)

                    self.selectedPost?.interimMedia[0].image = self.getImage(urlString: (self.selectedPost?.interimMedia[0].imageURLString!)!)

                    print((self.selectedPost?.interimMedia[0].imageURLString!), "<-- here is img val??????")
                } else {
                    print("this is a video")

                    print((self.selectedPost?.media[0].videoURLString)!, "<--- this has val?")

                    self.selectedPost?.interimMedia.append(InterimMediaObject(videoURLString: (self.selectedPost?.media[0].videoURLString)!, timeStamp: (self.selectedPost?.media[0].timeStamp)!, numMedia: numberMedia, thumbnailString: (self.selectedPost?.media[0].thumbnail)!))
                    self.selectedPost?.interimMedia[0].thumbnailImage = self.getImage(urlString: (self.selectedPost?.interimMedia[0].thumbnailString!)!)
                    self.selectedPost?.interimMedia[0].videoURL = self.getVideoURL(stringUrl: self.selectedPost?.interimMedia[0].videoURLString!)
                    self.selectedPost?.interimMedia[0].postID = self.selectedPost?.media[0].postID

                    print(self.getVideoURL(stringUrl: self.selectedPost?.interimMedia[0].videoURLString!), "<-- nil or nil? TEWICE")
                }

getImage() or getVideo() whichever is needed will run and I have found it to be slow. That is: It takes about 1-3 seconds before I can tap to the table 3 otherwise the view will take time to load.

I would like to make it as fast as snapchat. How could I achieve that?


Solution

  • There is one and only way to make asynchronous looks synchronous which is by pre-fetching them , in your case you tap the cell in the 2nd vc then get image/video and navigate , to make this process more faster you need to fetch that media inside viewDidLoad of the 2nd vc so if the user goes to the 3rd vc there will be a high probability of finishing the fetch , fetching in firstVc will make it also more faster but it has a low probability for navigation which will cut from the user's network as there you would save all visible medias