ios – Cannot save knowledge to core knowledge on important thread or personal thread


Yesterday I used to be in a position to save data to core knowledge however then I did somewhat little bit of refactoring in my venture to make my code cleaner(principally code from my DTO the place I encode / decode knowledge with NSCoder and created separate information for my customized worth transformers)

After that I observed saving knowledge stopped working, that is my code for including:

 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
    
    func add(pet: PetDTO) {
        let managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
        managedObjectContext.carry out {
            let newPet = PetEntity(context: managedObjectContext)
            
            newPet.id = Int64(pet.id)
            newPet.identify = pet.identify
            newPet.class = pet.class
            newPet.photoUrls = pet.photoUrls
            newPet.tags = pet.tags
            newPet.standing = pet.standing.rawValue
            do {
                strive self.context.save()
            } catch {
                print(error)
            }
        }
    }

I It appears that evidently I’m lacking one thing associated to contexts. I created this managedObjectContext and from what I perceive, I needed to save knowledge on this context, however nothing occurs. I attempted eradicating let managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) and saving on important context however I had this error:

CoreData: error: SQLCore dispatchRequest: exception dealing with request: <NSSQLSaveChangesRequestContext: 0x600000156640> , <shared NSSecureUnarchiveFromData transformer> threw whereas encoding a price. with userInfo of (null)
Error Area=NSCocoaErrorDomain Code=134060 "A Core Information error occurred."

I managed to resolve it solely by including one other context and passing it to PetEntity.

So the query is: what am I doing fallacious? Can I someway merge contexts? I’ve seen a way for that however undecided if I would like it.

Thanks

Replace:
Modified my code to this:

 do {
       strive managedObjectContext.save()
        } catch {
        print(error)
     }

Simply realized wasn’t saving it on the precise context. Now I’ve nilError and it nonetheless did not save into the database. I feel it occurs as a result of I get dangerous knowledge from Json, my class area is typically nil and that is why it exhibits that error, am I proper?

And about getting knowledge from Core knowledge, now I fetch it from important context, however the knowledge is being saved on one other context, what can I do right here to repair this?

Leave a Reply

Your email address will not be published.