Skip to content

Casting child [filter:include] models w/ LBPersistedModels #2

Description

@soltrinox

I must be missing something wrong because i can not directly translate child objects that are "filter[include]" to their respective model class types. I created a parser method to handle this.
Any recommendations to do it easier or correctly?

I request the child objects through a parent model like this

 [[[self adapter] contract] addItem:[SLRESTContractItem itemWithPattern:@"/Notes" verb:@"GET"] forMethod:@"Notes.filterOne"];
    [self.notesModelRepository
    invokeStaticMethod:@"filterOne"
    parameters: @{ @"filter[include]":
    @[ @"TAGS", @{@"profile" : @[@"ACCOUNT", @"TYPE", @"MEDIA", @"STATUS" ]}],
    @"filter[where]": @{ @"id" : iid } }
         success:staticMethodSuccessBlock    failure:staticMethodErrorBlock];

My Child Model Class is here

@interface MediaModel : LBPersistedModel
@property (nonatomic, copy) NSString *URL1;         //  (string, optional),
@property (nonatomic, copy) NSDate *URL2;           //  (string, optional),
@property (nonatomic, copy) NSDate *DATA;           //  (any, optional),
@property (nonatomic, copy) NSString *created;            //  (string, optional),
@property (nonatomic, copy) NSString *updated;             //  (string, optional),
@property (nonatomic, copy) NSString *REFERENCE;        //  (any, optional),
@property (nonatomic, copy) NSString *mediaId;          //  (objectid, optional)

//  =====================   RELATIONS  ========================
@property (weak) NSObject *TYPE;
- (MediaModel*) transformMediaModel:(LBPersistedModel *)model;
- (MediaModel*) transformMediaObject:(NSObject *)model;
- (NSMutableArray *) processArrayMediaModels:(NSArray*)model;

@end

And the model looks like this

-(MediaModel*) transformMediaObject:(NSObject *)model{

    [self setValue:[model valueForKeyPath:@"created"] forKeyPath:@"created"];
    [self setValue:[model valueForKeyPath:@"updated"] forKeyPath:@"updated"];
    [self setValue:[model valueForKeyPath:@"id"] forKeyPath:@"mediaId"];
    [self setValue:[model valueForKeyPath:@"TYPE"] forKeyPath:@"TYPE"];

    [self setValue:[model valueForKeyPath:@"URL1"] forKeyPath:@"URL1"];
    [self setValue:[model valueForKeyPath:@"URL2"] forKeyPath:@"URL2"];
    [self setValue:[model valueForKeyPath:@"DATA"] forKeyPath:@"DATA"];
    [self setValue:[model valueForKeyPath:@"REFERENCE"] forKeyPath:@"REFERENCE"];

    return self;
}

- (NSMutableArray *) processArrayMediaModels:(NSArray*)model{
    NSMutableArray *jMEDIA = [[NSMutableArray alloc] init];

    for(NSDictionary *tMEDIA in [model objectAtIndex:0]){
        NSLog(@"MEDIA ITEM: %@", tMEDIA);
        if(tMEDIA == (id)[NSNull null]){
        }else{
            LBRESTAdapter *adapter = ((AppDelegate *)[[UIApplication sharedApplication] delegate]).adapter;
            MediaModelRepository *lbr = (MediaModelRepository*)[adapter repositoryWithModelName:@"Media" persisted:YES ];
            LBPersistedModel *xt  = (LBPersistedModel *)[lbr modelWithDictionary:tMEDIA];
            [jMEDIA addObject:[self transformMediaObject:[xt toDictionary]]];
        }
    }

    return jMEDIA;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions