YSRealmStore 0.11.0

YSRealmStore 0.11.0

测试已测试
语言语言 Objective-CObjective C
许可证 MIT
发布时间最后发布时间2017 年 12 月

Yu Sugawara维护。



YSRealmStore

Realm Cocoa 提供简单的封装。

功能

  • 支持 Realm Cocoa (3.0.x)
  • 异步/取消操作。

安装

pod 'YSRealmStore'

用途

初始化

RLMRealmConfiguration *configuration = [[RLMRealmConfiguration alloc] init];
configuration.path = [YSRealmStore realmPathWithFileName:@"twitter"];
    
configuration.objectClasses = @[[Tweet class],
                                [User class],
                                [Entities class],
                                [Url class],
                                [Mention class]];
    
configuration.schemaVersion = 2;
configuration.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
    /* Migration */
};
    
TwitterRealmStore *store =  [[TwitterRealmStore alloc] initWithConfiguration:configuration];

写入事务

事务

/* Sync */
[store writeTransactionWithWriteBlock:^(YSRealmWriteTransaction *transaction, RLMRealm *realm) {
    // Can be operated for the realm. (Main thread)
    [realm addOrUpdateObject:[[Tweet alloc] initWithObject:obj]];
}];

/* Async */
[store writeTransactionWithWriteBlock:^(YSRealmWriteTransaction *transaction, RLMRealm *realm) {
    // Can be operated for the realm. (Background thread)
    [realm addOrUpdateObject:[[Tweet alloc] initWithObject:obj]];
} completion:^(YSRealmStore *store, YSRealmWriteTransaction *transaction) {
        
}];

取消

YSRealmWriteTransaction *transaction = [store writeTransactionWithWriteBlock:writeBlock
                                                                  completion:completion];
[transaction cancel];

操作

添加

/* Sync */
[store writeObjectsWithObjectsBlock:^id(YSRealmOperation *operation, RLMRealm *realm) {
    // Can be operated for the realm. (Main thread)
    return [[Tweet alloc] initWithObject:tweetJsonObj];
}];

/* Async */
[store writeObjectsWithObjectsBlock:^id(YSRealmOperation *operation, RLMRealm *realm) {
    // Can be operated for the realm. (Background thread)
    return [[Tweet alloc] initWithObject:tweetJsonObj];
} completion:^(YSRealmStore *store, YSRealmOperation *operation) {

}];

删除

/* Sync */
[store deleteObjectsWithObjectsBlock:^id(YSRealmOperation *operation, RLMRealm *realm) {
    // Can be operated for the realm. (Main thread)
    return [Tweet allObjects];
}];

/* Async */
[store deleteObjectsWithObjectsBlock:^id(YSRealmOperation *operation, RLMRealm *realm) {
    // Can be operated for the realm. (Background thread)
    return [Tweet allObjects];
} completion:^(YSRealmStore *store, YSRealmOperation *operation) {

}];

检索

注意:检索到的对象需要主键。

/* Async */
[store fetchObjectsWithObjectsBlock:^id(YSRealmOperation *operation, RLMRealm *realm) {
    // Can be operated for the realm. (Background thread)
    RLMResults *tweets = [Tweet allObjects];
    return [tweets sortedResultsUsingProperty:@"id" ascending:YES];
} completion:^(YSRealmStore *store, YSRealmOperation *operation, RLMRealm *realm, RLMResults *results) {

}];

取消

YSRealmOperation *operation = [store writeObjectsWithObjectsBlock:objectsBlock
                                                       completion:completion];

[operation cancel];