iOS开发

iOS - objective-c中realm的对应关系

2021-01-24  本文已影响0人  翀鹰精灵

上一章节中我们学习了objective-c中realm的简单实用,例子中我们只涉及到一个模型的存储,但是很多实际项目开发中,我们一般会涉及到模型嵌套的关系。常见的模型关系分为:一对一关系、一对多关系、双向关系。我们一起来让我们来探索 Realm 在两个对象之间能够定义的各种关系。

步骤流程:

1. 一对一关系演示

01.jpg

准备工作:先定义一个Person和Dog的类
< Person 类>

#import <Realm/Realm.h>
#import "Dog.h"

NS_ASSUME_NONNULL_BEGIN

@interface Person : RLMObject
@property NSString *name;
@property int numberID;
@property Dog *dog;
@end

NS_ASSUME_NONNULL_END

-------------------------------------------------------------------------------
#import "Person.h"

@implementation Person
+ (NSString *)primaryKey {
    return @"numberID";
}
@end

<Dog类>

#import <Realm/Realm.h>
//#import "Person.h"

NS_ASSUME_NONNULL_BEGIN

@interface Dog : RLMObject

//@property Person *master;
@property NSString *name;
@property int numberID;

@end

NS_ASSUME_NONNULL_END

-------------------------------------------------------------------------------
#import "Dog.h"

@implementation Dog

@end

示例代码:

#pragma mark - <一对一关系>
- (void)testOneToOne {
    Person *p = [[Person alloc]init];
    p.numberID = 1;
    p.name = @"Allison";
    Dog *dog = [[Dog alloc]init];
    dog.name = @"小黑";
    p.dog = dog;
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    NSLog(@"数据库路径:%@",[[RLMRealmConfiguration defaultConfiguration]fileURL]);
    [realm transactionWithBlock:^{
            [realm addObject:p];
    }];
}

结果如下图所示:

02.png 03.png

2. 一对多关系演示

04.jpg

准备工作:
先定义一个Person和Dog的类,注意写法与一对一关系中有所不同,Dog类中多了个RLM_ARRAY_TYPE(Dog)的宏定义,是为了支持RLMArray<Dog *><Dog>语法。

示例代码:
< Person 类>

#import <Realm/Realm.h>
#import "Dog.h"

NS_ASSUME_NONNULL_BEGIN

@interface Person : RLMObject
@property NSString *name;
@property int numberID;
//@property Dog *dog;
@property RLMArray<Dog *><Dog> *dogs;

@end

NS_ASSUME_NONNULL_END

-------------------------------------------------------------------------------

#import "Person.h"

@implementation Person
+ (NSString *)primaryKey {
    return @"numberID";
}
@end

<Dog类>

#import <Realm/Realm.h>

NS_ASSUME_NONNULL_BEGIN

@interface Dog : RLMObject

@property NSString *name;
@property int numberID;

@end

//RLMArray<Dog *><Dog>
RLM_ARRAY_TYPE(Dog)

NS_ASSUME_NONNULL_END

-------------------------------------------------------------------------------

#import "Dog.h"

@implementation Dog

@end

示例代码:


#pragma mark -- <一对多关系>
- (void)testOneToMany {
    NSLog(@"数据库路径:%@",[[RLMRealmConfiguration defaultConfiguration]fileURL]);

    Person *p = [[Person alloc]init];
    p.numberID = 1;
    p.name = @"Allison";
    Dog *dog1 = [[Dog alloc]init];
    dog1.name = @"小黑1";
    Dog *dog2 = [[Dog alloc]init];
    dog2.name = @"小黑2";
    
    [p.dogs addObject:dog1];
    [p.dogs addObject:dog2];
    
    RLMRealm *realm = [RLMRealm defaultRealm];
    NSLog(@"数据库路径:%@",[[RLMRealmConfiguration defaultConfiguration]fileURL]);
    [realm transactionWithBlock:^{
            [realm addObject:p];
    }];
}

效果如图所示:

04.png 05.png

3.双向关系/(反向关系)演示
(如:人拥有狗,狗又有对应的主人)

006.jpg
准备工作:
先定义一个Person和Dog的类,注意写法与一对多关系中有所不同,Dog类中需要实现对应的linkingObjectsProperties映射关系。
< Person 类>
#import <Realm/Realm.h>
#import "Dog.h"

NS_ASSUME_NONNULL_BEGIN

@interface Person : RLMObject
@property NSString *name;
@property int numberID;
@property RLMArray<Dog *><Dog> *dogs;

@end

NS_ASSUME_NONNULL_END

-------------------------------------------------------------------------------

#import "Person.h"

@implementation Person
+ (NSString *)primaryKey {
    return @"numberID";
}
@end

<Dog类>

#import <Realm/Realm.h>

NS_ASSUME_NONNULL_BEGIN

@interface Dog : RLMObject

@property (readonly) RLMLinkingObjects *master;
@property NSString *name;
@property int numberID;

@end

//RLMArray<Dog *><Dog>
RLM_ARRAY_TYPE(Dog)

NS_ASSUME_NONNULL_END

-------------------------------------------------------------------------------

#import "Dog.h"

@implementation Dog

+ (NSDictionary<NSString *,RLMPropertyDescriptor *> *)linkingObjectsProperties {
    return  @{
        @"master":[RLMPropertyDescriptor descriptorWithClass:NSClassFromString(@"Person") propertyName:@"dogs"],
        
    };
}
@end

示例代码:

- (void)testManyToMany {
    NSLog(@"数据库路径:%@",[[RLMRealmConfiguration defaultConfiguration]fileURL]);

    Person *p = [[Person alloc]init];
    p.numberID = 1;
    p.name = @"Allison";

    Dog *dog1 = [[Dog alloc]init];
    dog1.numberID = 1;
    dog1.name = @"小黑1";


    Dog *dog2 = [[Dog alloc]init];
    dog2.numberID = 2;
    dog2.name = @"小黑2";


    [p.dogs addObject:dog1];
    [p.dogs addObject:dog2];

    RLMRealm *realm = [RLMRealm defaultRealm];
    NSLog(@"数据库路径:%@",[[RLMRealmConfiguration defaultConfiguration]fileURL]);
    [realm transactionWithBlock:^{
        [realm addObject:p];
    }];
}

如图所示:

07.png 08.png

从查询结果中我们也发现,master并没有存储,只是realm维护一种映射关系。

通过下面的查询语句,从Person中取出dogs数组中的第一个元素的master属性,打印结果如下:

 Person *person = [Person allObjects].firstObject;
  NSLog(@"=====%@",person.dogs.firstObject.master);

打印结果如图所示:

09.png
END.
上一篇下一篇

猜你喜欢

热点阅读