strongloop-community / loopback-sdk-ios

iOS Client SDK for the LoopBack framework.
Other
76 stars 39 forks source link

Accessing the results of an include filter #115

Open jpswensen opened 8 years ago

jpswensen commented 8 years ago

I am trying to figure out how to access the results of an include filter (I am using swift). I have tried a ton of things, but none of them seem to be working.

For example, once I execute the findWithFilter and stop at a breakpoint I can see that the results actually do include the included items (user_closet_items). (lldb) po closet <WRWW.UserCloset { "closet_name" = "My First Closet"; "create_date" = "2016-06-25 07:50:51 +0000"; "delete_datetime" = ""; "owner_id" = 3; "privacy_level_id" = 0; "user_closet_items" = ( { "create_date" = "2016-06-25T00:00:00.000Z"; "delete_date" = ""; id = 4; "item_img_url" = string; "item_name" = "Item 4"; "user_closet_id" = 3; }, { "create_date" = "2016-06-25T00:00:00.000Z"; "delete_date" = ""; id = 5; "item_img_url" = string; "item_name" = "Item 5"; "user_closet_id" = 3; } ); "user_location_id" = 0; "wardrobe_type_id" = 0; }>

However, for the life of me I can't actually figure out how to access them. I've tried a bunch of stuff by adding an [AnyObject?] member variable to the derived model, but can't seem to get anything to work. I search through all the examples and all the usual sources (Google, StackOverflow) and still can't get help. Any suggestions?

jpswensen commented 8 years ago

As a follow-up, I finally found something that works. In the parent model that has a "hasMany" relationship I added var user_closet_items: [NSDictionary]? = nil

Then, in the response to findWithFilter I added the following code let closetItem:UserClosetItem = AppDelegate.userClosetItemRepository.modelWithDictionary(closet.user_closet_items![idx] as [NSObject : AnyObject]) as! UserClosetItem

I guess the only problem with this is that I still don't get it for free. I have been thinking about it and looking through the LBModel and LBModelRepository code and I think it may be possible, but you would need to somehow infer the correct LBModelRepository to be able to call modelWithDictionary on the right repository. This could probably be done if the model's variable was var user_closet_items:[UserClosetItem]? = nil so that the UserClosetItemRepository.modelWithDictionary() could be used.

So, unless the library already has this feature and I just don't know how to use it, I may try to make a patch and submit it. Please let me know if the feature doesn't exist and this would interest you.