Note that there are some explanatory texts on larger screens.

plurals
  1. POis there a way to clear -changedValues for only certain NSMangedObjects in Core Data?
    primarykey
    data
    text
    <p>my problem is about coordinating core data's awareness of changes in objects (including relationships) with posting this updates to a server.</p> <p>For example:</p> <ol> <li><p>Say I have a managed object with 2 to-many relationships. I do a get on a server to get the objects contained in both relationships. For relationship A, the server returns that 1 object belongs. For relationship B the server returns there's none. So for A, I parse the JSON response into an NSManagedObject and insert it into relationship A. For B I do nothing. At this point, if I was to query <code>-changedValues</code> in the only object in rel A, It'd say that the inverse of rel A changed for it, since it was inserted into rel A. Since I want the objects fetched from the server to represent the "default" or "last committed" state, I issue a core data context save, at which point, changedValues does not reflect the insertion anymore. So far so good.</p></li> <li><p>Next, I locally (not by fetching from the server), create 2 objects. I insert one in rel A (it now has 2 objects) and another on rel B (now has 1 object).</p></li> <li>At this point, I'd like the whole system to be aware of the fact that the first object in rel A was there to start with so nothing needs to be done about it. The second one in A is a new insertion and so it needs to be posted to the server and the third object which is the only one in rel B, is also a new insertion and so needs a POST.</li> </ol> <p>I tried approaching this by querying the context for all objects of the appropriate relationship destination entity that had a value for -valueChanged over the key named after the appropriate relationship['s inverse. That is for some object x with these relationships, the current state is:</p> <pre><code>x.relA = &lt;NSSet: obj1, obj2&gt; x.relB = &lt;NSSet: obj3&gt; </code></pre> <p>and the query is essentially </p> <pre><code>// For each relationship: NSError *error = nil; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:mapping.entityName]; NSArray *fetchResult = [self.context executeFetchRequest:fetchRequest error:&amp;error]; NSArray *deletedObjects = [fetchResult filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { id relationship = [evaluatedObject changedValues][&lt;inverseOfRelationship&gt;]; return relationship &amp;&amp; ![relationship containsObject:x]; }]]; NSArray *insertedObjects = [fetchResult filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) { id relationship = [evaluatedObject changedValues][&lt;inverseOfRelationship&gt;]; return relationship &amp;&amp; [relationship containsObject:x]; }]]; </code></pre> <p>This works in principle. But the problem is, say I first evaluate rel A. I get the list of deletions and insertions (not counting what was already there by default.) and issue the appropriate DELETE and POST to the server for those 2 arrays. So far so good. However, now I issue a coredata context save so that this relationships current state becomes the "default" state. OK. sure... BUT... the save also wipes out the changes for obj3 in rel B. So if I now try to query that relationship with the above code, obj3s' changedValues will not report any value since in theory obj3's membership in rel B is the default (since the last save at least).</p> <p>So I need a way to either do a selective context save for some objects only (which I believe is not possible since this is a "context" save and not an object save), or some way to selectively clear changedValues for the objects I've already processed with the server, for instance. </p> <p>Another approach would be inserting extra updated / deleted flags in the ManagedObjects and handle those manually via my own code. Seems viable but kind of ghetto, since it's essentially repurposing flags that managedObjects already have in place in relation to the context.</p> <p>Any thoughts or ideas?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload