Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTKit deleting object when the response is empty

I have been using RESTKit for a while now and its been easier to load json save them in database. But, I have been googling for sometimes and could not find solution for this problem.

I have a managed object model set up such that three different entities has a straight one to one relationship with each other. The layout for my model is as ;

enter image description here

If I load the object on Friend class it also maps to the rest two models on the basis of the mapping relationship in Friend model.

Say the user connects his facebook account and linked in account, then it is obvious that the json response would contain the mapping for the linkedin and friend model. The json response in such case would be;

[
   {
      "name":"foo bar",
      "id":307,
      "facebook":{
         "name":"foo bar",
         "username":"foo.bar.5074",
         "id":"1000013421379389",
         "picture":"https://fbcdn-profile-a.akamaihd.net/foo-bar.jpg"
      },
     "linkedin":{
         "name":"foo bar",
         "headline":"some headline",
         "id":"1000013",
         "link":"http://www.linkedin.com/foo.bar",
         "image":"https://linkedin.com/foo-bar.jpg"
      },
      "picture":"https://fbcdn-profile-a.akamaihd.net/foo-bar.jpg"
   },
   {
     ... 
    }
  ]

But, once the user has linked Facebook and Linkedin, he may also unlink it through website. So, if the user unlinks it, the json response would not have the same mapping as it would generally and may not contain the facebook object or linkedin object in json response such that the response would look like,

[
       {
          "name":"foo bar",
          "id":307,
          "facebook":{
             "name":"foo bar",
             "username":"foo.bar.5074",
             "id":"1000013421379389",
             "picture":"https://fbcdn-profile-a.akamaihd.net/foo-bar.jpg"
          },
          "picture":"https://fbcdn-profile-a.akamaihd.net/foo-bar.jpg"
       },
       {
         ... 
        }
      ]

If such response comes, I would like to delete the existing linked in record for the user. Or may be if the response doesnot have facebook or both I would like to delete local record and the association for the model whichever is empty in the response.

I would want the RESTKit to delete the associated objects automatically. I suppose it is possible through Restkit by specifying the fetch request in object loader but I am not particularly sure about it. I am using the loader pattern as this to load the object so where would I specify the fetch request,

EDITED

RKObjectMappingProvider *provider = [RKObjectMappingProvider mappingProvider];
[provider setObjectMapping:[self objectMappingForFriend] forKeyPath:@""];

[provider setObjectMapping:[self objectMappingForFriend] forResourcePathPattern:resourcePath withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {

    return [Friend fetchRequest];

}];

    [[[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader){
        loader.mappingProvider = provider;
        loader.delegate = self;
        loader.method = RKRequestMethodGET;
    }];

_I think I have redundant code here. _

With this I hoped to delete all the objects in the local store when the response is empty. But, it does not work for me. I dont know how I should get around with this problem.

Updates: 2:23 PM July 8, 2012 Local time

self.objectManager = [RKObjectManager objectManagerWithBaseURL:[NSURL URLWithString: @"https://mywebsite"]];
        self.objectManager.client.disableCertificateValidation = YES;
        self.objectManager.client.cachePolicy = RKRequestCachePolicyLoadIfOffline | RKRequestCachePolicyLoadOnError | RKRequestCachePolicyTimeout | RKRequestCachePolicyEtag;
        self.objectManager.requestCache.storagePolicy = RKRequestCacheStoragePolicyPermanently;
        self.objectManager.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;
        self.objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"test.sqlite" ];
        self.objectManager.objectStore.cacheStrategy = [RKInMemoryManagedObjectCache new];

Now, setting the mapping ad loading data,

RKObjectMappingProvider *provider = [RKObjectMappingProvider mappingProvider];
[provider setObjectMapping:[self objectMappingForFriend] forKeyPath:@""];

[provider setObjectMapping:[self objectMappingForFriend] forResourcePathPattern:resourcePath withFetchRequestBlock:^NSFetchRequest *(NSString *resourcePath) {

    return [Friend fetchRequest];

}];

    [[[RKObjectManager sharedManager] loadObjectsAtResourcePath:resourcePath usingBlock:^(RKObjectLoader *loader){
        loader.mappingProvider = provider;
        loader.delegate = self;
        loader.method = RKRequestMethodGET;
    }];

Updates: 4:11 PM July 8, 2012 Local time

It seems like it is deleting some nullifying some local objects but only some many of the object have the references and the main model Friend still exist even if there is empty json response.

like image 398
Sandeep Avatar asked Jan 17 '23 02:01

Sandeep


1 Answers

I have to assume you are using the latest version of RestKit. So, try this line:

[RKObjectManager sharedManager].objectStore.managedObjectCache = [RKFetchRequestManagedObjectCache new];

For a successful GET request it will eventually call [RKManagedObjectLoader deleteCachedObjectsMissingFromResult:] which will uncache your local data. The principle is explained better in the article I linked to in my previous answer to your previous question.

like image 160
Paul de Lange Avatar answered Jan 28 '23 20:01

Paul de Lange