SMWRealm 使在不同线程中创建、读取、更新和删除 RLMObjects 更加容易。只需在不同的线程之间传递 SMWRealmKey 对象,并使用其方法与其 RLMObject 通信。这简化了使用 Realm 的多线程思想。
要运行示例项目,首先克隆仓库,然后从 Example 目录运行 pod install
。
SMWRealm 需要 iOS 7.0 及以上或 OS X 10.9 及以上。
SMWRealm 还需要第三方开源库 Realm
要运行示例项目,运行 git clone https://github.com/meech-ward/SMWRealm.git
然后打开 Example 目录并运行 pod install
现在您可以在 Example/SMWRealm.xcworkspace 中打开并运行项目。
SMWRealm 通过 CocoaPods 提供使用。要安装它,只需将以下行添加到您的 Podfile 中:
pod "SMWRealm"
Sam Meech-Ward,[email protected]
SMWRealm 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。
使用 SMWRealmKey 的任何 RLMObject 必须有一个有效的主键。
导入 SMWRealm 头文件
#import <SMWRealm/SMWRealm.h>
像往常一样设置您的 RLMObject。
Person *person = [[Person alloc] init];
person.firstName = @"Sam";
person.lastName = @"Meech-Ward";
然后使用 SMWRealmKey 对象将其保存到 realm
RLMRealm *realm = [RLMRealm defaultRealm];
SMWRealmKey<Person *> *personKey = [SMWRealmKey createOrUpdateObject:person inRealm:realm];
现在您可以在不同的线程之间传递此 SMWRealmKey 对象,并使用其方法读取和更新 RLMObject。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[personKey updateRealmObject:^(SMWRealmPerson *object, RLMRealm *realm) {
object.firstName = @"New First Name";
object.lastName = @"New Last Name";
}];
});
- (instancetype)initWithRealmObject:(RLMObject *)realmObject;
- (void)readRealmObject:(void(^)(RLMObjectType _Nullable object))block;
- (void)updateRealmObject:(void(^)(RLMObjectType _Nullable object, RLMRealm * _Nullable realm))block;
- (void)deleteRealmObject;
- (BOOL)isEqualToKey:(nullable id)object;
在删除相关对象时,使用更新功能很方便
[key updateRealmObject:^(Person * _Nullable object, RLMRealm * _Nullable realm) {
Dog *dog = person.dog;
[realm deleteObjects:@[person, dog]];
}];