2
votes

linked RestKit issue #1604

If my API gives me no id attribute, but i still want to cache the objects via Core Data, what should i use to identify my object. For example i have

response =  {
  translation =     {
          text = "longlongtext";
          dictionary = "general";
          lang = "en";
      };
  otherdata =     {
          author = "May";
          date = "434134";
      };
}

So i would be glad to use hashed (md5) translation text as an id string. Notice that my future requests which happen without network connection should be able to identify this cached entity and give it as a result.

I cant declare mapping to fill responseID property from [translation.text md5hash] to use as responseMapping.identificationAttributes = @[ responseID ]; because mappings doesnt have such feature.

4
So your concern really is matching existing data for offline requests? - Wain
@Wain yep thats what i really care about. - Baca6u

4 Answers

3
votes

As proposed by @segiddins in the github issue discussion:

... in your managed object subclass, hook into one of the core data callbacks to generate a compound key that is saved as part of the model and just use that key as your identification attribute.

The approach may look like this:

@property (nonatomic, copy) NSString *identifier;


- (void)willSave
{
    [super willSave];

    NSString *computedIdentifier = [[NSString stringWithFormat:@"%@%@", self.text, self.langCode] md5hash];
    [self setPrimitiveValue:computedIdentifier forKey:@"identifier"];
}
1
votes

I also wanted to do a hash of the JSON fields like you, but as you know it's not possible. I ended up doing the following to achieve (I believe) the same end result, which is for JSON objects returned without a unique ID, a unique identification attribute is generated by RestKit:

    entityMapping.identificationAttributes = @[ @"text",@"dictionary",@"lang",@"author",@"date" ];
0
votes

You should keep this kind of functionality outside of RestKit if you have no identifiers being provided by the server.

I would generate a custom identifier for each request you make (a GUID), I'd save that identifier into each of the result objects in the RestKit success completion block. I'd also save the request details and the identifier into user defaults.

Now, when the user makes a request and they are offline you can analyse user defaults to determine if it's a repeat request and find the identifier with which to query the results from the data store.

0
votes

Just to clarify about offline requests after discussion. In the end, such feature (offline requests) does not exist inside RestKit. The way you can achieve it is complicated, but possible. Steps are:

  • you use CoreData with RestKit (RKEntityMapping, managed objects etc)
  • Your provide good identification attributes for entities. It can be URL of request from @metadata.
  • on both success and failure callbacks from getObjectsAtPath you query CoreData with fetch request and return the result just the same way as if it was loaded directly and taken from mappingResult.firstObject and mark it as cached if it is old result loaded on failure.