1

How can I store array values (RLMArray) in Realm DB ?

My .h file below,

 @interface Hotlines : RLMObject
 @property (strong, nonatomic) NSString  *id;       
 @property (strong, nonatomic) NSString *department_name;
 @property (strong, nonatomic) NSString *flag;
 @property (strong,nonatomic) RLMArray<Numbers> *numbers;   
 @end

2 Answers 2

3

You need to create realm model for Numbers array:

#import <Realm/Realm.h>

// Define your models for Numbers array
@interface Numbers : RLMObject
@property NSInteger *num;
@property (strong, nonatomic) NSString *name;
@end
RLM_ARRAY_TYPE(Numbers) // define RLMArray< Numbers >


// Define your models for Numbers array
@interface Hotlines : RLMObject
@property (strong, nonatomic) NSString  *id;       
@property (strong, nonatomic) NSString *department_name;
@property (strong, nonatomic) NSString *flag;
@property (strong,nonatomic) RLMArray<Numbers> *numbers;   
@end

// Implementations
@implementation Numbers
@end // none needed

@implementation Hotlines
@end // none needed

For more information refer Realm Objective c

Update :

 RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
       Hotlines *obj = [[Hotlines alloc] init]; 
       obj.department_name = @"anyString";
        Numbers *number = [[Numbers alloc] init]
        number.num = 1;
      [obj.numbers addObject:number] 
      [realm addObject:obj];
}];

For multiple data :

[realm transactionWithBlock:^{
    Hotlines *obj = [[Hotlines alloc] init];
    obj.department_name = @"anyString";

    for (int i=0; 1< 10; i++) {
        Numbers *number = [[Numbers alloc] init]
        number.num = i;
        number.name = @"XYZ"
        [obj.numbers addObject:number]
    }
    [realm addObject:obj];
}];
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but I know that but I need to know how to save values for RLMArray<Numbers> *numbers: for example: Hotlines *obj = [[Hotlines alloc] init]; obj.department_name = @"anyString"; obj.numbers = ???? RLMRealm *realm = [RLMRealm defaultRealm]; [realm beginWriteTransaction]; [realm addObject:obj]; [realm commitWriteTransaction];
Numbers *number = [[Numbers alloc] init] number.num = @"1"; [obj.numbers addObject:number] for adding number in hotline model. You can use for loop to add multiple valuses
This answer is correct. One comment -- it is probably a better idea to store the number on Numbers as a NSInteger, double, or NSNumber * type property, rather than turning it into a string.
Unfortunately I got: 'Property 'numers' requires a protocol defining the contained type - example: RLMArray<Person>.'
@RodBaker If you reading data then you need to use RLMArray< Numbers > *number_array = obj.numbers;
0

Can be done with primitives if you really just want numbers:

@interface Hotlines : RLMObject
@property NSString *id;       
@property NSString *department_name;
@property NSString *flag;
@property RLMArray<NSNumber*><RLMFloat> *numbers;
@end

From the realm docs:

RLMArrays can store primitive values in lieu of Realm objects. In order to do so, constrain a RLMArray with one of the following protocols: RLMBool, RLMInt, RLMFloat, RLMDouble, RLMString, RLMData, or RLMDate

.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.