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
}