我试图了解为什么这仍然为我提供了来自主记录的所有详细记录。我期待只能获得与当前收件人相关的那些记录,而是每次迭代都会向我展示所有细节,而不管掌握如何:
Func MoveImageStocoredata(_实体:字符串,InManagedObjectContext ManagedObjectContext:nsmanagedObjectContext) - > [NSManagedObject] {
打印(“将数据从文件移动到云”)
打印(“首先让我们获得所有收件人”)

    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
}

弄清楚这个......

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")
                }
            }
        }