realm开始使用领域


备注

Realm是一种移动优先数据库解决方案,旨在通过使用延迟评估以及类型安全和实时更新模型对象来提供低内存消耗的快速查询。

Realm可用于多个平台,因此包含多个项目:

版本

平台发布日期
领域目标-C 2.4.4 2017年3月14日
境界斯威夫特 2.4.4 2017年3月14日
Realm Java 3.2.0 2017年5月16日

Java的

public class Dog extends RealmObject {
    public String name;
    public int age;
}

Dog dog = new Dog();
dog.name = "Rex";
dog.age = 1;

Realm realm = Realm.getDefaultInstance();
realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        realm.copyToRealmOrUpdate(dog);
    }
});

RealmResults<Dog> pups = realm.where(Dog.class)
                               .lessThan("age", 2)
                               .findAll();
 

Objective-C的

@interface Dog : RLMObject
@property NSString *name;
@property NSInteger age;
@end
@implementation Dog
@end

Dog *dog = [Dog new];
dog.name = @"Rex";
dog.age = 1;

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

RLMResults<Dog *> *allDogs = [Dog allObjects];
RLMResults<Dog *> *pups = [allDogs objectsWhere:@"age < 2"];
 

反应母语

class Dog {}

Dog.schema = {
    name: 'Dog',
    properties: {
        name: 'string',
        age: 'int',
    }
};

let realm = new Realm();
realm.write(() => {
    realm.create('Dog', {name: 'Rex', age: 1});
});

let pups = realm.objects('Dog').filtered('age > 2');
 

迅速

创建对象类:

class Dog: Object {
    dynamic var name = ""
    dynamic var age = 0
}   
 

分配对象的值:

let dog = Dog()
dog.name = "Rex"
dog.age = 1
 

保存对象:

let realm = try! Realm()
try! realm.write {
    realm.add(dog)
}
 

阅读对象:

let realm = try! Realm()
let pups = realm.objects(Dog.self)
 

过滤对象:

let realm = try! Realm()
let filteredPups = realm.objects(Dog.self).filter("age < 2")
 

计数对象:

let realm = try! Realm()
let pupsCount = realm.objects(Dog.self).count
 

Xamarin

public class Dog : RealmObject 
{
    public string Name { get; set; }
    public int Age { get; set; }
}

var realm = Realm.GetInstance();
realm.Write(() => 
{
    var mydog = realm.CreateObject<Dog>();
    mydog.Name = "Rex";
    mydog.Age = 1;
});

var pups = realm.All<Dog>().Where(d => d.Age < 2);