Cycle detected in CKModifyRecordsOperation when trying to bulk save

I'm working to refactor a somewhat clunky iterative save loop for CloudKit to use CKModifyRecordsOperation and bulk save records.

I have a Course, which has 1+ Weeks, each which has 1+ Lessons. Previously I'd create the Course in CloudKit, then create the Weeks, then the Lessons and circle back to update the Weeks with the Lesson references once created. And also fetch and save the Course record with the references to the Weeks once Weeks were created.

I've refactored to create all (Course, Week and Lesson) records locally, with the relevant references set up. E.g., course["weeks"] contains the record references for each week I've created locally, for example:
Code Block
course["weeks"] = getWeekRefsForCourse(for: allWeeks)
func getWeekRefsForCourse(for allWeeks: [CKRecord]) -> [CKRecord.Reference] {
var weekRefsArray: [CKRecord.Reference] = []
for each in allWeeks {
let weekRef = CKRecord.Reference(record: each, action: .deleteSelf)
weekRefsArray.append(weekRef)
return weekRefsArray
}
The issue is when I go to save, the error I get back is:

Invalid list of records: Cycle detected in record graph

This suggests that I've got a record referring to itself, but I've gone record by record and I I can't see anything. The Weeks reference the Course and the Lessons, but not themselves, etc. So my only theory is that because I'm trying to save items that refer to other items that haven't yet been created, what I'm trying to do isn't possible.

Is the correct protocol here actually my original approach?

Or is there something I'm missing?

Original approach:
  1. Save Course

  2. Save Week

  3. Save Lessons

  4. Update Weeks with Lesson references

  5. Update Course with Week references


CKModifyRecordsOperation code:
Code Block
let bulkSaveQueryOp = CKModifyRecordsOperation()
bulkSaveQueryOp.recordsToSave = [courseRecord]
bulkSaveQueryOp.recordsToSave?.append(contentsOf: weeks)
bulkSaveQueryOp.recordsToSave?.append(contentsOf: lessons)
//note I've confirmed I have the correct number of records
bulkSaveQueryOp.modifyRecordsCompletionBlock = { records, recordIDs, error in
if let error = error as? CKError {
log.error(error)
} else { // success }
}
CKContainer.default().publicCloudDatabase.add(bulkSaveQueryOp)

Accepted Answer
I was able to find this question after some further digging: https://developer.apple.com/forums/thread/654392?answerId=621352022#621352022

And it helped identify the source of the problem - that I had effectively created a loop with my .deleteSelf actions I was creating on various references.

Once I double checked and adjusted those, the error went away and I was able to save.

Spotting this was made more complex by the fact that I wasn't changing the .deleteSelf actions version what I had previously done - and was working fine - when my saves were done in sequence rather than a bulk CKModifyRecordsOperation save.
Cycle detected in CKModifyRecordsOperation when trying to bulk save
 
 
Q