How do I check app connectivity correctly? - ios

I'm trying to create a function to check the Connectivity when the App loads. If a internet connection is detected, the app should download the JSON data and save the array in UserDefaults, then proceed to the UITableView methods. However, if the internet connection is not found, the app should recover the array on USerDefault to populate another array, then proceed to UItableView methods.
The problem I'm facing, is that when the compiler goes trough UserDefault line to be able to save the array, the app crashes immediately. What I'm doing wrong?
Compiler Error:
) for key backupSaved'
*** First throw call stack: (0x18257ad8c 0x1817345ec 0x18257ac6c 0x1825b1d08 0x1824e730c 0x1824e5a60 0x1825b2080 0x182515cec
0x1825b2080 0x1825b2304 0x182518d6c 0x182518588 0x182518c54
0x1825bc218 0x1825bf8a0 0x182edaaf4 0x102a794c0 0x102a7452c
0x102e8d314 0x102e45b7c 0x103da11dc 0x103da119c 0x103da5d2c
0x182523070 0x182520bc8 0x182440da8 0x184425020 0x18c45d758
0x102a756b0 0x181ed1fc0) libc++abi.dylib: terminating with uncaught
exception of type NSException (lldb)
[FIXED]ViewController:
import UIKit
import Kingfisher
import Alamofire
var arrCerveja = [Cerveja]()
var arrBackup = [Cerveja]()
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
//TableView DataSource
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if Connectivity.isConnectedToInternet {
return arrCerveja.count
} else {
return arrBackup.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellID") as! TableViewCell
if Connectivity.isConnectedToInternet {
let model = arrCerveja[indexPath.row]
cell.labelName.text = model.name
cell.labelDetail.text = "Teor alcoólico: \(model.abv)"
let resource = ImageResource(downloadURL: URL(string: "\(model.image_url)")!, cacheKey: model.image_url)
cell.imageViewCell.kf.setImage(with: resource, placeholder: UIImage(named: "icons8-hourglass-48"), options: nil, progressBlock: nil, completionHandler: nil)
return cell
} else {
let model = arrBackup[indexPath.row]
cell.labelName.text = model.name
cell.labelDetail.text = "Teor alcoólico: \(model.abv)"
let resource = ImageResource(downloadURL: URL(string: "\(model.image_url)")!, cacheKey: model.image_url)
cell.imageViewCell.kf.setImage(with: resource, placeholder: UIImage(named: "icons8-hourglass-48"), options: nil, progressBlock: nil, completionHandler: nil)
return cell
}
}
//TableView Delegate
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if Connectivity.isConnectedToInternet {
performSegue(withIdentifier: "segueId", sender:arrCerveja[indexPath.row])
} else {
performSegue(withIdentifier: "segueId", sender:arrBackup[indexPath.row])
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueId" {
let des = segue.destination as? TableViewDetalhes
//.item possui uma propriedade instanciada na TelaDetalheProdutos
des?.item = (sender as? Cerveja)
//Segue para CollectionView Categorias
}
}
struct Connectivity {
static let sharedInstance = NetworkReachabilityManager()!
static var isConnectedToInternet:Bool {
return self.sharedInstance.isReachable
}
}
override func viewDidAppear(_ animated: Bool) {
if Connectivity.isConnectedToInternet {
print("Connected")
getApiData { (cerveja) in
arrCerveja = cerveja
//Backup
do{
let data = try JSONEncoder().encode(arrCerveja)
UserDefaults.standard.set(data, forKey: "backupSaved")
//
self.tableView.reloadData()
}catch{print(error)
}
}
} else {
print("No Internet")
do{
if let savedData = UserDefaults.standard.value(forKey: "backupSaved") as? Data {
arrBackup = try JSONDecoder().decode([Cerveja].self, from: savedData)
self.tableView.reloadData()
}
}catch{
print(error)
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
//SetupNavBarCustom
navigationController?.navigationBar.setupNavigationBar()
}
}
Model:
struct Cerveja:Decodable{
let name:String
let image_url:String
let description:String
let tagline:String
let abv:Double
let ibu:Double?
}

The array should be endcoded to Data before saving , as it's array of custom objects
do {
....... write
let data = try JSONEncoder().encode(arr)
UserDefaults.standard.set(data, forKey: "backupSaved")
// save as data
....... read
if let savedData = UserDefaults.standard.value(forKey: "backupSaved") as? Data {
let savedArr = try JSONDecoder().decode([Cerveja].self, from: savedData)
// use the array here
}
}
catch {
print(error)
}
//
Since now you encode & decode
struct Cerveja:Codable {--}
//
Also I don't vote for saving in userDefaults consider CoreData

Related

Problem with saving data using Core Data in swift

I'm trying to save data to the core data and then display it on another view controller. I have a table view with custom cell, which have a button. I've created a selector, so when we tap on the button in each of the cell, it should save all the data from the cell. Here is my parent view controller:
import UIKit
import SafariServices
import CoreData
class ViewController: UIViewController, UISearchBarDelegate {
#IBOutlet weak var pecodeTableView: UITableView!
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var savedNews = [SavedNews]()
var newsTitle: String?
var newsAuthor: String?
var urlString: String?
var newsDate: String?
var isSaved: Bool = false
private var articles = [Article]()
private var viewModels = [NewsTableViewCellViewModel]()
private let searchVC = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
pecodeTableView.delegate = self
pecodeTableView.dataSource = self
pecodeTableView.register(UINib(nibName: S.CustomCell.customNewsCell, bundle: nil), forCellReuseIdentifier: S.CustomCell.customCellIdentifier)
fetchAllNews()
createSearchBar()
loadNews()
saveNews()
countNewsToCategory()
}
#IBAction func goToFavouritesNews(_ sender: UIButton) {
performSegue(withIdentifier: S.Segues.goToFav, sender: self)
}
private func fetchAllNews() {
APICaller.shared.getAllStories { [weak self] result in
switch result {
case .success(let articles):
self?.articles = articles
self?.viewModels = articles.compactMap({
NewsTableViewCellViewModel(author: $0.author ?? "Unknown", title: $0.title, subtitle: $0.description ?? "No description", imageURL: URL(string: $0.urlToImage ?? "")
)
})
DispatchQueue.main.async {
self?.pecodeTableView.reloadData()
}
case .failure(let error):
print(error)
}
}
}
private func createSearchBar() {
navigationItem.searchController = searchVC
searchVC.searchBar.delegate = self
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return viewModels.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: S.CustomCell.customCellIdentifier, for: indexPath) as! CustomNewsCell
cell.configure(with: viewModels[indexPath.row])
cell.saveNewsBtn.tag = indexPath.row
cell.saveNewsBtn.addTarget(self, action: #selector(didTapCellButton(sender:)), for: .touchUpInside)
return cell
}
#objc func didTapCellButton(sender: UIButton) {
guard viewModels.indices.contains(sender.tag) else { return }
print("Done")// check element exist in tableview datasource
if !isSaved {
saveNews()
print("success")
}
//Configure selected button or update model
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let article = articles[indexPath.row]
guard let url = URL(string: article.url ?? "") else {
return
}
let vc = SFSafariViewController(url: url)
present(vc, animated: true)
}
//Search
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
guard let text = searchBar.text, !text.isEmpty else {
return
}
APICaller.shared.Search(with: text) { [weak self] result in
switch result {
case .success(let articles):
self?.articles = articles
self?.viewModels = articles.compactMap({
NewsTableViewCellViewModel(author: $0.author ?? "Unknown", title: $0.title, subtitle: $0.description ?? "No description", imageURL: URL(string: $0.urlToImage ?? "")
)
})
DispatchQueue.main.async {
self?.pecodeTableView.reloadData()
self?.searchVC.dismiss(animated: true, completion: nil)
}
case .failure(let error):
print(error)
}
}
}
}
extension ViewController {
func loadNews() {
let request: NSFetchRequest<SavedNews> = SavedNews.fetchRequest()
do {
let savedNews = try context.fetch(request)
//Handle saved news
if savedNews.count > 0 {
isSaved = true
}
} catch {
print("Error fetching data from context \(error)")
}
}
func saveNews() {
//Initialize the context
let news = SavedNews(context: self.context)
//Putting data
news.title = newsTitle
news.author = newsAuthor
news.publishedAt = newsDate
news.url = urlString
do {
try context.save()
} catch {
print("Error when saving data \(error)")
}
}
func countNewsToCategory() {
//Initialize the context
let request: NSFetchRequest<SavedNews> = SavedNews.fetchRequest()
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [
])
request.predicate = predicate
do {
savedNews = try context.fetch(request)
} catch {
print("Error fetching data from category \(error)")
}
}
}
I don't know where is the problem, I've created a correct data model, but data could not be saved. Here is my model:
import Foundation
struct APIResponse: Codable {
let articles: [Article]
}
struct Article: Codable {
let author: String?
let source: Source
let title: String
let description: String?
let url: String?
let urlToImage: String?
let publishedAt: String
}
struct Source: Codable {
let name: String
}
And also my model in Core Data:
My second view controller, to which I want display the data:
import UIKit
import CoreData
class FavouriteNewsViewController: UIViewController {
#IBOutlet weak var favTableView: UITableView!
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
var savedNews = [SavedNews]()
override func viewDidLoad() {
super.viewDidLoad()
favTableView.delegate = self
favTableView.delegate = self
loadSavedNews()
favTableView.register(UINib(nibName: S.FavouriteCell.favouriteCell, bundle: nil), forCellReuseIdentifier: S.FavouriteCell.favouriteCellIdentifier)
// Do any additional setup after loading the view.
}
}
extension FavouriteNewsViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return savedNews.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = favTableView.dequeueReusableCell(withIdentifier: S.FavouriteCell.favouriteCellIdentifier, for: indexPath) as! FavouritesCell
print(savedNews)
let article = savedNews[indexPath.row]
if let articleTitle = article.title {
cell.favTitle.text = articleTitle
}
if let articleAuthor = article.author {
cell.favAuthor.text = articleAuthor
}
if let articleDesc = article.desc {
cell.favDesc.text = article.desc
}
return cell
}
}
extension FavouriteNewsViewController {
func loadSavedNews() {
let request: NSFetchRequest<SavedNews> = SavedNews.fetchRequest()
do {
savedNews = try context.fetch(request)
} catch {
print("Error fetching data from context \(error)")
}
}
func deleteNews(at indexPath: IndexPath) {
// Delete From NSObject
context.delete(savedNews[indexPath.row])
// Delete From current News list
savedNews.remove(at: indexPath.row)
// Save deletion
do {
try context.save()
} catch {
print("Error when saving data \(error)")
}
}
}
you did not assign your properties that you are trying to save
newsTitle,newsAuthor,newsDate,urlString
seems these properties have nil value . make sure these properties have valid value before save .

IOS Swift - API fetch (Able to fetch data in console log, but can't display it)

import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var data = [WeatherResponse]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Værmelding"
downloadJson {
self.tableView.reloadData()
}
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = data[indexPath.row].geometry.type.capitalized
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetails", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? DetailViewController {
destination.data_detail = data[(tableView.indexPathForSelectedRow?.row)!]
}
}
func downloadJson(completed: #escaping () -> ()) {
let jsonUrlString = "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=60.10&lon=9.58"
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
print("checking for data")
if err == nil {
do {
let weather = try JSONDecoder().decode(WeatherResponse.self, from: data!)
print(weather)
DispatchQueue.main.async {
completed()
}
} catch let jsonErr {
print(jsonErr, "is error")
}
}
}.resume()
}
}
import Foundation
struct WeatherResponse: Decodable {
let geometry: Weather
let properties: Properties
let type: String
}
// MARK: - Geometry
struct Weather: Decodable {
let type: String
let coordinates: Array<Double>
}
//MARK: - Properties
struct Properties: Decodable {
let meta: Meta
//let timeseries: [Timesery]
}
//MARK: - Meta
struct Meta: Decodable {
let updated_at: String
let units: Units
}
// MARK: - Units
struct Units: Decodable {
let air_pressure_at_sea_level: String
let air_temperature: String
let cloud_area_fraction: String
let precipitation_amount: String
let relative_humidity: String
let wind_from_direction: String
let wind_speed: String
}
The code works when I fetch APIs as an array, but here it doesn't display. It might be because it is trying to read data from as a Dictionary. The code works fine when i fetch from APIs such as; Dota, JSON dummy data and apis that works with id.

How can I delete data in coreData and Tableview with one Button?

I'm actually doing an app for recipe, I already did the persistent data to save my ingredients in the list but when I want to delete my ingredients with my button it works at the first time but come back when I restart my app.
Here's my code :
class AddIngredientController: UIViewController, ShowAlert {
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var ingredientsTableView: UITableView!
var itemArrayIngredient = [Item]()
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
override func viewDidLoad() {
super.viewDidLoad()
let dataFilePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
print(dataFilePath)
loadItems()
}
func saveItems() {
do {
try context.save()
} catch {
print("Error saving context \(error)")
}
}
func loadItems(with request: NSFetchRequest<Item> = Item.fetchRequest()) {
do {
itemArrayIngredient = try context.fetch(request)
} catch {
print("Error fetching data from context \(error)")
}
}
#IBAction func clearButton(_ sender: UIButton) {
context.delete() //Here the problem
itemArrayIngredient.removeAll()
saveItems()
ingredientsTableView.reloadData()
}
#IBAction func addButton(_ sender: UIButton) {
if textField.text!.isEmpty {
showAlert(title: "No Ingredients", message: "Please, add some ingredients to your list.")
} else {
newIngredientAdded()
}
}
func newIngredientAdded() {
let newItem = Item(context: context)
newItem.title = textField.text!
itemArrayIngredient.append(newItem)
saveItems()
ingredientsTableView.reloadData()
textField.text! = ""
}
}
extension AddIngredientController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArrayIngredient.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customSearchCell", for: indexPath)
let item = itemArrayIngredient[indexPath.row]
cell.textLabel?.text = item.title
cell.textLabel?.textColor = UIColor.white
cell.textLabel?.font = UIFont(name: "Marker Felt", size: 19)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
saveItems()
tableView.deselectRow(at: indexPath, animated: true)
}
}
context.delete must be called with an argument. For example
#IBAction func clearButton(_ sender: UIButton) {
itemArrayIngredient.forEach { context.delete($0) }
itemArrayIngredient.removeAll()
saveItems()
ingredientsTableView.reloadData()
}
You need to delete a specific object from core data. Please refer below code
let fetchRequest = NSFetchRequest(entityName: "EntityName")
if let result = try? context.fetch(fetchRequest) {
for object in result {
//Please check before delete operation
if object.id == Your_Deleted_Object_ID{
context.delete(object)
}
}
}
You can delete all data from particular entity by using NSBatchDeleteRequest. See following code. The main advantage of NSBatchDeleteRequest is, you don't need to enumerate on array of object.
let fetch = NSFetchRequest<NSFetchRequestResult>(entityName: "EntityName")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: fetch)
do {
try context.execute(deleteRequest)
} catch {
print(error.localizedDescription)
}

Core Data not fetching in table view

My code below is using core data to add entities to a tableview. The problem is that when I hit the button to add a new entry the data is not displayed on the tableview. But if I quit the simulator and re install the app the entity is there. To get the entities displayed I have to enter it then reinstall the app to get the entity displayed.
import UIKit
import CoreData
class ViewController: UIViewController {
#IBOutlet var rot: UITableView!
#IBOutlet var enterName: UITextField!
var people : [NSManagedObject] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
showdata()
}
#IBAction func iDontLikeSchool(_ sender: Any) {
if(enterName.text?.isEmpty)! {
alertmsg(name: "d")
showdata()
} else {
saveData(name: enterName.text!)
}}
func alertmsg(name:String){
let alert = UIAlertController(title: "D", message: "d", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func saveData(name: String) {
guard let appDelage = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelage.persistentContainer.viewContext
let ee = NSEntityDescription.entity(forEntityName: "PersonalInfo", in: managedContext)!
let person = NSManagedObject(entity : ee , insertInto: managedContext)
person.setValue(name, forKey: "userName")
do {
try managedContext.save()
people.append(person)
alertmsg(name: "saved")
enterName.text = ""
}
catch let err as NSError {
print("judo",err)
}
///
}
func showdata() {
guard let appDelage = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelage.persistentContainer.viewContext
let fetchReuqst = NSFetchRequest<NSManagedObject>(entityName: "PersonalInfo")
do {
people = try managedContext.fetch(fetchReuqst)
if people.count == 0 {
} else {
}}
catch let err as NSError {
print("judo", err)
}
///
}
}
extension ViewController: UITableViewDataSource, UITableViewDelegate{
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = rot.dequeueReusableCell(withIdentifier: "Person")
let personn = people[indexPath.row]
cell?.textLabel?.text = personn.value(forKey: "userName") as? String
return cell!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}
The reason why the entry doesn't show up is that you don't tell the view that your model has changed so it doesn't refresh itself.
The tableView gets rendered once on viewDidLoad that's why the data is displayed correctly after the view is loaded freshly (after your app restart).
To solve this issue, keep your viewController's tableView as a property (#IBOutlet if you use the Storyboard) and call
tableView.reloadData()
after you changed your data, in your case e.g. at the end of your func showData() like this.
func showdata() {
guard let appDelage = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelage.persistentContainer.viewContext
let fetchReuqst = NSFetchRequest<NSManagedObject>(entityName: "PersonalInfo")
do {
people = try managedContext.fetch(fetchReuqst)
if people.count == 0 {
//sth
} else {
// sth
}
} catch let err as NSError {
print("judo", err)
}
tableView.reloadData()
}

UITableView doesn't show the realm objects that it should show

I have a problem with the content of a tableView. It doesn't show me the realm objects, that I asked for. Here's my code:
At the top of the class i declared this variable:
let newPlan = TrainingPlan()
Then I have this button action, which is copying the exercise objects, put them in an array an add this to realm. I did this, because I only want to change this copied array:
#IBAction func savePlanAction(_ sender: AnyObject) {
if planNameTextField.text!.isEmpty{
planNameFehlerLabel.isHidden = false
}
else{
newPlan.name = planNameTextField.text!
newPlan.creationDate = NSDate() as Date
let selectedExcercises = loadSelectedExcercises()
if selectedExcercises != nil{
for var i in (0..<selectedExcercises!.count){
excerciseCopies.append(selectedExcercises![i])
do{
try realm.write{
realm.add(excerciseCopies[i])
}
}
catch{
print(error)
}
}
try! realm.write{
realm.add(newPlan)
}
for object in excerciseCopies {
do{
try realm.write{
newPlan.excercises.append(object)
}
}
catch{
print(error)
}
}
performSegue(withIdentifier: "savePlan", sender: self)
}
else{
uebungFehlerLabel.isHidden = false
}
}
}
Then I give the newPlan object to another ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier! == "savePlan" {
let tct = segue.destination as! TrainingPlanConfTableViewController
tct.plan = newPlan
}
}
Now in the next class I want to show all exercise objects from the copied array in a tableView, but the tableView doesn't show anything:
class TrainingPlanConfTableViewController: UITableViewController {
//Properties
let realm = try! Realm()
var excercisesFromPlan: Results<Excercise>?{
didSet{
tableView.reloadData()
}
}
var plan: TrainingPlan?
//Lifecycle
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
loadExcercisesFromPlan()
}
//Request
func loadExcercisesFromPlan(){
if plan != nil{
let predicate = NSPredicate(format: "trainingsplan = %#", plan!)
excercisesFromPlan = realm.objects(Excercise.self).filter(predicate)
}
}
//Tableview Funktionen
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if excercisesFromPlan != nil{
return excercisesFromPlan!.count
}
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: TrainingPlanConfTableViewCell = tableView.dequeueReusableCell(withIdentifier: "PlanConfCell") as! TrainingPlanConfTableViewCell
let excercise = excercisesFromPlan![indexPath.row]
cell.nameLabel.text = excercise.name
return cell
}
}
I'm pretty new in Swift and I can't find the issue. Is the predicate seated wrong? I would be thankful for any help! If you need any more information, for example the data model etc, pls feel free to ask
Since you didn't include the code for your models, there's no way to know for sure if your predicate is correct or not.
What I can tell is the TrainingPlan has a exercises property on it that you should use instead of querying for the same thing.
You would just need to change excercisesFromPlan to be a List instead of a Results
var excercisesFromPlan: List<Excercise>?
and change loadExcercisesFromPlan to get it
func loadExcercisesFromPlan() {
excercisesFromPlan = plan?.excercises
}

Resources