ios 开发之没事来看看

iOS Realm 使用总结

2016-10-17  本文已影响959人  等这姑娘老在我心里

官方档说明:https://realm.io/cn/docs/objc/latest/
安装就按照文档上说的来就可以了
**可以下载个插件 来快速创建model 插件在压缩包里面 文档上也有说明 **

#import <Realm/Realm.h>
@interface Beacons: RLMObject

@property NSString *uuid;
@property NSString * major;
@property NSString *minor;
@end
RLM_ARRAY_TYPE(Beacons)

@interface Wifi : RLMObject

@property NSString *bssid;
@property NSString *ssidname;
@end
RLM_ARRAY_TYPE(Wifi)
@class Mall;
@interface Floor : RLMObject
@property NSString *name;
@property NSString *floor_key;
@property RLMArray<Beacons*><Beacons> *beacons;
@property RLMArray<Wifi*><Wifi> *wifi;
@end

RLM_ARRAY_TYPE(Floor)

@interface Mall : RLMObject
@property NSString *key;
@property NSString *latitude2;
@property NSString *longitude2;
@property RLMArray<Floor*><Floor>  *floors;
@end
RLM_ARRAY_TYPE(Mall)

@interface Building : RLMObject
@property NSString *user;
@property NSString *scanTime;
@property NSString *mall_key;
@property Mall *mall;
@property BOOL isSent;
@end
RLM_ARRAY_TYPE(Building)
//添加默认值
@implementation Mall
+ (NSDictionary *)defaultPropertyValues
{
    return @{@"latitude2":@"0.0",@"longitude2":@"0.0"};
}

操作方式

[realm beginWriteTransaction];
[realm commitWriteTransaction];

或者 block方式

[realm transactionWithBlock:^{
 
}];

储存


        RLMRealm *realm = [RLMRealm defaultRealm];
        Building *building = [[Building alloc]init];
        building.user = [AppVariables sharedInstance].user.username;
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"yyyy-MM-dd"];
        building.scanTime = [formatter stringFromDate:[NSDate date]];
        Mall *mall = [[Mall alloc]init];
        mall.key = mallkey.key;
        mall.longitude2 = [NSString stringWithFormat:@"%f",[AppVariables sharedInstance].dynamicLocation.longitude];
        mall.latitude2 = [NSString stringWithFormat:@"%f",[AppVariables sharedInstance].dynamicLocation.latitude];
        building.mall_key = mall.key;
        building.mall = mall;
        [realm transactionWithBlock:^{
            [realm addObject:building];
        }];

更新


RLMResults *results = [Building objectsWhere:@"mall_key = %@",self.mall_key];
        Building *building = results.firstObject;
        RLMResults *queryFloor = [building.mall.floors objectsWhere:@"name = %@",titleMessage];
        if (queryFloor.count == 0) {
            RLMRealm *realm = [RLMRealm defaultRealm];
            NSString *floor_key = [NSUUID UUID].UUIDString;
            [realm transactionWithBlock:^{
                Floor *floor = [[Floor alloc]init];
                floor.floor_key = floor_key;
                floor.name = titleMessage;
                [building.mall.floors addObject:floor];
            }];
        }

查询

RLMResults *results = [Building objectsWhere:@"mall_key = %@",self.mall_key];

查询所有

[Building allObjects]

删除

[realm beginWriteTransaction];
[realm deleteObject:cheeseBook];
[realm commitWriteTransaction];

或者

[realm transactionWithBlock:^{
 [realm deleteObject:cheeseBook];
}];

删除多条数据

 [realm beginWriteTransaction];
 [realm deleteObjects:[Building allObjects]];
 [realm commitWriteTransaction];

删除所有

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

最坑的地方

  RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    // 设置新的架构版本。这个版本号必须高于之前所用的版本号(如果您之前从未设置过架构版本,那么这个版本号设置为 0)
    config.schemaVersion = 1;
    
    // 设置闭包,这个闭包将会在打开低于上面所设置版本号的 Realm 数据库的时候被自动调用
    config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {
        // 目前我们还未进行数据迁移,因此 oldSchemaVersion == 0
        if (oldSchemaVersion < 1) {
            // 什么都不要做!Realm 会自行检测新增和需要移除的属性,然后自动更新硬盘上的数据库架构
        }
    };
    
    // 告诉 Realm 为默认的 Realm 数据库使用这个新的配置对象
    [RLMRealmConfiguration setDefaultConfiguration:config];
    
    // 现在我们已经告诉了 Realm 如何处理架构的变化,打开文件之后将会自动执行迁移
    [RLMRealm defaultRealm];
原模型为
@interface Person : RLMObject
@property NSString *firstName;
@property NSString *lastName;
@property int age;
@end


想变更为  把 firstName 和 lastName 合并在一起
@interface Person : RLMObject
@property NSString *fullName;
@property int age;
@end
  RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];

// 在 [AppDelegate didFinishLaunchingWithOptions:] 中进行配置RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];

  config.schemaVersion = 1;

  config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {

   // 目前我们还未进行数据迁移,因此 oldSchemaVersion == 0 
  if (oldSchemaVersion < 1) { 
    // enumerateObjects:block: 方法遍历了存储在 Realm 文件中的每一个“Person”对象

     [migration enumerateObjects:Person.className block:^(RLMObject *oldObject, RLMObject *newObject) {
     // 将名字进行合并,存放在 fullName 域中 
      newObject[@"fullName"] = [NSString stringWithFormat:@"%@ %@",oldObject[@"firstName"], oldObject[@"lastName"]];
 }]; 
}};

  [RLMRealmConfiguration setDefaultConfiguration:config];

有问题欢迎来评论区留言

上一篇 下一篇

猜你喜欢

热点阅读