I have a Message object that has a messageID property. I would like to make sure that there is only one instance of the Message object with the given messageID. In SQL, I would simply add a unique constraint to the messageID column, but I don't know how to do this with Core Data. I donβt think it can be done in the data model itself, so how do you do it?
My initial thought was to use a validation method to fetch in the context of an NSManagedObject for an identifier, see if it finds anything other than itself, and if so, the validation fails. I suspect this will work, but I'm worried about the performance of something like that. I made a lot of efforts to minimize the sample requests needed for the entire import procedure, and checking it by fetching for each individual message object seems a bit overwhelming. I can get all the pre-existing objects that I need and identify all the new objects that I need to insert into the repository using only two fetch requests before I do the actual work of importing and connecting everything together. This would add a selection for each individual update or insert in addition to the two, which would seem to eliminate any performance advantage that I had in the pre-processing of import data in the first place!
The main reason is that the importer can (potentially) run several batches simultaneously on several streams and may include some duplicate / duplicate data, which ultimately should lead to only one object in the storage, and not duplicate records. Is there any reasonable way to do this and do what I ask to make sense for Core Data?
source share