Skip to content

Instantly share code, notes, and snippets.

View AlanQuatermain's full-sized avatar

Jim Dovey AlanQuatermain

View GitHub Profile
@AlanQuatermain
AlanQuatermain / update_package_archive.rb
Created March 27, 2011 21:36
Updates root locations of .xcarchive bundles for given sub-packages or components within a PackageMaker document. Doesn't modify permissions, content inclusion, etc. Just the root content path, everywhere it lies.
#!/usr/bin/ruby
# == Usage
#
# Modifies an existing (and fully-set-up) PackageMaker document to point a package component
# at a new Xcode Archive root directory.
#
# This script is designed to work only with components based on Xcode 4 archives (folders ending
# in .xcarchive) containing a 'Products' subfolder which is used as the component's root folder.
#
@AlanQuatermain
AlanQuatermain / gist:957060
Created May 5, 2011 13:48
Keep your iOS network activity indicator under control, in a *thread-safe* way. Also shows a useful means of debugging it by logging backtraces for on/off calls.
//
// KBNetworkActivityIndicator.h
// Kobov3
//
// Created by Jim Dovey on 10-03-21.
// Copyright 2010 Kobo Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
@AlanQuatermain
AlanQuatermain / gist:977280
Created May 17, 2011 20:13
A fix for a bug in -countForFetchRequest: in iOS versions prior to 4.0. If you have multiple stores at the base of your CoreData stack, it would throw an exception. The fix below swaps out the faulty version with one which directs the request at the corre
@interface NSManagedObjectContext (AQFixedCountForFetchRequestOnIOS32)
@end
@implementation NSManagedObjectContext (AQFixedCountForFetchRequestOnIOS32)
+ (void) load
{
// +load gets called per-category, unlike +initialize
if ( [[[UIDevice currentDevice] systemVersion] compare: @"4.0" options: NSNumericSearch] == NSOrderedAscending )
{
@AlanQuatermain
AlanQuatermain / gist:1046799
Created June 25, 2011 19:22
A simple macro to create a non-retaining reference to an object for use within a block. For variable 'foo' will create non-retained variable '_block_foo' of type 'id'.
#if __has_feature(arr)
# if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
# define block_unretained(obj) id _block_##obj __weak = (obj)
# else
# define block_unretained(obj) id _block_##obj __unsafe_unretained = (obj)
#else
# define block_unretained(obj) id _block_##obj __block = (obj)
#endif
@AlanQuatermain
AlanQuatermain / gist:1063948
Created July 4, 2011 21:08
An example of how to use the prior gist
coverImage = [[KBImageCacheManager sharedManager] cachedImageForImageObject: imageObject type: imageType];
if ( coverImage == nil )
{
MAKE_WEAK_SELF();
_imageObserver = [[imageObject registerImageUpdateObserverUsingBlock: ^(UIImage *image, NSString *imageID, NSString *type) {
USE_WEAK_SELF();
self->_coverImageView.image = image;
[[KBImageCacheManager sharedManager] removeObserver: _imageObserver];
[_imageObserver release]; _imageObserver = nil;
}] retain];
@AlanQuatermain
AlanQuatermain / gist:1069966
Created July 7, 2011 16:53
I love the ObjC runtime sometimes…
+ (void) load
{
if ( dispatch_barrier_async == 0 )
return;
// switch in the dispatch_barrier_async() versions since they're available
Method m1 = class_getInstanceMethod(self, @selector(handleEventsForObserver:usingBlock:));
Method m2 = class_getInstanceMethod(self, @selector(_barrier_handleEventsForObserver:usingBlock:));
@AlanQuatermain
AlanQuatermain / KBMultiDispatchSource.m
Created July 7, 2011 17:44
As-yet untested code for a dispatch source wrapper which can fire multiple event/cancel callbacks
#import <Foundation/Foundation.h>
#import <dispatch/dispatch.h>
#import "MAZeroingWeakRef.h"
@interface KBMultiDispatchSource : NSObject
{
dispatch_source_t _dispatch;
dispatch_queue_t _queue;
dispatch_source_type_t _type;
CFMutableDictionaryRef _eventObservers;
@AlanQuatermain
AlanQuatermain / gist:1112224
Created July 28, 2011 18:44
The change that fixed a HORRENDOUS CoreData-related bug which we've been chasing today.
[managedObjectContext setMergePolicy: NSMergeByPropertyObjectTrumpMergePolicy];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
- StoreManagedObjectContextForCurrentThread( managedObjectContext ); // store in thread dictionary
+
+ if ( [NSThread isMainThread] )
+ StoreManagedObjectContextForCurrentThread(managedObjectContext); // store in thread dictionary
+ else
+ dispatch_sync(dispatch_get_main_queue(), ^{ StoreManagedObjectContextForCurrentThread(managedObjectContext); });
// set some flags in the store metadata
@AlanQuatermain
AlanQuatermain / gist:1120236
Created August 2, 2011 13:57
Sample code to illustrate different types of blocks— global. heap, and stack.
//
// main.m
// BlocksRuntime
//
// Created by Jim Dovey on 11-07-22.
// Copyright 2011 Jim Dovey. All rights reserved.
//
#import <Foundation/Foundation.h>
@AlanQuatermain
AlanQuatermain / gist:1189049
Created September 2, 2011 16:18
Now *that*'s a predicate…
+ (NSFetchRequest *) findPageNoteWithRangeRequestWithVariables: (NSDictionary *) variables context: (NSManagedObjectContext *) context
{
// template does something like the following:
/*
$pageLen = ($pageEnd - $pageStart);
$halfPageLen = $pageLen / 2;
$boundStart = $PAGE_START - $halfPageLen;
$boundEnd = $PAGE_END + $halfPageLen;
return "(contentItem.contentID == $CONTENT_ID) AND (chapterFileName == $CHAPTER_FILE_NAME) AND (pageStart BETWEEN {$boundStart, $PAGE_END}) AND (pageEnd BETWEEN {$PAGE_START, $boundEnd})";
*/