-1

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!

Medicine Man
  • 81
  • 2
  • 10
  • you should filter the results in viewWillAppear and then reload. Currently you are filtering inside viewDidLoad so favFigArray array will not update again. – Kamran Apr 13 '18 at 21:55

1 Answers1

1

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()
}
Ankit Jayaswal
  • 5,549
  • 3
  • 16
  • 36
Kamran
  • 14,987
  • 4
  • 33
  • 51
  • You are awesome! The favFigArray.removeAll() was what I needed. I had tried the other code previously but all it did was add it over and over. Thank you :) If I may ask, is there a way to only call this function if a change has been made? Meaning if the user doesn't change the favorites and wants to look at the list again, I would prefer not to have it reload. – Medicine Man Apr 13 '18 at 22:19
  • Yes, you should use some delegate pattern to reload again only if change is happened. – Kamran Apr 13 '18 at 23:01