Reputation: 21144
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 ;
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.
Upvotes: 4
Views: 2877
Reputation: 21144
With the new RestKit this can be done using the fetchRequest block as;
[[RKObjectManager sharedManager] addFetchRequestBlock:[self fetchRequestBlock]];
-(RKFetchRequestBlock)fetchRequestBlock{
RKFetchRequestBlock fetchRequestBlock = ^NSFetchRequest*(NSURL *url){
if( [url.relativePath isEqualToString:@"api/app/v1/network_contacts"] ){
return [NSFetchRequest fetchRequestWithEntityName:@"Person"] ;
}
return nil ;
};
return fetchRequestBlock;
}
This method queries the local database and compares the remote object and if un matched deletes the object from local store.
Upvotes: 1
Reputation: 1767
Here is how you setup your cache strategy
[RKObjectManager sharedManager].objectStore.cacheStrategy = [RKFetchRequestManagedObjectCache new];
Upvotes: 2
Reputation: 10633
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.
Upvotes: 2
Reputation: 10633
You have two settings to configure here. Firstly, in your RKObjectMapping you set setNilForMissingRelationships to YES. Then you also need to make sure the Core Data relationship has the correct Delete Rule (in the Utilities panel when editing the relationship). See this for a description of what each Delete Rule does.
Upvotes: 4