Persistence scheme -Realm one: Basic use

What is a Realm

Realm is a cross-platform mobile database engine that supports iOS, OS X (Objective-C and Swift), and Android. The core data engine is built in C++. It is not an ORM built on top of SQLite, but a standalone database storage engine.

  • Realm Core database engine exploration
  • Using the tutorial
  • Realm Browser
  • Xcode plug-in

The advanced

Supported data types

Realm supports the following data types: BOOL, BOOL, int, NSInteger, long, long, float, double, NSString, NSDate, NSData, and NSNumber.

Realm does not support collection types. It can be serialized to NSData for storage and converted to RLMArray for storage.

Relationship between

Data relationships supported by Real

  • In a relationship
  • The more relationship
  • Inverse relationship

In a relationship

When an object holds another object, such as a person who has a pet 🐶.

The more relationship

//Dog.h @interface Dog : RLMObject @property int num; @property NSString *name; @end // This protocol enables typed collections. i.e.: // RLMArray<Dog *><Dog> // the RLM_ARRAY_TYPE macro creates a protocol that allows the use of the RLMArray<Dog> syntax. RLM_ARRAY_TYPE(Dog)Copy the code
//Person.h
#import "Dog.h"
@interface Person : RLMObject
@property int num;
@property NSString *name;
@property RLMArray<Dog *><Dog> *pets;
@end

// This protocol enables typed collections. i.e.:
// RLMArray<Person *><Person>
RLM_ARRAY_TYPE(Person)

Copy the code

Inverse relationship

People own dogs, and dogs have corresponding owners

//Person.h
#import "Dog.h"
@interface Person : RLMObject
@property int num;
@property NSString *name;
@property RLMArray<Dog *><Dog> *pets;
@end

// This protocol enables typed collections. i.e.:
// RLMArray<Person *><Person>
RLM_ARRAY_TYPE(Person)

Copy the code

Implement protocol methods that indicate link relationships

//Dog.h @interface Dog : RLMObject @property int num; @property NSString *name; @property (readonly) RLMLinkingObjects *master; @end // This protocol enables typed collections. i.e.: // RLMArray<Dog *><Dog> // the RLM_ARRAY_TYPE macro creates a protocol that allows the use of the RLMArray<Dog> syntax. RLM_ARRAY_TYPE(Dog) //Dog.m + (NSDictionary<NSString *,RLMPropertyDescriptor *> *)linkingObjectsProperties { return @{ @"master": [RLMPropertyDescriptor descriptorWithClass:NSClassFromString(@"Person") propertyName:@"pets"] }; }Copy the code

Nullable attribute & default value & ignore attribute

Can be an empty attributes

By default, the value of an attribute can be null. If you force a non-null attribute, you can use the following method: follow the protocol method, and if you assign nil again, an exception error is thrown.

+ (NSArray *)requiredProperties {
    return @[@"name"];
    }
Copy the code

Setting defaults

+ (NSDictionary *)defaultPropertyValues {
     return@ {@"name": @""};
    }
Copy the code

Ignore the attribute

For properties you don’t want to store, the + (NSArray *)ignoredProperties implementation can also use the ignore properties & read-only properties to create computational properties for storing and retrieving collections and UIImage objects

notice

Realm instances will send notifications to Realm instances on other threads every time a write transaction commits

Get Realm Notifications

The returned token must be strongly referenced

token = [realm addNotificationBlock:^(NSString *notification, RLMRealm * realm) {
}]
Copy the code

Remove the notification

[token stop]