Realm Memo

Realm 0.91.5

Creating Realm Models

定義各個屬性欄位為 NSString/NSInteger/BOOL...
groups 是特有的 RLMArray<Group> 陣列

接著看狀況設定下面幾個 methods
primaryKey: 這是最基本的,通常會設 primary key
indexedProperties: 設定 index,可以指定多個 property
defaultPropertyValues: property 預設值

另外在 Group 裡,建立 inverse relationship to Person.groups
linkingPerson: 這在計算群組裡有多少人的時候很好用

@interface Person : RLMObject

@property NSString *sno;
@property NSString  *name;
@property NSInteger gender;
@property NSDate    *birthday;
@property NSString  *phone;
@property NSString  *address;
@property NSString  *phoneForHome;
@property NSString  *phoneForOffice;
@property NSString  *email;
@property NSString  *reference;
@property NSString  *company;
@property NSString  *economy;
@property BOOL      maritalStatus;
@property NSString  *children;
@property NSString  *total;
@property BOOL      otherFilter;
@property NSString  *otherFilterType;
@property NSString  *note;
@property RLMArray<Group> *groups;

@end

@implementation Person

+ (NSString *)primaryKey {
  return @"sno";
}

+ (NSArray *)indexedProperties {
  return @[@"name"];
}

+ (NSDictionary *)defaultPropertyValues {
  return @{
           @"sno":[NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]],
           @"gender":@0,
           @"birthday":[NSDate dateWithTimeIntervalSince1970:0],
           @"address":@"",
           @"phoneForHome":@"",
           @"phoneForOffice":@"",
           @"email":@"",
           @"reference":@"",
           @"company":@"",
           @"economy":@"",
           @"maritalStatus":@NO,
           @"children":@"",
           @"total":@"",
           @"otherFilter":@NO,
           @"otherFilterType":@"",
           @"note":@""
           };
}

@end

@interface Group : RLMObject

@property NSString *name;
@property (readonly) NSArray *linkingPerson;

@end
RLM_ARRAY_TYPE(Group)

@implementation Group

+ (NSString *)primaryKey {
  return @"name";
}

// Define "linkingPerson" as the inverse relationship to Person.groups
- (NSArray *)linkingPerson {
  return [self linkingObjectsOfClass:@"Person" forProperty:@"groups"];
}

@end

新增,有設 primaryKey 才可以用 createOrUpdateInDefaultRealmWith...

RLMRealm *realm = [RLMRealm defaultRealm];
Group *group = [Group new];
group.name = groupName;
[realm beginWriteTransaction];
[Group createOrUpdateInDefaultRealmWithObject:group];
[realm commitWriteTransaction];

刪除

[realm beginWriteTransaction];
[realm deleteObject:[_arrDataSource[indexPath.section] objectAtIndex:indexPath.row]];
[realm commitWriteTransaction];

刪除所有 objects

[realm beginWriteTransaction];
[realm deleteAllObjects];
[realm commitWriteTransaction];

Block

[realm transactionWithBlock:^{
    [realm deleteObject:[MyCustomRealmObject objectForPrimaryKey:@"1234567890"]];
}];

新增多筆資料

// creating _sortedArray
[_sortedArray addObject:@{
                 @"name":[self contactName:contact],
                 @"phone":[self contactPhonesFirstLabel:contact],
                 @"phoneForHome":[self contactPhones:contact],
                 @"email":[self contactEmails:contact],
                 @"company":contact.company.length > 1 ? contact.company : @"",
                 @"note":contact.note.length > 1 ? contact.note : @""
                 }];
[realm beginWriteTransaction];
for (NSDictionary *dic in _sortedArray) {
  [Person createInDefaultRealmWithObject:dic];
}
[realm commitWriteTransaction];

Query all objects

RLMResults *person = [Person allObjects];

查詢 name="groupA"

[Group objectsWhere:@"name=%@",@"groupA"];

Checking first object

[Group objectsWhere:@"name=%@",@"groupA"].firstObject;

不分大小寫

[Person objectsWhere:@"name contains[c] %@",searchText];

搜詢 Person.groups 裡有 currentGroup 的所有人

[[Person allObjects] objectsWhere:@"ANY groups = %@", _currentGroup];

currentGroup 裡有多少人(要設定 linkingPerson)

[[_currentGroup.linkingPerson valueForKeyPath:@"name"] count];

Notifications

realm 有更新的時候,送出通知,用來更新 reload tableView 很好用。

@property (nonatomic, strong) RLMNotificationToken *notification;
__weak typeof(self) weakSelf = self;
_notification = [RLMRealm.defaultRealm addNotificationBlock:^(NSString *note, RLMRealm *realm) {
    //...
  [weakSelf.tableView reloadData];
}];

remove notification

[RLMRealm.defaultRealm removeNotification:_notification];