3

I am facing the following problem..

I have a class Menu.h and Item.h. Menu is like a menu of a restaurant and has multiple categories (like appetizers, salads, etc) and each menu has multiple items associated. So Menu.h has an NSArray property called itemList. I am trying to automatically load these objects using Mantle.

Menu.h

@interface Menu : MTLModel <MTLJSONSerializing>
@property (nonatomic) NSArray *itemList;
@end

And

Menu.m

@implementation Menu
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
    // model_property_name : json_field_name
    return @{
             };
}

+ (NSValueTransformer *)itemListJSONTransformer {
    return [NSValueTransformer mtl_JSONArrayTransformerWithModelClass: Item.class];
}

- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError **)error {
    self = [super initWithDictionary:dictionaryValue error:error];
    if (self == nil) return nil;
    return self;
}

And

Item.m

- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError **)error {
    self = [super initWithDictionary:dictionaryValue error:error];
    if (self == nil) {
      //DO SOMETHING WITH SELF AND RETURN A NON NIL OBJECT
      return self;
    } 
    return self;
}

My question is the following: if itemList is null, i.e., from the server null response comes for itemList, and then I want to override the default initWithDictionary behavior to DO SOMETHING AND RETURN A NON NIL OBJECT from the constructor of Item.h how do I do it? The code is not reaching this constructor to my surprise, because it was null when Menu.h was being formed.. I did specify the (NSValueTransformer) as well.. Any leads ? Thanks!

Arpit Goel
  • 163
  • 1
  • 16
  • Can you add the code you're using to create these objects? – David Snabel-Caunt Dec 16 '14 at 11:10
  • Hi @DavidCaunt Thanks for looking into this. I make a call to the backend rest api to get a JSON dictionary called responseObject. Then I create the full-Menu with itemList and further links by this code.. `self.menuList = [MTLJSONAdapter modelsOfClass:[Menu class] fromJSONArray:responseObject error:nil];` Here menuList is the entire list of menu items for a restaurant (for this specific application) – Arpit Goel Dec 16 '14 at 11:11
  • The actual json is located here.. www.grubble.clipr.me/api/v1/restaurant/menu/?restaurant=2 – Arpit Goel Dec 16 '14 at 11:22
  • any one ? am having bunch of more questions about mantle, really thinking if I should continue using it or not.. – Arpit Goel Dec 16 '14 at 11:45

1 Answers1

3

If itemList is null in the JSON, Mantle won't call your transformer, so Item's initializer is never called.

You can specify a default by changing the Menu model like this:

- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError *__autoreleasing *)error {
    // Create itemListDefault value.  
    NSDictionary *defaults = @{
        @"itemList" : itemListDefault
    };
    dictionaryValue = [defaults mtl_dictionaryByAddingEntriesFromDictionary:dictionaryValue];
    return [super initWithDictionary:dictionaryValue error:error];
}
David Snabel-Caunt
  • 57,804
  • 13
  • 114
  • 132
  • Thanks David, that was pretty helpful! I have few more questions on Mantle, will try to work around them myself, else will ask them by Tonight again :) – Arpit Goel Dec 16 '14 at 20:15