Home > Net >  "LPMetadataProvider is a one-shot object". what does it mean?
"LPMetadataProvider is a one-shot object". what does it mean?

Time:10-07

I want to fetch 'link previews' in collectionview cell with using LPMetaDataProvider.

Everything works well. But whenever scroll the collection view and cell dissappear (or displaying again), I get this error:

"Trying to start fetching on an LPMetadataProvider that has already started. LPMetadataProvider is a one-shot object." Any idea ?

    var urlString: String?

    var previewCell: LinkModel? {
    
    didSet {
      
        urlString = previewCell?.linkURL
        
        fetchPreview()
    }
}

   let linkPreview = LPLinkView()
   let provideer = LPMetadataProvider()


   override init(frame: CGRect) {
         super.init(frame: frame)
    contentView.backgroundColor = .black
    
  
     }

fileprivate func fetchPreview() {

    guard let url = URL(string: urlString ?? "") else {return}

    
    provideer.startFetchingMetadata(for: url) { metaData, error in

        guard let data = metaData, error == nil else {
            
        
            return
        }
        DispatchQueue.main.async  {
            self.linkPreview.metadata = data
            self.contentView.addSubview(self.linkPreview)
            self.linkPreview.frame = (self.contentView.bounds)
        }
    }
}

If i call the function in 'override init' everything works fine but this time it doesn't get the 'urlString' from didset. I will be glad if you help

CodePudding user response:

Make provider a local variable in fetchPreview.

fileprivate func fetchPreview() {

    guard let url = URL(string: urlString ?? "") else { return }
    let provideer = LPMetadataProvider()
    provider.startFetchingMetadata(for: url) { metaData, error in
        ...

"One-shot" means you should use this object once, and then throw it away.

  • Related