Simperium/simperium-ios — pull request #121 by mackross (2013-08-23 07:46:35+00:00)
Title: Refactored different members out in favour of SPJSONDiff and policies and

Added support for embeddedObjects in 1-1 and 1-* relationships.

:warning: not yet ready to merge :warning:
- [ ] Needs to use policies on transformable members so that if they transform to strings they don't do string diffs rather than replaces.
- [ ] Support operation type for member to force operation types.
- [ ] Wait for jsondiff list diff spec to be properly fleshed out for full recursive and then implement in objc.
- [ ] Probably a few more tests on the member stuff would be a good idea.

Hey @mikejohnstn, @Rjonhson, @tomwitkin, @jleandroperez

I've done some a first pass on recursive embedded objects and embedded object lists. I've tested sending objects up and down without problems. I've got to do some tests on transformations tomorrow and iron out any bugs there are. I've heavily refactored the SPMember and SPDiffer classes. In fact the entire diff/applydiff/transformation process occurs completely in json formatted values (its pretty much a direct port of jsondiff.js). The member class then uses the transformed json formatted values to update the core data model.

The biggest benefit of this change is that data is much easier to access with the javascript libraries and data is synced as conceptually whole units. For example in our app we have an inspection entity that inspection areas which in turn have inspection items which have photos ( [inspection]1-*[areas]1-*[items]1-*[photo-reference] ). This used to be stored on simperium in 4 buckets. Now it is stored as just 1 and as soon as the inspection is loaded it's guaranteed that all the areas, items, and photo references are updated and ready to go too.

The way that it works is pretty simple. Like normal you make your bucket objects inherit from SPManagedObject. All objects that you want to embed in relationships must inherit from SPEmbeddedManagedObject and the relationship to the SPEmbeddedManagedObject from the object must have the userInfo key 'spEmbed'. I've also changed the base64 type to a transformable type that can have a custom transformer set on it. A useful case for this might be if you store UIColor on your entity as a transformable. If you create a UIColorToHexStringValueTransformer class and then set spJSONTransformerName in the userInfo dictionary to this class name. The UIColor would now be stored in simperium as the hex string but in core data as a UIColor (archived with NSKeyedArchiver most liked).

Please let me know what you think. We're going to be rolling this out into our production app very soon.

PS Apologies for the spelling and what not, it's late!