desired type = Towns; given type = Towns - Unacceptable type of value for to-one relationship

Hello everyone,


I encounter a very strange bug/problem.


I'm syncing locally CloudKits objects with CoreData and AleCrim framework.

I have 2 entities in my core data model:

  • Towns
  • Restaurant

I have defined the relationship one to many and inverse. (yep, you've got it, a Town can have multiple restaurants ;))

I'm using the CoreData Stack to persist elements from CloudKit and the AlecrimCoreData library. In a class that grab content from ClouKit, I create the context thanks to Alecrim:


let dataContext = DataContext()


Then I have a bunch of functions that call class functions that create if needed the NSManagedObjects


let town = Towns.createTownsFromCK(record, dataContext: self.dataContext) ... let restaurant = Restaurants.createRestaurantFromCK(record, dataContext: self.dataContext) ...


As you can see I transmit the dataContext. So it's the same for Restaurants and Towns. CloudKit restaurant objects are grabbed after Towns have been locally saved.


do { try dataContext.save() } catch { }


let restaurantPredicate = NSPredicate(format: "town == %@", record.recordID) let restaurantQuery = CKQuery(recordType: "Restaurants", predicate: restaurantPredicate) self.publicDB.addOperation(createQueryOperation(restaurantQuery, cursor: nil, resultblock:fetchedRestaurantsRecord))


When grabbed, I can create restaurants NSManagedObject and add properties, but when I want to link the town, I have this strange error when I set the town:


if let cktownreference = record["town"] as? CKReference, town = dataContext.towns.filter({ $0.ckid! == cktownreference.recordID.recordName }).first {

     restaurant.town = town

}


The error (weird one...):

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-one relationship: property = "town"; desired type = Towns; given type = Towns; value = (entity: Towns; id: 0xd000000000080000 ; data: { ckid = "6665ca0f-af57-4305-8e18-ced7540e6279"; name = Paris; recordChangeTag = ii0l38hp; restaurant = ""; }).'


I have tried to create a new context, I have tried to set the Town NSSet, same results.

Here a re 2 screenshots of my model

Restaurant model & relationship

Town model & relationship

This problem is for every one-many link I have.

Thanks for your help.

Hi, I'm facing the same problem here, have you managed to solved it?


Kind regards,

Looking over your code fragments, it appears that you have a CloudKit class called Towns, and you also appear to have a CoreData Entity named Towns. Do you happen to also have an NSManagedObject subclassed named Towns? I'm assuming the answer is "Yes" and they are in different packages.


My guess is that the error message isn't displaying the fully qualified types, probably due to the age of the code involved going back to before there was a such thing as packages in Objective C.

desired type = Towns; given type = Towns - Unacceptable type of value for to-one relationship
 
 
Q