Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RLMArray without initialisation not possible?

Tags:

ios

realm

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?

like image 607
brainray Avatar asked Nov 12 '15 14:11

brainray


1 Answers

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.

like image 178
jpsim Avatar answered Sep 26 '22 13:09

jpsim