Skip to content
This repository was archived by the owner on Jun 4, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions RHExtendedManagedObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// RHExtendedManagedObject.h
// SimplifiedCoreDataExample
//
// Created by David De Bels on 22/11/13.
//
//

#import "RHManagedObject.h"

@interface RHExtendedManagedObject : RHManagedObject
{
@protected

// Internal boolean to track whether or not to update the additionalData on save
BOOL _needsUpdate;

@private

NSMutableDictionary* _additionalProperties;
}

@property (nonatomic, retain) NSData * additionalData;

-(id)additionalPropertyForKey:(NSString*)key;

-(void)setAdditionalDateProperty:(NSDate*)date forKey:(NSString*)key;
-(void)setAdditionalNumberProperty:(NSNumber*)number forKey:(NSString*)key;
-(void)setAdditionalStringProperty:(NSString*)string forKey:(NSString*)key;

@end
120 changes: 120 additions & 0 deletions RHExtendedManagedObject.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//
// RHExtendedManagedObject.m
// SimplifiedCoreDataExample
//
// Created by David De Bels on 22/11/13.
//
//

#import "RHExtendedManagedObject.h"

static NSString* kRHRuntimePropertiesKey = @"kRHRuntimePropertiesKey";


@interface RHExtendedManagedObject ()

@property (nonatomic, readonly) NSMutableDictionary* additionalProperties;

@end


@implementation RHExtendedManagedObject

@dynamic additionalData;

#pragma mark - Get or set additional properties

-(NSMutableDictionary*)additionalProperties
{
if (!_additionalProperties)
{
_additionalProperties = [self readAdditionalData];

if (!_additionalProperties)
_additionalProperties = [[NSMutableDictionary alloc] init];
}

return _additionalProperties;
}

-(void)setAdditionalDateProperty:(NSDate*)date forKey:(NSString*)key
{
if (date == nil || [date isKindOfClass:[NSDate class]])
[self setAdditionalValue:date forKey:key];
}

-(void)setAdditionalNumberProperty:(NSNumber*)number forKey:(NSString*)key
{
if (number == nil || [number isKindOfClass:[NSNumber class]])
[self setAdditionalValue:number forKey:key];
}

-(void)setAdditionalStringProperty:(NSString*)string forKey:(NSString*)key
{
if (string == nil || [string isKindOfClass:[NSString class]])
[self setAdditionalValue:string forKey:key];
}

-(void)setAdditionalValue:(id)value forKey:(NSString*)key
{
if (!key)
return;

if (value)
[self.additionalProperties setObject:value forKey:key];
else
[self.additionalProperties removeObjectForKey:key];

_needsUpdate = YES;
}

-(id)additionalPropertyForKey:(NSString*)key
{
return [self.additionalProperties objectForKey:key];
}

-(void)willSave
{
[super willSave];
[self writeAdditionalData];
}



#pragma mark - Read or write the additionalData

-(NSMutableDictionary*)readAdditionalData
{
if ([self respondsToSelector:@selector(additionalData)])
{
NSDictionary* dataDictionary = nil;

NSData* data = [self performSelector:@selector(additionalData)];
if (data)
{
NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
dataDictionary = [unarchiver decodeObjectForKey:kRHRuntimePropertiesKey];
[unarchiver finishDecoding];
}

return [dataDictionary mutableCopy];
}

return nil;
}

-(void)writeAdditionalData
{
if (_needsUpdate && [self respondsToSelector:@selector(setAdditionalData:)])
{
NSMutableData* data = [[NSMutableData alloc] init];
NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:self.additionalProperties forKey:kRHRuntimePropertiesKey];
[archiver finishEncoding];

[self performSelector:@selector(setAdditionalData:) withObject:data];
_needsUpdate = NO;
}
}

@end
39 changes: 39 additions & 0 deletions RHManagedObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,40 @@ typedef void (^RHDidDeleteBlock)();
withLimit:(NSUInteger)limit
error:(NSError **)error;

+(NSArray *)fetchWithPredicate:(NSPredicate *)predicate
sortDescriptors:(NSArray *)descriptors
withLimit:(NSUInteger)limit
error:(NSError **)error;

+(void)fetchInBackgroundWithPredicate:(NSPredicate *)predicate
sortDescriptors:(NSArray *)descriptors
withLimit:(NSUInteger)limit
completion:(void (^)(NSArray* fetchedObjects, NSError* error))completion;

+(NSDictionary*)fetchAllAsDictionaryWithKeyProperty:(NSString*)keyProperty
error:(NSError **)error;

+(NSDictionary*)fetchAsDictionaryWithKeyProperty:(NSString*)keyProperty
withPredicate:(NSPredicate*)predicate
error:(NSError **)error;

+(NSDictionary*)fetchAsDictionaryWithKeyProperty:(NSString*)keyProperty
withPredicate:(NSPredicate*)predicate
withSortDescriptor:(NSSortDescriptor*)descriptor
error:(NSError **)error;

+(NSDictionary*)fetchAsDictionaryWithKeyProperty:(NSString*)keyProperty
withPredicate:(NSPredicate*)predicate
withSortDescriptors:(NSArray*)descriptors
withLimit:(NSUInteger)limit
error:(NSError **)error;

+(void)fetchInBackgroundAsDictionaryWithKeyProperty:(NSString*)keyProperty
withPredicate:(NSPredicate*)predicate
withSortDescriptors:(NSArray*)descriptors
withLimit:(NSUInteger)limit
completion:(void (^)(NSDictionary* fetchedObjects, NSError* error))completion;

+(NSUInteger)countWithError:(NSError **)error;

+(NSUInteger)countWithPredicate:(NSPredicate *)predicate error:(NSError **)error;
Expand Down Expand Up @@ -112,6 +146,11 @@ typedef void (^RHDidDeleteBlock)();
-(void)didUpdate;
-(void)didDelete;


+(NSArray*)arrayInCurrentThreadContext:(NSArray*)array;
+(NSDictionary*)dictionaryInCurrentThreadContext:(NSDictionary*)dictionary;
+(NSSet*)setInCurrentThreadContext:(NSSet*)set;

@end

@interface ImageToDataTransformer : NSValueTransformer
Expand Down
Loading