Skip to content

Instantly share code, notes, and snippets.

View quellish's full-sized avatar

Dan quellish

View GitHub Profile
@quellish
quellish / save.m
Created September 29, 2014 07:43
Save
[managedObjectContext performBlock:^{
NSError *error = nil;
if (![managedObjectContext save:&error]){
[self handleError:error];
}
}];
@quellish
quellish / allPeopleInManagedObjectContext.m
Created September 29, 2014 07:42
allPeopleInManagedObjectContext
- (void) allPeopleInManagedObjectContext:(NSManagedObjectContext *)managedObjectContext withCompletion:(void (^)(NSArray*, NSError*))completion {
[managedObjectContext performBlock:^{
NSError *error = nil;
NSArray *results = nil;
NSFetchRequest *fetchRequest = nil;
NSPredicate *predicate = nil;
NSEntityDescription *entity = nil;
fetchRequest = [[NSFetchRequest alloc] init];
entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:context];
@quellish
quellish / saveManagedObjectContext.m
Created September 29, 2014 07:45
Recursive save of managed object context
- (void) saveManagedObjectContext:(NSManagedObjectContext *)managedObjectContext withCompletion::(void (^)(BOOL, NSError *))completio{
managedObjectContext performBlock:^{
NSError *error = nil;
if (![managedObjectContext save:&error]){
completion(NO, error);
} else {
if ([managedObjectContext parentContext] != nil){
[self saveManagedObjectContext:[managedObjectContext parentContext] withCompletion:completion];
} else {
@quellish
quellish / fetchThingsWithCompletion.m
Created August 9, 2014 20:07
Pseudo-sample of a core data fetch with a completion block.
- (void) fetchThingsWithCompletion:(void (^)(NSArray *, NSError *))completion{
[context performBlock:^{
result = [context executeFetch:...
if (completion != nil){
completion(result, error);
}
}];
}
@quellish
quellish / JSONCoreData.m
Created September 21, 2014 08:47
Assigning JSON values to a Core Data Managed Object
NSEntityDescription *entityDescription = nil;
NSArray *attributeNames = nil;
NSDictionary *mappedValues = nil;
entityDescription = [managedObject entity];
attributeNames = [[entity attributesByName] allKeys];
mappedValues = [jsonObject dictionaryWithValuesForKeys:attributeKeys];
[managedObject setValuesForKeysWithDictionary:mappedValues];
@quellish
quellish / distanceToLocationfromLocation.m
Created February 1, 2016 20:26
NSExpression distanceToLocation:fromLocation:
expression = [NSExpression expressionForFunction:@"distanceToLocation:fromLocation:"
arguments:@[
[NSExpression expressionForConstantValue:to ],
[NSExpression expressionForConstantValue:from ]
]
];
result = (NSNumber *)[expression expressionValueWithObject:nil context:nil];
@quellish
quellish / predicatedistanceToLocationfromLocation.m
Created February 1, 2016 20:29
NSPredicate distanceToLocation:fromLocation
predicate = [NSPredicate predicateWithFormat:@"distanceToLocation:fromLocation:(%@, %@) =< %@",
toLocation,
fromLocation,
expectedDistance
];
@quellish
quellish / coredatajsonremap.m
Created September 21, 2014 08:50
Remapping JSON keys to Core Data attributes using the userInfo dictionary of the attribute entity description as defined in the Core Data Model Editor
entityUserInfo = [entity userInfo];
reKeyedValues = [[NSMutableDictionary alloc] initWithDictionary:jsonObject];
for (NSString *key in keyedValues){
if ([entityUserInfo valueForKey:key] != nil){
[reKeyedValues setValue:[keyedValues valueForKey:key] forKey:[entityUserInfo valueForKey:key]];
// Remove the original key
[reKeyedValues setValue:nil forKey:key];
}
}
@quellish
quellish / zomg.swift
Created January 27, 2016 01:04
zomg.swift
if (__dtrace_isenabled$http_cache$response_from_cache$v1() != 0){
__dtrace_probe$http_cache$response_host_uri_status$v1$63686172202a$63686172202a$696e74(host.UTF8String, path.UTF8String, cached);
}
@quellish
quellish / ProbedURLCache2.m
Created January 26, 2016 23:39
ProbedURLCache2.m
#import "ProbedURLCache.h"
#import "http_cache.h"
@implementation ProbedURLCache
- (nullable NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
NSCachedURLResponse *result = nil;
int cached = 0;
result = [super cachedResponseForRequest:request];
if (result != nil){
cached = 1;