@chinese-ppmt
2017-11-08T14:00:45.000000Z
字数 2986
阅读 1584
Realm
2017-11-08 21:43:21 update
RLMLinkingObjects是一个自动更新(auto-updating)的集合(链接到它父对象)对象.(由它的父类RLMResults可以更容易明白,源码如下:)
@interface RLMLinkingObjects<RLMObjectType: RLMObject *> : RLMResults@end
源码如下:
//注意返回值字典的key和value值类型+ (NSDictionary<NSString *, RLMPropertyDescriptor *> *)linkingObjectsProperties;重写该方法来提供某个属性(Dog中的owners)链接到的父类模型(Person)的关系信息(必须用
RLMPropertyDescriptor来描述,aPerson有dogs,aDog有主人owners)。
有两个人
aPerson和bPerson,两条狗aDog和bDog。aDog属于aPerson和bPerson公有;bDog只属于bPerson。需求:现在要把
aPerson和bPerson存储到本地 (关系图如下:)。
#import "AppDelegate.h"#import <Realm/Realm.h>@interface Dog : RLMObject@property NSString *name;@property NSInteger age;@property (readonly) RLMLinkingObjects *owners;@endRLM_ARRAY_TYPE(Dog)@interface Person : RLMObject@property NSString *name;@property RLMArray<Dog> *dogs;@property NSNumber<RLMInt> *age;@end@implementation Dog+ (NSDictionary *)linkingObjectsProperties{return @{ @"owners": [RLMPropertyDescriptor descriptorWithClass:Person.class propertyName:@"dogs"] };}@end@implementation Person@end
@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];self.window.rootViewController = [[UIViewController alloc] init];[self.window makeKeyAndVisible];NSURL *url = [RLMRealmConfiguration defaultConfiguration].fileURL;//根据URL移除本地已经存储的realm数据库[[NSFileManager defaultManager] removeItemAtURL:url error:nil];//创建一个aDogDog *aDog = [[Dog alloc]init];aDog.name = @"aDogName";aDog.age = 5;//创建一个bDogDog *bDog = [[Dog alloc]init];bDog.name = @"bDogName";bDog.age = 6;//新建一个aPersonPerson *aPerson = [[Person alloc]init];aPerson.name = @"aPersonName";[aPerson.dogs addObject:aDog];//新建一个bPersonPerson *bPerson = [[Person alloc]init];bPerson.name = @"bPersonName";[bPerson.dogs addObjects:@[aDog,bDog]];//创建一个defaultRealm数据库RLMRealm *realm = [RLMRealm defaultRealm];//realm数据库的读写都是在事务里完成[realm transactionWithBlock:^{//存入方式一:aPerson会在存储的时候根据KVC赋值给新建的一个Person模型,并将新建的存入realm.(person指针变了)[Person createInDefaultRealmWithValue:aPerson];[Person createInDefaultRealmWithValue:bPerson];//存入方式二:不用新建(person指针不变)[realm addObject:aPerson];[realm addObject:bPerson];}];//从defaultRealm读取所有的Dog模型(realm保存的是Person模型,但是能读出所有的Dog模型,good,决定入坑)RLMResults *allDogs = [Dog allObjects];for (Dog *dog in allDogs) {NSArray *ownerNames = [dog.owners valueForKeyPath:@"name"];NSLog(@"%@ has %lu owners (%@)", dog.name, (unsigned long)ownerNames.count, ownerNames);}return YES;}@end

详细分析请看下面:
方式一有3个打印log:
createInDefaultRealmWithValue 详细说明
第1个 log出自
//aPerson中加入aDog,[aPerson.dogs addObject:aDog];[Person createInDefaultRealmWithValue:aPerson];第2、3个 log出自
//bPerson中依次加入aDog、bDog,[bPerson.dogs addObjects:@[aDog,bDog]];[Person createInDefaultRealmWithValue:bPerson];方式二有2个打印log:
addObject 详细说明
第1个 log出自
//全部([aPerson.dogs addObject:aDog];)[realm addObject:aPerson];//以及aDog部分([bPerson.dogs addObjects:@[aDog,bDog]];去掉bDog)[realm addObject:bPerson];第2个 log出自
//上面后一部分去掉aDog那部分