asynchronous function that maintains a strong reference to the object?

Asked

Viewed 43 times

0

I’m having trouble having strong reference of an asynchronous function, I’m using Coredata to make data persistence. When I start the app appears to me the following message : inserir a descrição da imagem aqui

however, when I call Dispatchqueue.main.async, the function ends up losing its "return""

Follow the full class

  import Foundation
  import UIKit
  import CoreData

  class FilmesFavoritoss {


      func getContexto() -> NSManagedObjectContext{

         let appDelegate = UIApplication.shared.delegate as! AppDelegate
         return appDelegate.persistentContainer.viewContext
      }

      func prepare (dataForSaving: [Results]){
        _ = dataForSaving.map{self.createEntity(filme: $0)}
      }

      private func createEntity(filme:Results) -> FilmesFavoritos?{
        guard let id = filme.id , let titulo = filme.original_title , let overview = 
        filme.overview, let poster = filme.poster_path else {return nil}

        let itemFilme = FilmesFavoritos(context: getContexto())
        itemFilme.id = id
        itemFilme.original_title = titulo
        itemFilme.overview = overview
        itemFilme.poster_path = poster as NSObject
        return itemFilme
       }

     }

the class that uses the filmsFavoritoss class

import Foundation
import UIKit
import CoreData
class Api{

let setFilmesFavorite = FilmesFavoritoss()
func loadInfoFilmes(page:Int = 1,onComplete: @escaping (_repositories: 
[Results])-> Void) {
    let urlApi = URL(string: "https://api.themoviedb.org/3/movie/popular?api_key=329294313940e3420db6b6c600ee33e8&language=en-US&page=\(page)")
      guard let url = urlApi  else { return }
      URLSession.shared.dataTask(with: url) {(data, response, err)in
          guard let data = data else {return}
          do {
                let course = try JSONDecoder().decode(Item.self, from: data)
                let infos = course.results
                self.setFilmesFavorite.prepare(dataForSaving: infos)
                onComplete(infos)
          } catch  {
              print(error.localizedDescription)
          }
    }.resume()
}

Homeviewcontroller:

import Foundation
import UIKit
import CoreData
import Kingfisher

class HomeViewContollerViewController: UIViewController, 
UICollectionViewDataSource, UICollectionViewDelegate, 
UICollectionViewDelegateFlowLayout, UISearchBarDelegate {


 @IBOutlet weak var FilmesCollectionView: UICollectionView!

 @IBOutlet weak var SeachBar: UISearchBar!




 var nomes : HomeCollectionViewCell?

 var listaFilmes = [Results]()
 var item : Item!
 var callApi = Api()
 var page : Int = 1

 override func viewDidLoad() {
    super.viewDidLoad()

    self.SeachBar.delegate = self
    self.FilmesCollectionView.delegate = self
    self.FilmesCollectionView.dataSource = self
    callApi.loadInfoFilmes(page: page) { (result) in
        self.listaFilmes = result
    }
    // Do any additional setup after loading the view.
}

Does anyone know how I might be solving this problem? Thanks in advance!

  • It seems to me that the problem is not in the Filmesfavoritos class, but in who makes use of it. It would have to include the relevant source.

  • Yes, I will post @epx

  • @epx, modified!

  • It seems to me that it would be enough to call onComplete(Infos) within a Dispatchqueue.main.async block.

  • Correction, self.setFilmesFavorite.prepare() would go inside this block too.

  • @It worked out! That’s what it was all about

  • Excellent. When you have time fill in the answer which is a relevant question.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.