Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to serialize a NSPredicate object?
    primarykey
    data
    text
    <p>Is there a way to inspect a NSPredicate object for the purposes of serializing it into a URL? I am trying to retrieve the data remotely and need to translate the predicate object into a URL with querystring parameters that the server understands.</p> <p>This was inspired by a talk given in WWDC 2010 called "Building a Server Driven User EXperience" where the speakers talk about using Core-Data and with a server backend. I have followed the session video and slides, but am stuck on the serializing point. For example, there is a <code>Person</code> object, and I'm trying to fetch all people whose first name is "John". I am using a subclass of <code>NSManagedObjectContext</code> called <code>RemoteManagedObjectContext</code>, which overrides the <code>executeFetchRequest</code> method, and is supposed to send the call to the server instead. The fetch request is being created as (ellipsed non-essential parts):</p> <pre><code>@implementation PeopleViewController - (NSArray *)getPeople { RemoteFetchRequest *fetchRequest = [[RemoteFetchRequest alloc] init]; NSEntityDescription *entity = ... NSPredicate *template = [NSPredicate predicateWithFormat: @"name == $NAME AND endpoint = $ENDPOINT"]; NSPredicate *predicate = [template predicateWithSubstitutionVariables:...]; [fetchRequest setEntity:entity]; [fetchRequest setPredicate:predicate]; NSError *error = nil; // the custom subclass of NSManagedObjectContext executes this return [remoteMOC executeFetchRequest:fetchRequest error:&amp;error]; } @end </code></pre> <p>Now inside the custom subclass of <code>NSManagedObjectContext</code>, how can I serialize the fetch request into querystring parameters suitable for the server. So given the above fetch request, the corresponding URL would be:</p> <pre><code>http://example.com/people?name=John </code></pre> <p>It is possible to get a string representation of the predicate which returns,</p> <pre><code>name == "John" AND endpoint == "people" </code></pre> <p>that I can parse to get the parameters <code>name</code>, and <code>endpoint</code>. However, is it possible to do it without parsing the string? Here's a partial implementation of the <code>RemoteManagedObjectContext</code> class.</p> <pre><code>@implementation RemoteManagedObjectContext - (NSArray *)executeFetchRequest:(NSFetchRequest *)request error:(NSError **)error { // this gives name == "John" AND endpoint == "people" // don't know how else to retrieve the predicate data NSLog(@"%@", [[request predicate] predicateFormat]); ... } @end </code></pre>
    singulars
    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