Simple use of Realm database in iOS development

Keywords: Database github iOS Attribute

1. installation

Download address of realm GitHub

If you use cocoapods, the sample statement is as follows
platform :ios, '8.0'
target 'Simona_Realm' do
pod 'Realm', '~> 3.0.0-beta.3'
end

2. Create a simple model, inherit from RLMObject, and all models stored in the database need to inherit from RLMObject
Person.h
#import <Realm/Realm.h>
RLM_ARRAY_TYPE(Person);
@interface Person : RLMObject

@property NSString *personId;
@property BOOL status;
@property NSString *sex;

@end
Person.m
#import "Person.h"

@implementation Person

// Set primary key to ensure data uniqueness
+ (NSString *)primaryKey {
    return @"personId";
}

// Set property not nil
+ (NSArray<NSString *> *)requiredProperties {
    return @[@"sex"];
}

// Set ignore property
+ (NSArray<NSString *> *)ignoredProperties {
    return @[];
}

// Set the default value. For non null properties, the default value is blank
+ (NSDictionary *)defaultPropertyValues {
    return @{@"sex":@""};
}

//Index attribute, mainly used for searching, searching according to gender
+ (NSArray<NSString *> *)indexedProperties {
    return @[@"sex",@"woman"];
}

@end

About RLMObject
1.Realm ignores the properties of OC (such as nonatomic, atomic, strong, retain, week, copy, etc.), so it can not write when declaring properties. These properties will remain in effect until they are written to the database.
2.Realm supports the following types: BOOL, NSInteger, long, double, CGFloat, NSString, NSDate, NSData, etc
3. The RLM ﹣ array ﹣ type (person) is defined to support the rlmaray property, which is equivalent to allowing the use of rlmaray < person > property. For example, @ property rlmaray < person * > < person > * personal can be used in other properties, which is equivalent to inheritance relationship (this article will not go into details)

3. use
<1.> storage
Simulated storage of 10000 pieces of false data 
RLMRealm *realm = [RLMRealm defaultRealm];
       [realm transactionWithBlock:^{
           for (int i = 0; i < 10000; i++) {
               Person *person = [[Person alloc]init];
               person.personId = [NSString stringWithFormat:@"Zhang%d",i];
               person.sex = @"Not scanned";
               person.status = YES;
               [realm addObject:person];
           }
    }];

There
[realm transactionWithBlock:^{ }];
Equate to
[realm beginWriteTransaction];
[realm commitWriteTransaction];
This should be done every time the database is opened and closed

<2.> delete
 RLMRealm *realm = [RLMRealm defaultRealm];
    [realm beginWriteTransaction];
    [realm deleteAllObjects];
    [realm commitWriteTransaction];
<3.> query
1.Query by predicate
// RLMResults are equivalent to the queried array
NSPredicate *pred = [NSPredicate predicateWithFormat:@"status = %ld", NO];
RLMResults *result = [Person objectsWithPredicate:pred];
Person *per = result[indexPath.row];

2.Query by criteria
RLMResults *result = [Person objectsWhere:@"status = 'NO' AND personId BEGINSWITH 'Zhang 1'"];
Person *per = result[indexPath.row];

3.Query all
RLMResults *persons = [Person allObjects];
Person *per = persons[indexPath.row];
<4.> update
1.Update a property value in data
NSPredicate *pred = [NSPredicate predicateWithFormat:@"personId = %@", strScan];
RLMResults *result = [Person objectsWithPredicate:pred];
    if (result.count != 0) {

        RLMRealm *r = [RLMRealm defaultRealm];
        [r beginWriteTransaction];
        //directly modify
        Person *person = result[0];
        person.sex = @"Has been scanned";
        person.status = NO;
        [r commitWriteTransaction];
}

2.Create a new value and update the database,createOrUpdateInRealm:Update insert with primary key

 NSPredicate *pred = [NSPredicate predicateWithFormat:@"personId = %@", strScan];
    RLMResults *result = [Person objectsWithPredicate:pred];
    if (result.count != 0) {

        Person *person = [[Person alloc]init];
        person.personId = @"Simona1";
        person.sex = @"woman";
        person.status = NO;
        RLMRealm *r = [RLMRealm defaultRealm];
        [r beginWriteTransaction];
        [Person createOrUpdateInRealm:r withValue:person];
        [r commitWriteTransaction];

Posted by RON_ron on Sat, 04 Jan 2020 22:59:09 -0800