SMWRealm makes it even easier to create, read, update, and delete RLMObjects from different threads. Simply pass around the SMWRealmKey object betweeen different threads, and use its methods to communicate with its RLMObject. This takes the thinking out of mulithreading with Realm.
To run the example project, clone the repo, and run pod install
from the Example directory first.
SMWRealm requires iOS 7.0 and above or OS X 10.9 and above.
SMWRealm also requires the thirdy-party open source library Realm
To run the example project run git clone https://github.com/meech-ward/SMWRealm.git
Then open the Example directory and run pod install
Now you can open and run the project in Example/SMWRealm.xcworkspace.
SMWRealm is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "SMWRealm"
Sam Meech-Ward, sam@meech-ward.com
SMWRealm is available under the MIT license. See the LICENSE file for more info.
Any RLMObject that uses the SMWRealmKey must have a valid primary key.
Import the SMWRealm header
#import <SMWRealm/SMWRealm.h>
Setup your RLMObject as normal.
Person *person = [[Person alloc] init];
person.firstName = @"Sam";
person.lastName = @"Meech-Ward";
Then save to a realm using the SMWRealmKey object
RLMRealm *realm = [RLMRealm defaultRealm];
SMWRealmKey<Person *> *personKey = [SMWRealmKey createOrUpdateObject:person inRealm:realm];
Now you can pass this SMWRealmKey object around different threads and use its methods to read and update the 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;
When deleting related objects, it's convenient to use the update function
[key updateRealmObject:^(Person * _Nullable object, RLMRealm * _Nullable realm) {
Dog *dog = person.dog;
[realm deleteObjects:@[person, dog]];
}];