0

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?

1 Answers1

1

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

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • I fetch in var selectedPost : Post? { didSet { FormatPostdata() } } Because the code is a bit wirder than just table VCs i used that to simplify. the second is an imbeded tableView, so when selected posts recieves a value: taped on, I format that data. It prety fast, but not fast enugh in my opinion. The problem is when you tap on view (go to 3rd) the first media whether its a video or image is slow to load. –  May 09 '19 at 00:43
  • then you need to make that load inside `FormatPostdata` don't wait until the user tabs – Shehata Gamal May 09 '19 at 00:45
  • I know what you mean but I think you are misunderstanding something: So to clear that up this is how It runs -> [user taps cell in 1st]{ set selectedpost to the selected post then run formatData()} [User taps view button (to go to 3rd)] {by this time format data is basicaly done: exept for the converting of the media1 from a url refrence to a hard data like UIImage} This last part: turning media1 into hard data so it can be viewd instantly is what is slow –  May 09 '19 at 00:48
  • ok , you need to call what inside the view action which is converting the url to data end of `FormatPostdata` ? – Shehata Gamal May 09 '19 at 00:57
  • Inside there all I do is I first pass pass over some data to the new vc and then I do baseVC.expandToT3 –  May 09 '19 at 02:26
  • IS this whaty snapchat does? –  May 09 '19 at 05:10
  • @NCT127 being snapchat or any other well-known app doesn't make it do the thing magically , there are large groups of developers each may work in 1 activity – Shehata Gamal May 09 '19 at 18:43