Search code examples
iosstrongloop

Examples showing how to query for object relations from loopback SDK


I am trying to query a loopback server for relations between models, I have a "Section" Model and an "item" model, and a hasMany relation between them, so I can query for all items in a section with /sections/:id/items from a rest client.

How can I do the same query from loopback iOS client? any documentation or code examples?


Solution

  • The iOS SDK supports filtering against a Model, use the invokeStaticMethod on your LBModelPrototype and pass the filter parameters: @{ @"filter[order]":@"inventory ASC",@"filter[limit]":@3} string.

    [objectProto invokeStaticMethod:@"filter" parameters:@{ @"filter[order]":@"inventory ASC",@"filter[limit]":@3} success:staticMethodSuccessBlock failure:staticMethodErrorBlock];
    

    I made a quick iOS example to show you a simple filter against a custom model "product" type you can find it here

    If multiple filter requests are not a good solution you can also create a custom remote method to provide a custom method on a model type. in the Node middle tier LoopBack data Juggler Data Source Juggler provides ORM features for managing multiple data relations

    Engineering team also provided some feedback on the LoopBack-ios GitHub repo:

    This isn't supported yet in the SDK. You should be able to accomplish this by subclassing LBModel and adding a method similar to this...

    - (void)relatedItems:(LBModelAllSuccessBlock)success
                       failure:(SLFailureBlock)failure {
        [self invokeMethod:@"items"
                parameters:@{}
                   success:^(id value) {
                       NSAssert([[value class] isSubclassOfClass:[NSArray class]], @"Received non-Array: %@", value);
    
                       NSMutableArray *models = [NSMutableArray array];
    
                       [value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                           [models addObject:[self modelWithDictionary:obj]];
                       }];
    
                       success(models);
                   }
                   failure:failure];
    }