ObjCMongoDB (of which I'm author) performs deserialization to Core Data, but, at present, not deduplication or syncing. It treats BSON documents as simple ordered dictionaries, without a sense of an identification attribute. In particular, it treats _id
like any other key.
You can get the behavior you want but you'll need to implement it yourself.
The easiest way would be to implement -awakeAfterUsingCoder:
or -awakeAfterUsingBSONDecoder:
– doesn't matter which – in your NSManagedObject subclass. The decoder will automatically invoke these.
self
will be the new, deserialized object. Run a fetch request to look for an existing entry which self
duplicates. If you find one, apply the properties to the existing object, delete the new object from the context, and return the existing entry. If you don't find one, simply return self
.
You can use -dictionaryWithValuesForKeys:
and -setValuesForKeysWithDictionary:
to accomplish this, and I usually get a list of keys with a method like this:
+ (NSArray *) persistentKeysForEntity:(NSEntityDescription *) entity {
NSMutableArray *result = [NSMutableArray array];
for (NSPropertyDescription *pdesc in [entity properties]) {
if ([pdesc isTransient]) continue;
[result addObject:pdesc.name];
}
return [result copy];
}
Added: If you don't need to exclude transient attributes, you can apply the values with one line:
[target setValuesForKeysWithDictionary:[self dictionaryWithValuesForKeys:self.entity.attributesByName.allKeys]];