Search code examples
iosswiftuicollectionviewsdwebimage

Can't Reload Collection View Swift 4


In a detail view controller, the user can add a character to the favorites list. Clicking the favorite button adds the image url to CoreData. Then in the collection view controller, I run the code below:

override func viewDidLoad() {
    super.viewDidLoad()
    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Favorite")

    request.returnsObjectsAsFaults = false

    do {
        let results = try context.fetch(request)
        if results.count > 0
        {
            for result in results as! [NSManagedObject]
            {
                if let favFig = result.value(forKey: "favFig") as? String
                {
                    favFigArray.append(favFig)
                }
                if let favFigID = result.value(forKey: "favoriteID") as? String
                {
                    favFigIDArray.append(favFigID)
                }
            }

        }
    } catch {
        print("Error Fetching")
    }
}

The favFigArray then contains a list of the image urls. To set the image to my collection view cell I use this code:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ownedCell", for: indexPath) as! OwnedCollectionViewCell
    cell.ownedFigImage.sd_setImage(with: URL(string: favFigArray[indexPath.row]))

    return cell
}

The problem I have is that when a user wants to go back to the detail view controller and add more characters to the favorites list, the collection view will not update. I tried using self.ownedCollectionView.reloadData() in the viewWillAppear and that does not work. CoreData is updating properly, but favFigArray is not updating.

Can anyone help me get the reload to work? Thank you!


Solution

  • You can create a method for filtering results like below,

    func filterFavFigData()
         favFigArray.removeAll()
    
         let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
         let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Favorite")
    
         request.returnsObjectsAsFaults = false
    
         do {
            let results = try context.fetch(request)
            if results.count > 0
            {
                for result in results as! [NSManagedObject]
                {
                    if let favFig = result.value(forKey: "favFig") as? String
                    {
                        favFigArray.append(favFig)
                    }
                    if let favFigID = result.value(forKey: "favoriteID") as? String
                    {
                        favFigIDArray.append(favFigID)
                    }
                }
            }
        } catch {
            print("Error Fetching")
        }
    }
    

    and then update your viewWillAppear like below,

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.filterFavFigData()
        self.ownedCollectionView.reloadData()
    }