Load default array with Realm

85 views Asked by At

I want create a Model to load defualt an array with Realm (Objective C) to Tableview and then I can edit, update, delete. I read in https://realm.io/docs/objc/latest/, but i can't see that i need, please help me. Thanks

1

There are 1 answers

0
Nike Kov On

https://realm.io/docs/objc/latest/#models

1) Create class with code in .h

#import <Realm/Realm.h>

@interface MyModelFromArray : RLMObject
@property NSNumber<RLMInt> *id; //probably needed to faster indexing
@property NSString *name;
@property NSDate   *date;
@end

RLM_ARRAY_TYPE(MyModelFromArray)

2) .m file will look like

@implementation MyModelFromArray
+ (NSString *)primaryKey
{
    return @"id";
}
@end

3) Store it in realm

MyModelFromArray *model = [[MyModelFromArray alloc] init];
model.id = @1;
model.name = @"Hmmm"

RLMRealm *realm = [RLMRealm defaultRealm];
[realm transactionWithBlock:^{
  [realm addObject:model];
}];