Selecting detail records from a master question

I am trying to understand why this is still providing me all the detailed records from the master record. I am expecting to get only those records related to the current recipient, instead each iteration shows me all the details regardless of master:
func moveImagesToCoreData(_ entity: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) -> [NSManagedObject] {
print(“Gonna move data from files to the cloud”)
print(“First lets get all the Recipients”)

    let fetchRecipients = NSFetchRequest<NSFetchRequestResult>(entityName: "Recipient")
    var resultRecipients = [NSManagedObject]()
    do {
        let recipients = try managedObjectContext.fetch(fetchRecipients)
        if let masters = masters as? [NSManagedObject]{
            for master in masters {
                print("First Name: \(String(describing: recipient.value(forKey:"firstName")))) Last Name: \(String(describing: master.value(forKey:"lastName")))) ")
                let fetchEvents = NSFetchRequest<Event>(entityName: "Event")
                var resultEvents = [NSManagedObject]()
                do {
                    let details = try managedObjectContext.fetch(fetchEvents)
                    let predicate = NSPredicate(format: "%K == %@", #keyPath(Event.master), master)
                    fetchEvents.sortDescriptors = [NSSortDescriptor(key: "eventDate", ascending: false), NSSortDescriptor(key: "event", ascending: false)]
                    fetchEvents.predicate = predicate
                    if let details = details as? [NSManagedObject]{
                        for detail in details{  // need to figure out how to loop thru the entity's
                            autoreleasepool {
                                let backThumbnailmageURI = record.value(forKey: "cardBackThumbnailURI")
                                var dataBackThumbnail = record.value(forKey: "cardBackThumbnailImage")
                                
                                let backOfCardThumbnail = dataBackThumbnail
                               
                                record.setValue(backOfCardThumbnail, forKey: "cardBackThumbnailImage")
                                print("Front: \(String(describing: backOfCardThumbnail))")
                                do {
                                    try managedObjectContext.save()
                                }
                                catch
                                {
                                    print(error)
                                }
                                
                            }
                            resultEvents = records
                        }
                    }
                } catch {
                    print("oops .. can't fetch Detail")
                }
            }
        }
    } catch {
        print("oops .. can't fetch Master")
    }
    
    return resultMaster
}

Figured this one out…

func moveImagesToCoreData(_ entity: String, inManagedObjectContext managedObjectContext: NSManagedObjectContext) {
            autoreleasepool{
                let fetchRecipients = NSFetchRequest<NSFetchRequestResult>(entityName: "Recipient")
                do {
                    let recipients = try managedObjectContext.fetch(fetchRecipients)
                    if let recipients = recipients as? [NSManagedObject]{
                        for recipient in recipients {
                            let fetchEvents = NSFetchRequest<Event>(entityName: "Event")
                            fetchEvents.predicate = NSPredicate(format: "%K == %@", #keyPath(Event.recipient), recipient)
                            do {
                                let records = try managedObjectContext.fetch(fetchEvents)
                                if let records = records as? [NSManagedObject]{
                                    for record in records{  // need to figure out how to loop thru the entity's
                                        autoreleasepool {
                                            let frontImageURI = record.value(forKey: "cardFrontURI")
                                            let backImageURI = record.value(forKey: "cardBackURI")
                                            let frontThumbnailImageURI = record.value(forKey: "cardFrontThumbnailURI")
                                            let backThumbnailmageURI = record.value(forKey: "cardBackThumbnailURI")
                                            var dataFront = record.value(forKey: "cardFrontImage")
                                            var dataBack = record.value(forKey: "cardBackImage")
                                            var dataFrontThumbnail = record.value(forKey: "cardFrontThumbnailImage")
                                            var dataBackThumbnail = record.value(forKey: "cardBackThumbnailImage")
                                            do {
                                                dataFront = try UIImage(data: Data(contentsOf: FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(frontImageURI! as! String)))!.resized(toWidth: 600)
                                            } catch {
                                                print("Front error occured for record \(String(describing: record.value(forKey: "eventDate"))) - \(String(describing: record.value(forKey: "event")))")
                                            }
                                            do {
                                                dataBack = try UIImage(data: Data(contentsOf: FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(backImageURI! as! String)))!.resized(toWidth: 600)
                                            } catch {
                                                print("Back error occured for record \(String(describing: record.value(forKey: "eventDate"))) - \(String(describing: record.value(forKey: "event")))")
                                            }
                                            do {
                                                dataFrontThumbnail = try UIImage(data: Data(contentsOf: FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(frontThumbnailImageURI! as! String)))!
                                            } catch {
                                                print("Front Thumbnail error occured for record \(String(describing: record.value(forKey: "eventDate"))) - \(String(describing: record.value(forKey: "event")))")
                                            }
                                            do {
                                                dataBackThumbnail = try UIImage(data: Data(contentsOf: FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(backThumbnailmageURI! as! String)))!
                                            } catch {
                                                print("Back Thumbnail error occured for record \(String(describing: record.value(forKey: "eventDate"))) - \(String(describing: record.value(forKey: "event")))")
                                            }
                                            let backOfCard = dataBack
                                            let frontOfCard = dataFront
                                            let backOfCardThumbnail = dataBackThumbnail
                                            let frontOfCardThumbnail = dataFrontThumbnail
                                            record.setValue(frontOfCard, forKey: "cardFrontImage")
                                            print("Front: \(String(describing: frontOfCard))")
                                            record.setValue(backOfCard, forKey: "cardBackImage")
                                            print("Front: \(String(describing: backOfCard))")
                                            record.setValue(frontOfCardThumbnail, forKey: "cardFrontThumbnailImage")
                                            print("Front: \(String(describing: frontOfCardThumbnail))")
                                            record.setValue(backOfCardThumbnail, forKey: "cardBackThumbnailImage")
                                            print("Front: \(String(describing: backOfCardThumbnail))")
                                            do {
                                                try managedObjectContext.save()
                                            }
                                            catch
                                            {
                                                print(error)
                                            }
                                        }
                                    }
                                }
                            } catch {
                                print("oops .. can't fetch Events")
                            }
                        }
                    }
                } catch {
                    print("oops .. can't fetch Recipient")
                }
            }
        }

This topic was automatically closed after 166 days. New replies are no longer allowed.