brainray
brainray

Reputation: 12884

Using RLMArray without initialisation not possible?

I would like to collect some data in a method. The RLMArray will then be used as a one to many relationship e.g. menu.dishes

+ (RLMArray<Dish> *)parseDishesFromDictionary:(NSDictionary *)resultDictionary {

NSArray *menuDishes = (NSArray*)resultDictionary[@"menuDishes"];

RLMArray<Dish> *dishes;
for (NSDictionary *menuDishDictionary in menuDishes) {
    Dish *dish = [self getDishGetDishfromDictionary:menuDishDictionary];
    [dishes addObject:dish];
    }
return dishes;
}

The problem: the dishes array is always nil.

Is this really not possible, like the answer from this question implies?

Upvotes: 5

Views: 777

Answers (1)

jpsim
jpsim

Reputation: 14409

RLMArray is a container type that's mostly just useful for storing to-many relationships on Realm objects, and you don't get any of the collection optimizations until you've assigned it to an object, which is why Realm disallows direct initialization of RLMArray (+new and -init are listed as unavailable in the docs).

So your parseDishesFromDictionary method should return an NSArray<Dish *> that you can then add to your RLMArray property using -addObjects.

Upvotes: 6

Related Questions