Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
various changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Newberry committed Mar 30, 2011
1 parent 090cd01 commit f8b0fd7
Show file tree
Hide file tree
Showing 13 changed files with 9,835 additions and 215 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -2,5 +2,5 @@ build/*
.DS_Store
*.pbxuser
*.perspectivev3
*.xcodeproj/*
#*.xcodeproj/*
*.mode1v3
2 changes: 2 additions & 0 deletions ActiveManager.h
Expand Up @@ -20,6 +20,7 @@
BOOL _resetModel;

NSDateFormatter *_defaultDateParser;
NSNumberFormatter *_defaultNumberFormatter;

NSMutableDictionary *_entityDescriptions;
NSMutableDictionary *_modelProperties;
Expand All @@ -45,6 +46,7 @@
@property (nonatomic, assign) id connectionClass;
@property (nonatomic, assign) int logLevel;
@property (nonatomic, retain) NSDateFormatter *defaultDateParser;
@property (nonatomic, retain) NSNumberFormatter *defaultNumberFormatter;
@property (nonatomic, retain) NSMutableDictionary *entityDescriptions;
@property (nonatomic, retain) NSMutableDictionary *modelProperties;
@property (nonatomic, retain) NSMutableDictionary *modelRelationships;
Expand Down
60 changes: 41 additions & 19 deletions ActiveManager.m
Expand Up @@ -14,6 +14,7 @@
#define OR_CORE_DATE_STORE_NAME @"CoreDataStore.sqlite"
#define OR_CORE_DATE_BATCH_SIZE 25
#define kRKManagedObjectContextKey @"RKManagedObjectContext"
#define OR_CORE_DATA_MIGRATION_NEED @"coreDataMigrationNeeded"

static ActiveManager *_shared = nil;

Expand All @@ -27,6 +28,7 @@ @implementation ActiveManager
@synthesize connectionClass = _connectionClass;
@synthesize logLevel;
@synthesize defaultDateParser = _defaultDateParser;
@synthesize defaultNumberFormatter = _defaultNumberFormatter;
@synthesize entityDescriptions = _entityDescriptions;
@synthesize modelProperties = _modelProperties;
@synthesize modelRelationships = _modelRelationships;
Expand All @@ -37,7 +39,7 @@ @implementation ActiveManager
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;

+ (ActiveManager *) shared{

if(_shared == nil)
_shared = [[ActiveManager alloc] init];

Expand All @@ -59,6 +61,8 @@ - (id) init{
_defaultDateParser = [[NSDateFormatter alloc] init];
[_defaultDateParser setTimeZone:[NSTimeZone localTimeZone]];

_defaultNumberFormatter = [[NSNumberFormatter alloc] init];

self.entityDescriptions = [NSMutableDictionary dictionary];
self.modelProperties = [NSMutableDictionary dictionary];
self.modelRelationships = [NSMutableDictionary dictionary];
Expand Down Expand Up @@ -109,17 +113,29 @@ - (void) addRequest:(ActiveRequest *) request toQueue:(dispatch_queue_t)queue di

if([request.urlPath rangeOfString:@"http"].length == 0)
[self addBaseURL:request];

dispatch_async(queue, ^{

id <ActiveConnection> conn = [[[_connectionClass alloc] init] autorelease];
[conn setResponseDelegate:request.delegate];
[conn setDidFailBlock:didFailBlock];
[conn setDidFinishBlock:didFinishBlock];
[conn setDidParseObjectBlock:didParseObjectBlock];

[conn send:request];
});

if(dispatch_get_current_queue() == queue){

id <ActiveConnection> conn = [[_connectionClass alloc] init];
[conn setResponseDelegate:request.delegate];
[conn setDidFailBlock:didFailBlock];
[conn setDidFinishBlock:didFinishBlock];
[conn setDidParseObjectBlock:didParseObjectBlock];

[conn send:request];
[conn release];
}
else
dispatch_async(queue, ^{

id <ActiveConnection> conn = [[[_connectionClass alloc] init] autorelease];
[conn setResponseDelegate:request.delegate];
[conn setDidFailBlock:didFailBlock];
[conn setDidFinishBlock:didFinishBlock];
[conn setDidParseObjectBlock:didParseObjectBlock];

[conn send:request];
});
}

- (ActiveResult *) addSyncronousRequest:(ActiveRequest *)request{
Expand Down Expand Up @@ -184,7 +200,7 @@ - (NSData *) serializeObject:(id)object{
/* Core Data */

- (void) managedObjectContextDidSave:(NSNotification *)notification{
[self.managedObjectContext performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:)
withObject:notification
waitUntilDone:YES];
Expand All @@ -201,13 +217,16 @@ - (NSManagedObjectContext*) newManagedObjectContext{
[moc setPersistentStoreCoordinator:self.persistentStoreCoordinator];
[moc setUndoManager:nil];
[moc setMergePolicy:NSOverwriteMergePolicy];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:moc];

return moc;
}

- (NSManagedObjectContext*) managedObjectContext {

//Taken from RestKit
if ([NSThread isMainThread]) {
return _managedObjectContext;

Expand All @@ -221,10 +240,6 @@ - (NSManagedObjectContext*) managedObjectContext {
backgroundThreadContext = [self newManagedObjectContext];
[threadDictionary setObject:backgroundThreadContext forKey:kRKManagedObjectContextKey];
[backgroundThreadContext release];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:backgroundThreadContext];
}
return backgroundThreadContext;
}
Expand Down Expand Up @@ -276,8 +291,9 @@ - (NSPersistentStoreCoordinator*) persistentStoreCoordinator {

NSString* storePath = [self storePath];
NSURL *storeUrl = [self storeUrl];

NSError* error;

NSError* error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel: _managedObjectModel];

Expand All @@ -297,6 +313,11 @@ - (NSPersistentStoreCoordinator*) persistentStoreCoordinator {
_modelCreated = YES;
}
}

BOOL compatible = [_managedObjectModel isConfiguration:nil compatibleWithStoreMetadata:[NSPersistentStoreCoordinator metadataForPersistentStoreOfType:OR_CORE_DATE_STORE_TYPE URL:storeUrl error:&error]];

if(!compatible)
[[NSNotificationCenter defaultCenter] postNotificationName:OR_CORE_DATA_MIGRATION_NEED object:nil];

if (![_persistentStoreCoordinator
addPersistentStoreWithType: OR_CORE_DATE_STORE_TYPE
Expand Down Expand Up @@ -354,6 +375,7 @@ - (void)dealloc{
[_persistentStoreCoordinator release];

[_defaultDateParser release];
[_defaultNumberFormatter release];
[_entityDescriptions release];
[_modelProperties release];
[_modelRelationships release];
Expand Down
8 changes: 4 additions & 4 deletions ActiveRecord.h
Expand Up @@ -123,10 +123,10 @@ const typedef enum {
+ (NSString *) dateFormat;
- (NSString *) dateFormatPreprocessor:(NSString *) date;

- (void) didCreate:(id) parameters;
- (void) willCreate:(id) parameters;
- (void) didUpdate:(id) parameters;
- (void) willUpdate:(id) parameters;
- (void) didCreate:(id) parameters data:(NSDictionary *)data;
- (void) willCreate:(id) parameters data:(NSDictionary *)data;
- (void) didUpdate:(id) parameters data:(NSDictionary *)data;
- (void) willUpdate:(id) parameters data:(NSDictionary *)data;

+ (BOOL) remoteEnabled;
+ (BOOL) usesRootNode;
Expand Down
78 changes: 43 additions & 35 deletions ActiveRecord.m
Expand Up @@ -394,7 +394,7 @@ + (id) create:(id)parameters withOptions:(NSDictionary *)options {
}

[resource update:dict withOptions:options];
[resource willCreate:options];
[resource willCreate:options data:dict];

if ([[self class] activeManager].logLevel > 1) {
NSLog(@"Created new %@", self);
Expand All @@ -406,7 +406,7 @@ + (id) create:(id)parameters withOptions:(NSDictionary *)options {
if ([resource respondsToSelector:createdAtSel] && [resource valueForKey:[self createdAtField]] == nil)
[resource setValue:[NSDate date] forKey:[self createdAtField]];

[resource didCreate:options];
[resource didCreate:options data:dict];

return [resource autorelease];
}
Expand Down Expand Up @@ -476,8 +476,8 @@ - (id) update:(NSDictionary *) data withOptions:(NSDictionary *) options{
NSString *mappedKey = [[map allKeys] indexOfObject:key] == NSNotFound ? key : [map objectForKey:key];
[dict setObject:[data objectForKey:key] forKey:[mappedKey stringByReplacingOccurrencesOfString:@"-" withString:@"_"]];
}
[threadSafeSelf willUpdate:options];
[threadSafeSelf willUpdate:options data:dict];

for (NSString *field in [dict allKeys]) {

Expand Down Expand Up @@ -564,11 +564,10 @@ - (id) update:(NSDictionary *) data withOptions:(NSDictionary *) options{

if ([value isKindOfClass:[NSString class]]){

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:[[threadSafeSelf class] dateFormat]];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:[[threadSafeSelf class] dateFormat]];
[threadSafeSelf setValue:[formatter dateFromString:[threadSafeSelf dateFormatPreprocessor:value]] forKey:localField];
[formatter release];
[formatter release];
}

break;
Expand All @@ -593,10 +592,14 @@ - (id) update:(NSDictionary *) data withOptions:(NSDictionary *) options{
break;
case NSStringAttributeType:

if([value isKindOfClass:[NSString class]])
[value unescapeFromHTML];
if([value isKindOfClass:[NSString class]]){

[value unescapeFromHTML];
[threadSafeSelf setValue:value forKey:localField];
}
else
[threadSafeSelf setValue:[[[self class] activeManager].defaultNumberFormatter stringFromNumber:value] forKey:localField];

[threadSafeSelf setValue:value forKey:localField];
break;
}

Expand All @@ -605,7 +608,7 @@ - (id) update:(NSDictionary *) data withOptions:(NSDictionary *) options{
}
}

[threadSafeSelf didUpdate:options];
[threadSafeSelf didUpdate:options data:dict];

return threadSafeSelf;
}
Expand Down Expand Up @@ -765,6 +768,7 @@ - (void) fetchRelationship:(NSString *) relationship delegate:(id) delegate didF
- (void) fetchRelationship:(NSString *) relationship didFinishBlock:(void(^)(ActiveResult *result))didFinishBlock didFailBlock:(void(^)(ActiveResult *result))didFailBlock{

ActiveRequest *request = [self requestForFetch];
request.delegate = nil;

if(relationship)
request.urlPath = [self relationshipURL:relationship forAction:Read];
Expand Down Expand Up @@ -928,32 +932,36 @@ + (void) connectionDidFail:(ActiveResult *) result{
}

- (void) connectionDidFinish:(ActiveResult *) result{

NSString *relationship = [self relationshipForURLPath:result.urlPath];

ActiveRecord *threadSafeSelf = [self threadSafe];

if(relationship){

NSRelationshipDescription *destEntity = [[[threadSafeSelf class] relationshipsByName] objectForKey:relationship];
NSArray *propertyNames = [[[destEntity destinationEntity] propertiesByName] allKeys];

if(![propertyNames containsObject:[[threadSafeSelf class] localIDField]])
[threadSafeSelf performSelector:NSSelectorFromString($S(@"remove%@:", [relationship capitalizedString])) withObject:[self valueForKey:relationship]];

Class relatedClass = [threadSafeSelf classForRelationship:relationship];
[threadSafeSelf update:$D([result objects], relationship)];

// NSRelationshipDescription *destEntity = [[[threadSafeSelf class] relationshipsByName] objectForKey:relationship];
// NSArray *propertyNames = [[[destEntity destinationEntity] propertiesByName] allKeys];
//
// if(![propertyNames containsObject:[[threadSafeSelf class] localIDField]])
// [threadSafeSelf performSelector:NSSelectorFromString($S(@"remove%@:", [relationship capitalizedString])) withObject:[self valueForKey:relationship]];
//
// Class relatedClass = [threadSafeSelf classForRelationship:relationship];



NSMutableSet *objects = [NSMutableSet setWithCapacity:[result count]];

for(id object in result.objects){
ActiveRecord *builtObject = [relatedClass build:object];

if(builtObject)
[objects addObject:builtObject];
}

if(objects)
[threadSafeSelf performSelector:NSSelectorFromString($S(@"add%@:", [relationship capitalizedString])) withObject:objects];
// NSMutableSet *objects = [NSMutableSet setWithCapacity:[result count]];
//
// for(id object in result.objects){
// ActiveRecord *builtObject = [relatedClass build:object];
//
// if(builtObject)
// [objects addObject:builtObject];
// }
//
// //if(objects)
// //[threadSafeSelf performSelector:NSSelectorFromString($S(@"add%@:", [relationship capitalizedString])) withObject:objects];
}
else{

Expand Down Expand Up @@ -1022,13 +1030,13 @@ + (NSString *) rootNodeName{
return [[self entityName] lowercaseString];
}

- (void) didCreate:(id) parameters {}
- (void) didCreate:(id) parameters data:(NSDictionary *) data{}

- (void) willCreate:(id) parameters{}
- (void) willCreate:(id) parameters data:(NSDictionary *) data{}

- (void) didUpdate:(id) parameters {}
- (void) didUpdate:(id) parameters data:(NSDictionary *) data{}

- (void) willUpdate:(id) parameters{}
- (void) willUpdate:(id) parameters data:(NSDictionary *) data{}

+ (NSDictionary *) defaultCreateOptions { return nil; }

Expand Down
2 changes: 1 addition & 1 deletion ActiveSupportInflector/GTMNSString+HTML.m
Expand Up @@ -507,7 +507,7 @@ - (NSString *) unescapeFromHTML {
} else {
// "standard" sequences
for (unsigned i = 0; i < sizeof(gAsciiHTMLEscapeMap) / sizeof(HTMLEscapeMap); ++i) {
if ([escapeString isEqualToString:gAsciiHTMLEscapeMap[i].escapeSequence]) {
if ([escapeString rangeOfString:gAsciiHTMLEscapeMap[i].escapeSequence options:NSCaseInsensitiveSearch].location != NSNotFound){
[finalString replaceCharactersInRange:escapeRange withString:[NSString stringWithCharacters:&gAsciiHTMLEscapeMap[i].uchar length:1]];
break;
}
Expand Down
12 changes: 6 additions & 6 deletions NSManagedObjectContext+Additions.m
Expand Up @@ -13,10 +13,10 @@ @implementation NSManagedObjectContext (NSManagedObjectContext_Additions)

- (BOOL) save{

int insertedObjects = [[self insertedObjects] count];
int updatedObjects = [[self updatedObjects] count];
int deletedObjects = [[self deletedObjects] count];

int insertedObjectsCount = [[self insertedObjects] count];
int updatedObjectsCount = [[self updatedObjects] count];
int deletedObjectsCount = [[self deletedObjects] count];
NSDate *startTime = [NSDate date];

NSError *error;
Expand All @@ -29,14 +29,14 @@ - (BOOL) save{
}
}
else {
NSLog(@" %@", [error userInfo]);
NSLog(@" Error:%@", [error userInfo]);
}

return NO;
}

if([ActiveManager shared].logLevel > 0)
NSLog(@"Created: %i, Updated: %i, Deleted: %i, Time: %f seconds", insertedObjects, updatedObjects, deletedObjects, ([startTime timeIntervalSinceNow] *-1));
NSLog(@"Created: %i, Updated: %i, Deleted: %i, Time: %f seconds", insertedObjectsCount, updatedObjectsCount, deletedObjectsCount, ([startTime timeIntervalSinceNow] *-1));

return YES;
}
Expand Down

0 comments on commit f8b0fd7

Please sign in to comment.