Skip to content

Commit

Permalink
Added some macros to handle backwards, non-ARC compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
casademora committed Nov 15, 2011
1 parent e65b43a commit 3e0b36c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 53 deletions.
37 changes: 17 additions & 20 deletions Source/Categories/NSManagedObject+MagicalRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ + (NSArray *) MR_sortAscending:(BOOL)ascending attributes:(NSArray *)attributesT
{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:attributeName ascending:ascending];
[attributes addObject:sortDescriptor];
#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[sortDescriptor release];
#endif
MR_RELEASE(sortDescriptor);
}

return attributes;
Expand All @@ -150,10 +148,8 @@ + (NSFetchRequest *)MR_createFetchRequestInContext:(NSManagedObjectContext *)con
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[self MR_entityDescriptionInContext:context]];

#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[request autorelease];
#endif
MR_AUTORELEASE(request);

return request;
}

Expand Down Expand Up @@ -301,9 +297,7 @@ + (NSFetchRequest *) MR_requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)

NSSortDescriptor *sortBy = [[NSSortDescriptor alloc] initWithKey:sortTerm ascending:ascending];
[request setSortDescriptors:[NSArray arrayWithObject:sortBy]];
#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[sortBy autorelease];
#endif
MR_AUTORELEASE(sortBy);

return request;
}
Expand All @@ -321,12 +315,18 @@ + (NSFetchRequest *) MR_requestAllSortedBy:(NSString *)sortTerm ascending:(BOOL)
[request setPredicate:searchTerm];
[request setFetchBatchSize:[self MR_defaultBatchSize]];

NSSortDescriptor *sortBy = [[NSSortDescriptor alloc] initWithKey:sortTerm ascending:ascending];
[request setSortDescriptors:[NSArray arrayWithObject:sortBy]];
#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[sortBy release];
#endif

NSMutableArray* sortDescriptors = [[NSMutableArray alloc] init];
NSArray* sortKeys = [sortTerm componentsSeparatedByString:@","];
for (NSString* sortKey in sortKeys)
{
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:ascending];
[sortDescriptors addObject:sortDescriptor];
MR_AUTORELEASE(sortDescriptor);
}

[request setSortDescriptors:sortDescriptors];
MR_AUTORELEASE(sortDescriptors);

return request;
}

Expand Down Expand Up @@ -400,10 +400,7 @@ + (NSFetchedResultsController *) MR_fetchController:(NSFetchRequest *)request de
sectionNameKeyPath:groupKeyPath
cacheName:cacheName];
controller.delegate = delegate;

#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[controller autorelease];
#endif
MR_AUTORELEASE(controller);

return controller;
}
Expand Down
5 changes: 1 addition & 4 deletions Source/Categories/NSManagedObjectContext+MagicalRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ + (NSManagedObjectContext *) MR_contextWithStoreCoordinator:(NSPersistentStoreCo
ARLog(@"Creating MOContext %@", [NSThread isMainThread] ? @" *** On Main Thread ***" : @"");
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:coordinator];

#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[context autorelease];
#endif
MR_AUTORELEASE(context);
}
return context;
}
Expand Down
8 changes: 2 additions & 6 deletions Source/Categories/NSManagedObjectModel+MagicalRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ + (NSManagedObjectModel *) MR_mergedObjectModelFromMainBundle;
+ (NSManagedObjectModel *) MR_newManagedObjectModel
{
NSManagedObjectModel *model = [self MR_mergedObjectModelFromMainBundle];
#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[model autorelease];
#endif
MR_AUTORELEASE(model);
return model;
}

Expand Down Expand Up @@ -72,9 +70,7 @@ + (NSManagedObjectModel *) MR_newManagedObjectModelNamed:(NSString *)modelFileNa
+ (NSManagedObjectModel *) MR_managedObjectModelNamed:(NSString *)modelFileName
{
NSManagedObjectModel *model = [self MR_newManagedObjectModelNamed:modelFileName];
#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[model autorelease];
#endif
MR_AUTORELEASE(model);
return model;
}

Expand Down
5 changes: 1 addition & 4 deletions Source/Categories/NSPersistentStore+MagicalRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ + (NSURL *) MR_urlForStoreName:(NSString *)storeFileName
{
NSArray *paths = [NSArray arrayWithObjects:[self MR_applicationDocumentsDirectory], [self MR_applicationStorageDirectory], nil];
NSFileManager *fm = [[NSFileManager alloc] init];

#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[fm autorelease];
#endif
MR_AUTORELEASE(fm);

for (NSString *path in paths)
{
Expand Down
24 changes: 5 additions & 19 deletions Source/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ + (NSPersistentStoreCoordinator *) MR_coordinatorWithPersitentStore:(NSPersisten
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

[psc MR_setupSqliteStoreNamed:[persistentStore URL] withOptions:nil];

#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[psc autorelease];
#endif
MR_AUTORELEASE(psc);
return psc;
}

Expand All @@ -91,10 +88,7 @@ + (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

[psc MR_setupSqliteStoreNamed:storeFileName withOptions:options];

#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[psc autorelease];
#endif
MR_AUTORELEASE(psc);
return psc;
}

Expand Down Expand Up @@ -125,11 +119,8 @@ + (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNam
{
[coordinator performSelector:@selector(MR_setupAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
}
#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[coordinator autorelease];
#endif
MR_AUTORELEASE(coordinator);
return coordinator;

}

+ (NSPersistentStoreCoordinator *) MR_coordinatorWithInMemoryStore
Expand All @@ -138,12 +129,9 @@ + (NSPersistentStoreCoordinator *) MR_coordinatorWithInMemoryStore
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

[psc MR_addInMemoryStore];
MR_AUTORELEASE(coordinator);

#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[psc autorelease];
#endif
return psc;

}

- (NSPersistentStore *) MR_addInMemoryStore
Expand All @@ -164,9 +152,7 @@ - (NSPersistentStore *) MR_addInMemoryStore
+ (NSPersistentStoreCoordinator *) MR_newPersistentStoreCoordinator
{
NSPersistentStoreCoordinator *coordinator = [self MR_coordinatorWithSqliteStoreNamed:kMagicalRecordDefaultStoreFileName];
#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
[coordinator retain];
#endif
MR_RETAIN(coordinator);
return coordinator;

}
Expand Down
10 changes: 10 additions & 0 deletions Source/CoreData+MagicalRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

#import <CoreData/CoreData.h>

#ifndef NS_AUTOMATED_REFCOUNT_UNAVAILABLE
#define MR_RETAIN(xx) [xx retain];
#define MR_RELEASE(xx) [xx release];
#define MR_AUTORELEASE(xx) [xx autorelease];
#else
#define MR_RETAIN(xx) ((void)0)
#define MR_RELEASE(xx) ((void)0)
#define MR_AUTORELEASE(xx) ((void)0)
#endif

#ifdef MR_SHORTHAND
#import "MagicalRecordShorthand.h"
#endif
Expand Down

0 comments on commit 3e0b36c

Please sign in to comment.