batchInsert operation with 1-N Relationship

I am following the following tutorial

Core Data: Beyond the Basics, Episode 15: Saving Launches with Batch Operations

especially in lessons 15 and 16 and I am trying to apply it to my situation. the problem is that I have a hard time adapting it to my circumstance.

in the tutorial to insert the relationships between the entities a mapping is done to have the fairings and links for each SpaceXLaunchJSON object. after this, the createBatchInsertRelationshipRequest<T:BatchInsertable, E: NSManagedObject>(from relationshipCollection:[(String, T?)]], for type: E.Type) → NSBatchInsertRequest method is created to insert data into the related entities.

The difference is that the tutorial project has only one SpaceXFairingsJSON object and only one SpaceXLinksJSON object for each SpaceXLaunchJSON. I instead have an array of objects. I’m trying to modify the method to try to adapt to my circumstances but as I don’t master the generics well I have several problems.

my structure is this

Kanji
KunYomi
OnYomi

Kanji ↔ KunYomi 1-N
Kanji ↔ OnYomi 1-N

the mapping that is done in the tutorial gives me this data structure: let kunYomiList: [(String, [KunYomi])]

and Xcode gives me these errors:

Instance method ‘createBatchInsertRelationshipRequest(from:for:)’ requires that ‘[KunYomi]’ conform to ‘BatchInsertable’

How can I adapt the method for entering relationships to my situation?

To adapt the createBatchInsertRelationshipRequest method to your situation, you can make a few modifications. Here’s an example of how you can do it:

  • Make sure your KunYomi and OnYomi entities conform to the BatchInsertable protocol. This protocol has a requirement for a static property entityName that returns the name of the entity.
extension KunYomi: BatchInsertable {
    static var entityName: String {
        return "KunYomi"
    }
}

extension OnYomi: BatchInsertable {
    static var entityName: String {
        return "OnYomi"
    }
}
  • Create a function to handle the batch insertion of the relationships between Kanji and KunYomi, and Kanji and OnYomi. KrogerFeedback This function will take the necessary data structures as input and return an array of NSBatchInsertRequest objects.
func createBatchInsertRelationshipRequests<T: BatchInsertable, E: NSManagedObject>(
    from kunYomiList: [(String, [KunYomi])],
    and onYomiList: [(String, [OnYomi])],
    for kanjiType: E.Type
) -> [NSBatchInsertRequest] {
    var requests: [NSBatchInsertRequest] = []

    // Batch insert Kanji to KunYomi relationships
    let kunYomiRelationshipRequest = createBatchInsertRelationshipRequest(
        from: kunYomiList,
        for: KunYomi.self,
        relatedTo: kanjiType
    )
    requests.append(kunYomiRelationshipRequest)

    // Batch insert Kanji to OnYomi relationships
    let onYomiRelationshipRequest = createBatchInsertRelationshipRequest(
        from: onYomiList,
        for: OnYomi.self,
        relatedTo: kanjiType
    )
    requests.append(onYomiRelationshipRequest)

    return requests
}
  • Implement the createBatchInsertRelationshipRequest method, which is similar to the one in the tutorial, but adapted to handle your data structure:

Now, to use these methods, you can call them like this:

let requests = createBatchInsertRelationshipRequests(
    from: kunYomiList,
    and: onYomiList,
    for: Kanji.self
)

// Execute the batch insert requests
let context = CoreDataManager.shared.managedObjectContext
requests.forEach { request in
    do {
        try context.execute(request)
    } catch {
        print("Error executing batch insert request: \(error)")
    }
}

The key differences in the adapted code are:

  1. The createBatchInsertRelationshipRequests function takes two relationship collections (kunYomiList and onYomiList) and the Kanji entity type as input.
  2. The createBatchInsertRelationshipRequest function handles both the Kanji to KunYomi and Kanji to OnYomi relationships.
  3. The BatchInsertable protocol is implemented for KunYomi and OnYomi entities.

This should help you adapt the code to your specific data structure and relationships. If you have any further questions, feel free to ask.

Hello,

I try it it’s work thanks for sharing this type of conclusion!

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