Restkit: GET remote linked object when foreign key

2019-07-27 18:25发布

I cannot figure out if Restkit is able to download an object not present locally, in particular when a foreign key is referring to that missing object in a to-one relationship in Core Data.

Take the following example: - contact 1 refers to company 2

  • contact 1 is present in the local db but company 2 is not

  • when in the UI the user inspects the details of contact 1, then a GET for the contact 1 is performed

  • the GET returns a JSON containing among other contact details the property company_id = 2

  • I have already setup Restkit via the foreign key convention where I store the foreign key in the contact object (companyID) and I link it to the remote identifier (identifier) stored in the company object:

    [contactResponseMapping addConnectionForRelationship:@"forCustomer" connectedBy:@{@"companyID" : @"identifier"}]

I do not manage in this case to configure restkit to download the entity (referred to by the company_id) if not present in the local db.

Is this possible?

The alternative workaround would be to override the companyID setter and double check if the entity exists and if not download the entity, although this is not desirable to me as I have setup an engine that everytime that an object is edited receives a notification and post it to the server. This means I'd have to block the thread execution until the linked object is downloaded.

Also is this mechanism called lazy loading? or hydrating entities? I cannot find any other similar cases around.

Hope you can help, going a bit crazy on this. Thanks a lot.

PS: I am using restkit 0.21.0 (i.e. the latest development release which Blake Watters confirmed to be stable)

1条回答
倾城 Initia
2楼-- · 2019-07-27 18:59

This is not a feature that RestKit currently offers (probably because of the types of issues you discuss).

For your workaround, consider what your engine is doing in relation to relationship edits - how are they pushed back to the server? Are they always pushed?

Also, think about creating stub objects for your 'foreign' objects so that at least some representation always exists (there are a couple of ways to do this, you can setup mappings to accomplish the task). Then when you come to use one of these objects you can hydrate / lazy load it.

See also Clarifying terminology : "Hydrating" an entity : Fetching properties from the DB.

查看更多
登录 后发表回答