Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData using NSBatchInsertRequest

I'm trying to insert Array of dictionary in CoreData using NSBatchInsertRequest according WWDC 2019 (https://developer.apple.com/videos/play/wwdc2019/230/). The insertResult is nil, and my CoreData is empty.

let modelURL = Bundle.main.url(forResource: "CoreDataPerformance", withExtension: "momd")!
let model = NSManagedObjectModel(contentsOf: modelURL)!
let container = NSPersistentCloudKitContainer(name: "CoreDataPerformance", managedObjectModel: model)
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
    if let error = error as NSError? {
        fatalError("Unresolved error \(error), \(error.userInfo)")
    }
})

try container.viewContext.setQueryGenerationFrom(.current)

let moc = container.viewContext
moc.automaticallyMergesChangesFromParent = true
moc.perform {
    let insertRequest = NSBatchInsertRequest(entity: Client.entity(), objects: clients)
    let insertResult = try? moc.execute(insertRequest) as? NSBatchInsertRequest
    let success = insertResult?.resultType
    print("RESULT STATUS: \(success)")
}

This is the error I receive in the console:

2020-02-04 18:30:25.800705+0200 CoreDataPerformance[62836:778869] [error] warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CoreDataPerformance.Client' so +entity is unable to disambiguate.

CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'CoreDataPerformance.Client' so +entity is unable to disambiguate.

2020-02-04 18:30:25.800846+0200 CoreDataPerformance[62836:778869] [error] warning:   'Client' (0x600000c50bb0) from NSManagedObjectModel (0x600001877480) claims 'CoreDataPerformance.Client'.

CoreData: warning:   'Client' (0x600000c50bb0) from NSManagedObjectModel (0x600001877480) claims 'CoreDataPerformance.Client'.

2020-02-04 18:30:25.800940+0200 CoreDataPerformance[62836:778869] [error] warning:   'Client' (0x600000c589a0) from NSManagedObjectModel (0x600001861680) claims 'CoreDataPerformance.Client'.

CoreData: warning:   'Client' (0x600000c589a0) from NSManagedObjectModel (0x600001861680) claims 'CoreDataPerformance.Client'.
like image 528
benhi Avatar asked Nov 16 '22 17:11

benhi


1 Answers

your line let insertResult = try? moc.execute(insertRequest) as? NSBatchInsertRequest

should be: let insertResult = try? moc.execute(insertRequest) as? NSBatchInsertResult

like image 122
andre Avatar answered Dec 31 '22 13:12

andre