{"id":85,"date":"2008-09-27T15:53:00","date_gmt":"2008-09-27T23:53:00","guid":{"rendered":"http:\/\/eschatologist.net\/blog\/?p=85"},"modified":"2009-01-30T19:10:33","modified_gmt":"2009-01-31T03:10:33","slug":"lets-merge-managed-object-models","status":"publish","type":"post","link":"https:\/\/eschatologist.net\/blog\/?p=85","title":{"rendered":"Let&#8217;s merge managed object models!"},"content":{"rendered":"<p>There was a question recently on <a href=\"http:\/\/stackoverflow.com\/\">Stack Overflow<\/a> asking <a href=\"http:\/\/stackoverflow.com\/questions\/130316\/cross-model-relationships-in-nsmanagedobjectmodel-from-merged-models\">how to handle cross-model relationships in managed object models<\/a>.  Now, the poster wasn&#8217;t asking about how to handle relationships across <em>persistent stores<\/em> \u00e2\u20ac\u201d he was asking how to handle splitting a model up into pieces such that the pieces could be recombined.<\/p>\n<p>It turns out that this is somewhat straightforward to do using Core Data.  Let&#8217;s say you have a simple model with Song and Artist entities.  I&#8217;ll write it out here in a pseudo-modeling language for ease of reading:  <code><\/p>\n<pre>MusicModel = {\n    Song = {\n        attribute title : string;\n        attribute duration : float;\n        to-one-relationship artist : Artist,\n            inverse : songs,\n            delete-rule : nullify;\n        userInfo = { };\n    };\n\n    Artist = {\n        attribute name : string;\n        to-many-relationship songs : Song,\n            inverse : artist,\n            delete-rule : cascade;\n        userInfo = { };\n    };\n};<\/pre>\n<p><\/code>  Now let&#8217;s say you want to split this up into two models, where Song is in one and Artist is in the other.  You could just try and create two <code>xcdatamodel<\/code> files in Xcode, one with each entity, and wire the relationships together after loading them and merging them with <code><a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/CoreDataFramework\/Classes\/NSManagedObjectModel_Class\/Reference\/Reference.html#\/\/apple_ref\/occ\/clm\/NSManagedObjectModel\/modelByMergingModels:\">+[NSManagedObjectModel modelByMergingModels:]<\/a><\/code>.  Except that won&#8217;t work:  Relationships with no destination entity won&#8217;t be compiled by the model compiler.<\/p>\n<p>What else might you try?  You could try just putting dummy entities in for relationships to point to.  However, merging models will fail then, because <a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Reference\/CoreDataFramework\/Classes\/NSManagedObjectModel_Class\/Reference\/Reference.html\">NSManagedObjetModel<\/a> won&#8217;t merge models that have entity name collisions.<\/p>\n<p>It turns out, though, that you can merge models very easily by hand, by taking advantage of the way Core Data&#8217;s model-description objects handle the <code>NSCopying<\/code> protocol.  All you have to do is create your destination model, loop through every entity in each of your source  models, and copy every entity that you <em>haven&#8217;t<\/em> tagged as a stand-in using a special key in their <code>userInfo<\/code> dictionary.<\/p>\n<p>Why does this work?  The trick is that before you tell a persistent store coordinator to <em>use<\/em> a model, that model is mutable and references relationship destination entities and inverse relationships by <em>name<\/em>.  So you can have only a minimal representation of Artist in one model, and a minimal representation of Song in another model:  <code><\/p>\n<pre>SongModel = {\n    Song = {\n        attribute title : string;\n        attribute duration : float;\n        to-one-relationship artist : Artist,\n            inverse : songs,\n            delete-rule : nullify;\n        userInfo = { };\n    };\n\n    Artist = {\n        \/* Note no attributes. *\/\n        to-many-relationship songs : Song,\n            inverse : artist,\n            delete-rule : cascade;\n        userInfo = { IsPlaceholder = YES; };\n    };\n};\n\nArtistModel = {\n    Song = {\n        \/* Note no attributes. *\/\n        to-one-relationship artist : Artist,\n            inverse : songs,\n            delete-rule : nullify;\n        userInfo = { IsPlaceholder = YES; };\n    };\n\n    Artist = {\n        attribute name : string;\n        to-many-relationship songs : Song,\n            inverse : artist,\n            delete-rule : cascade;\n        userInfo = { };\n    };\n};<\/pre>\n<p><\/code>  Then, when you write some code to combine them, the merged model will wind up with the full definition of Song and the full definition of Artist.  Here&#8217;s an example of the code you might write to do this:  <code><\/p>\n<pre>- (NSManagedObjectModel *)mergeModelsReplacingDuplicates:(NSArray *)models {\n    NSManagedObjectModel *mergedModel = [[[NSManagedObjectModel alloc] init] autorelease];\n\n    \/\/ General strategy:  For each model, copy its non-placeholder entities\n    \/\/ and add them to the merged model. Placeholder entities are identified\n    \/\/ by a MyRealEntity key in their userInfo (which names their real entity,\n    \/\/ though their mere existence is sufficient for the merging).\n\n    NSMutableArray *mergedModelEntities = [NSMutableArray arrayWithCapacity:0];\n\n    for (NSManagedObjectModel *model in models) {\n        for (NSEntityDescription *entity in [model entities]) {\n            if ([[[entity userInfo] objectForKey:@\"IsPlaceholder\"] boolValue]) {\n                \/\/ Ignore placeholder.\n            } else {\n                NSEntityDescription *newEntity = [entity copy];\n                [mergedModelEntities addObject:newEntity];\n                [newEntity release];\n            }\n        }\n    }\n\n    [mergedModel setEntities:mergedModelEntities];\n\n    return mergedModel;\n}<\/pre>\n<p><\/code>  This may seem like a bit of overhead for this simple example.  The critical thing to see above is that <em>only that which is necessary for model consistency is in the placeholder entities<\/em>.  Thus you only need the inverse relationship from Song to Artist in ArtistModel.  Say you wanted to add a Picture entity related to the Artist entity \u00e2\u20ac\u201d you don&#8217;t have to add that to both models, only to ArtistModel.  The benefit of this method for merging models should then be pretty apparent:  It gives you the ability to make your model separable, just like your code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There was a question recently on Stack Overflow asking how to handle cross-model relationships in managed object models. Now, the poster wasn&#8217;t asking about how to handle relationships across persistent stores \u00e2\u20ac\u201d he was asking how to handle splitting a model up into pieces such that the pieces could be recombined. It turns out that&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3],"tags":[9,8,17],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-technology","tag-cocoa","tag-core-data","tag-xcode"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p74loH-1n","_links":{"self":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=85"}],"version-history":[{"count":5,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":92,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/85\/revisions\/92"}],"wp:attachment":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}