Skip to content

Instantly share code, notes, and snippets.

View AlanQuatermain's full-sized avatar

Jim Dovey AlanQuatermain

View GitHub Profile
/*
* NSException+Backtrace.h
* Utilities
*
* Created by Jim Dovey on 8/7/2010.
*
* Copyright (c) 2010 Kobo, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@AlanQuatermain
AlanQuatermain / AQPerThreadManagedObjectContext.m
Created May 28, 2010 14:17
A set of simple wrapper functions to get per-thread NSManagedObjectContext instances. When creating main context (i.e. in app delegate) pass it to StoreManagedObjectContextForCurrentThread(). Then just call PerThreadManagedObjectContext() anywhere you nee
static NSString * const AQPerThreadManagedObjectContext = @"AQPerThreadManagedObjectContext";
void StoreManagedObjectContextForCurrentThread( NSManagedObjectContext * context )
{
[[[NSThread currentThread] threadDictionary] setObject: context forKey: AQPerThreadManagedObjectContext];
}
NSManagedObjectContext * PerThreadManagedObjectContext( void )
{
NSManagedObjectContext * result = [[[NSThread currentThread] threadDictionary] objectForKey: AQPerThreadManagedObjectContext];
@AlanQuatermain
AlanQuatermain / JavaScriptCore iPhone
Created May 27, 2010 19:38
This is based on SVN checkout of revision 60307
Index: JavaScriptCore/profiler/ProfilerServer.mm
===================================================================
--- JavaScriptCore/profiler/ProfilerServer.mm (revision 60307)
+++ JavaScriptCore/profiler/ProfilerServer.mm (working copy)
@@ -30,7 +30,7 @@
#import "JSRetainPtr.h"
#import <Foundation/Foundation.h>
-#if PLATFORM(IPHONE_SIMULATOR)
+#if !PLATFORM(IPHONE)
@AlanQuatermain
AlanQuatermain / smart_status.c
Created December 7, 2009 01:49
A simple C program to display SMART status for all supported disks.
/*
* smart_status.c
* SMARTStatus
*
* Created by Jim Dovey on 6/12/2009.
*
* Copyright (c) 2009 Jim Dovey
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
/*
* proplist.go
* PropertyLists
*
* Created by Jim Dovey on 17/11/2009.
*
* Copyright (c) 2009 Jim Dovey
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@AlanQuatermain
AlanQuatermain / insert-bsd-header-snippet
Last active September 1, 2020 21:48
A TextMate snippet to create a BSD License header for your source file.
/*
* $TM_FILENAME
* ${1:`echo "$TM_FILEPATH" | awk -F"/" '{x=NF-1}{print $x}'`}
*
* Created by `id -P | awk -F ":" '{ print $8 }'` on `date "+%d/%m/%Y"`.
*
* Copyright (c) `date +%Y` ${2:$TM_ORGANIZATION_NAME}
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
- (void) functionWhichAnnoyinglyBlocksExecution: (NSFileHandle *) annoyingFileHandle
{
dispatch_async(dispatch_get_global_queue(0,0), ^{
id result = [annoyingFileHandle someBlockingOp];
dispatch_async(dispatch_get_main_queue(), ^{
[myController displayResult: result];
});
});
}
@AlanQuatermain
AlanQuatermain / gist:185377
Created September 11, 2009 15:54
Example of modifying synchronous code to asynchronous with GCD.
// Before:
- (IBAction) optimizeDataObject: (MyDataObject *) obj
{
// going to start work, so show the user that something's happening
[self.progressIndicator startAnimating];
// these two are *usually* quick -- unless the user opens a 1GB file.
// because this is happening in an event handler, it'll stall the event loop, which
// causes the good ol' spinning beachball of death
[self pruneDuplicatesInDataObject: obj];
@AlanQuatermain
AlanQuatermain / gist:119712
Created May 29, 2009 01:19
A lockless way of setting a lazily-initialized static global value. It works by using a CompareAndSwap atomic operation with a memory barrier to sync threads' instruction & data caches. If another thread sets the value since we looked at __staticVar, Comp
#import <libkern/OSAtomic.h>
@implementation SomeClass
+ (id) someStaticValueComputedOnFirstAccess
{
static volatile id __staticVar = nil;
if ( __staticVar == nil )
{
id var = [[Something alloc] init];
@AlanQuatermain
AlanQuatermain / Memory.c
Created May 15, 2009 16:05
Utilities for getting memory allocation & free amounts (works on Mac & iPhone).
/*
* Memory.c
* Outpost
*
* Created by Jim Dovey on 23/04/09.
* Copyright 2009 Morfunk, LLC. All rights reserved.
*
* Copyright (c) 2009, Jim Dovey
* All rights reserved.
*