4

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.

4

4 回答 4

4

您需要在此处配置两个设置。首先,在您的 RKObjectMapping 中,您将 setNilForMissingRelationships 设置为 YES。然后您还需要确保核心数据关系具有正确的删除规则(在编辑关系时在实用程序面板中)。有关每个删除规则的作用的描述,请参阅此内容。

于 2012-07-18T06:42:02.230 回答
2

我必须假设您使用的是最新版本的 RestKit。所以,试试这一行:

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

对于成功的 GET 请求,它最终会调用 [RKManagedObjectLoader deleteCachedObjectsMissingFromResult:] 这将取消缓存您的本地数据。在我之前对您上一个问题的回答中链接到的文章中更好地解释了该原理。

于 2012-07-18T09:45:04.273 回答
2

这是您设置缓存策略的方法

[RKObjectManager sharedManager].objectStore.cacheStrategy = [RKFetchRequestManagedObjectCache new];
于 2012-11-08T22:13:09.210 回答
1

使用新的 RestKit,这可以使用 fetchRequest 块来完成:

 [[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;
}

此方法查询本地数据库并比较远程对象,如果不匹配,则从本地存储中删除对象。

于 2014-03-13T23:10:59.417 回答