Skip to content

Instantly share code, notes, and snippets.

@beloso
Last active February 15, 2017 10:15
Show Gist options
  • Save beloso/c4241e9d5c7a208a35d8ea0a8adcdf72 to your computer and use it in GitHub Desktop.
Save beloso/c4241e9d5c7a208a35d8ea0a8adcdf72 to your computer and use it in GitHub Desktop.
Realm Utilities (Helping for crash log)
#import "RLMObject+MYUtils.h"
@interface RLMObject ()
+ (NSPredicate *)basePredicate;
@end
@implementation RLMObject (CYUtils)
+ (void)removeObjectsByIdIfNeeded:(NSArray *)ids
onDeleteBlock:(void (^)(RLMResults<RLMObject *> *rlmObject))onDeleteBlock
realm:(RLMRealm *)realm {
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"NOT(%K IN %@)",
[[self class] primaryKey],
ids];
RLMResults *result = [self objectsWithPredicate:predicate];
if (result.count) {
if (onDeleteBlock) {
onDeleteBlock(result);
}
[realm deleteObjects:result];
}
}
+ (void)addObjectsIfNeeded:(NSArray<RLMObject *> *)rlmArray
oldObjectsIds:(NSArray *)ids
realm:(RLMRealm *)realm
onAddBlock:(void (^)(RLMObject *rlmObject))onAddBlock {
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"NOT(%K IN %@)",
[[self class] primaryKey],
ids];
NSArray *result = [rlmArray filteredArrayUsingPredicate:predicate];
if (onAddBlock) {
for (RLMObject *rlmObject in result) {
onAddBlock(rlmObject);
}
}
if (result.count) {
[realm addOrUpdateObjectsFromArray:result];
}
}
+ (void)updateObjectsIfNeeded:(NSArray<RLMObject *> *)rlmObjects
oldObjectsIds:(NSArray *)ids
updateCondition:(BOOL (^)(RLMObject *oldRlmObject, RLMObject *newRlmObject))conditionBlock
realm:(RLMRealm *)realm {
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"%K IN %@",
[[self class] primaryKey],
ids];
NSArray *result = [rlmObjects filteredArrayUsingPredicate:predicate];
NSMutableArray *objectsToUpdate = [[NSMutableArray alloc] init];
for (RLMObject *newRlmObject in result) {
id rlmObjectId = [newRlmObject valueForKey:[[self class] primaryKey]];
RLMObject *oldRlmObject = [self objectForPrimaryKey:rlmObjectId];
BOOL shouldUpdate = YES;
if (conditionBlock) {
shouldUpdate = conditionBlock(oldRlmObject, newRlmObject);
}
if (shouldUpdate) {
[objectsToUpdate addObject:newRlmObject];
}
}
if (objectsToUpdate.count) {
[realm addOrUpdateObjectsFromArray:objectsToUpdate];
}
}
+ (void)performNeededChangesForObjects:(NSArray<RLMObject *> *)rlmObjects
onAddBlock:(void (^)(RLMObject *rlmObject))onAddBlock
updateCondition:(BOOL (^)(RLMObject *oldRlmObject, RLMObject *newRlmObject))conditionBlock
autoDeleteNotUpdatedValues:(BOOL)autoDelete
onDeleteBlock:(void (^)(RLMResults<RLMObject *> *rlmObject))onDeleteBlock
realm:(RLMRealm *)realm {
NSArray *oldIds = [[self allObjects] valueForKey:[self primaryKey]];
NSArray *newIds = nil;
if (autoDelete) {
newIds = [rlmObjects valueForKey:[self primaryKey]];
}
[realm beginWriteTransaction];
if (newIds) {
[self removeObjectsByIdIfNeeded:newIds
onDeleteBlock:onDeleteBlock
realm:realm];
}
[self addObjectsIfNeeded:rlmObjects
oldObjectsIds:oldIds
realm:realm
onAddBlock:onAddBlock];
[self updateObjectsIfNeeded:rlmObjects
oldObjectsIds:oldIds
updateCondition:conditionBlock
realm:realm];
[realm commitWriteTransaction];
}
+ (void)performNeededChangesForObjects:(NSArray<RLMObject *> *)rlmObjects
onAddBlock:(void (^)(RLMObject *rlmObject))onAddBlock
updateCondition:(BOOL (^)(RLMObject *oldRlmObject, RLMObject *newRlmObject))conditionBlock
deleteCondition:(NSPredicate *)deleteCondition
onDeleteBlock:(void (^)(RLMResults<RLMObject *> *rlmObject))onDeleteBlock
realm:(RLMRealm *)realm {
NSArray *oldIds = [[self allObjects] valueForKey:[self primaryKey]];
RLMResults *objectsToDelete = nil;
if (deleteCondition) {
objectsToDelete = [self objectsWithPredicate:deleteCondition];
}
[realm beginWriteTransaction];
if (objectsToDelete.count) {
if (onDeleteBlock) {
onDeleteBlock(objectsToDelete);
}
[realm deleteObjects:objectsToDelete];
}
[self addObjectsIfNeeded:rlmObjects
oldObjectsIds:oldIds
realm:realm
onAddBlock:onAddBlock];
[self updateObjectsIfNeeded:rlmObjects
oldObjectsIds:oldIds
updateCondition:conditionBlock
realm:realm];
[realm commitWriteTransaction];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment