mutualmobile/MMRecord

Automatic fetch and network download of relationship objects

thebarndog opened this issue · 4 comments

If I have a model with a relationship, either to-many or one-to-one, is it possible to use MMRecord to not only automatically do a core data fetch via the primary key(s) of that relationship and then, if it's not found locally, do a fetch via the network?

For example, lets say this is my Post model:

{
Post_ID:1266634,
Post_Content: I am a post!,
Post_Creator: 45, //primary key for user object that posted
Post_Comments:[2, 7, 5, 99], //list of primary keys for comments
Post_Likes:[1, 100, 39, 6] //list of user model primary keys
}

Is it possible to configure MMRecord or is there a place to insert some logic where based on the entity class that the fetch is being performed on, to insert my own network logic on the contingency that the core data fetch comes back with nothing?

Ha, interesting. I wondered if I'd ever get this feature request. The first version of MMRecord internally at Mutual Mobile actually included this functionality, but I removed it because it felt too chatty.

You could absolutely implement something like this inside of a MMRecordRepresentation subclass. The representation is tied to a specific entity, and based on the input dictionary and the presence (or lack thereof) of certain attributes you could trigger a network request to fetch a specific relationship.

The main catch would be how that request gets handled. It would be pretty difficult to block the original request from returning while those requests were performed, and even then I'm not sure that you would want to. It MIGHT be possible to extend the existing batching functionality to trigger the batch completion block after all the child requests finished, but I'm not sure yet.

I'm still not convinced this is something I want the library to support out of the box, but I do want to make sure its possible to extend it to support this if you need to. Do you feel like taking a look at the MMRecordRepresentation API and see if that will do what you need?

Thanks,

  • Conrad

Sure thing! I thought that might be the way to do it. Yeah it seems that this feature is somewhat of a power user feature and probably only comes up in complex cases like mine where I have a post model with references to multiple relationships, both one-to-one and one-to-many.

My initial theory without digging in too deep is, although it'll be a lot of code duplication, is to create a unique MMRecordRepresentation per model so that each model class can handle the network requests independently of one another. I'll fork master and maybe add a few methods like - (void)performFetchForRelationship:(BOOL)local or something like that.

I'm gonna close this for now. The more I think about it, I think I would actually just perform the secondary fetches in the successBlock from the first request. Alternatively, you could also use the customResponseBlock functionality to gain a little bit more access within the parsing system. Alternatively again, you could also use the prePopulationBlock to do the same thing :)

Since achieving the main goal is certainly possible already, and the details around how that request gets made are so implementation/case-by-case specific, I'm hesitant to make major changes to implement it in a specific fashion here.

Cheers,

  • Conrad

Excellent! Best repo on Github eva.