Skip to content

Instantly share code, notes, and snippets.

View mikeabdullah's full-sized avatar

Mike Abdullah mikeabdullah

View GitHub Profile
@mikeabdullah
mikeabdullah / gist:4285229
Created December 14, 2012 12:43
Guaranteeing a directory URL
- (NSURL *)makeURLWithDirectoryPath:(NSURL *)url;
{
return [url URLByAppendingPathComponent:@""];
}
@mikeabdullah
mikeabdullah / gist:4740463
Last active January 6, 2024 07:31
Atomic file copying
- (BOOL)atomicCopyItemAtURL:(NSURL *)sourceURL
toURL:(NSURL *)destinationURL
error:(NSError **)outError
{
NSFileManager *manager = [NSFileManager defaultManager];
// First copy into a temporary location where failure doesn't matter
NSURL *tempDir = [manager URLForDirectory:NSItemReplacementDirectory
inDomain:NSUserDomainMask
appropriateForURL:destinationURL
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
BOOL inMultipleSelectionMode = (self.tableView.editing ?
self.tableView.allowsMultipleSelectionDuringEditing :
self.tableView.allowsMultipleSelection);
NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
if (selectedRowIndexPath && !inMultipleSelectionMode) {
@mikeabdullah
mikeabdullah / gist:867284
Created March 12, 2011 14:41
Apple's recommended way to disable undo registration for NSManagedObjectContext
[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] disableUndoRegistration];
// Make your special changes to the managed object
[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] enableUndoRegistration];
@mikeabdullah
mikeabdullah / gist:4147871
Created November 26, 2012 12:06
Recovering from "Dangling reference to an invalid object." error
- (BOOL)attemptRecoveryFromPossibleCascadeDeleteRuleFailureError:(NSError *)error;
{
// Make sure it's an unknown validation error for a managed object
if ([error code] != NSManagedObjectValidationError) return NO;
if (![[error domain] isEqualToString:NSCocoaErrorDomain]) return NO;
NSManagedObject *object = [[error userInfo] objectForKey:NSValidationObjectErrorKey];
if (![object isKindOfClass:[NSManagedObject class]]) return NO;
NSString *key = [[error userInfo] objectForKey:NSValidationKeyErrorKey];
@mikeabdullah
mikeabdullah / gist:b6bfa44aa2f1b9f1fdf6
Created November 5, 2014 12:28
UITableViewCell Right Detail layout workaround
- (void)layoutSubviews {
if (self.detailTextLabel.text.length) {
[self.contentView addSubview:self.detailTextLabel];
}
[super layoutSubviews];
}
@mikeabdullah
mikeabdullah / gist:cc7bcb8a352a7ece98da
Last active January 5, 2017 08:41
Decoding URL query items for schemes where + is to be treated as a space
NSURLComponents *components = [NSURLComponents componentsWithString:input];
// Re-encode + symbols so NSURLQueryItem recognises them as spaces
components.percentEncodedQuery = [components.percentEncodedQuery
stringByReplacingOccurrencesOfString:@"+"
withString:@"%20"];
NSArray *items = components.queryItems;
// Enumerate and handle the items
@mikeabdullah
mikeabdullah / gist:faa6fd7a75c04e7f9f9c
Last active September 2, 2016 13:51
Safer merge from saved context
- (void)mergeChangesFromSaveNotification:(NSNotification *)notification
intoContext:(NSManagedObjectContext *)context {
// NSManagedObjectContext's merge routine ignores updated objects which aren't
// currently faulted in. To force it to notify interested clients that such
// objects have been refreshed (e.g. NSFetchedResultsController) we need to
// force them to be faulted in ahead of the merge
NSSet *updated = [notification.userInfo objectForKey:NSUpdatedObjectsKey];
for (NSManagedObject *anObject in updated) {
@mikeabdullah
mikeabdullah / gist:866873
Created March 12, 2011 00:47
How to make a managed object complain if it's being turned into a fault while KVO observers are still registered with it.
- (void)willTurnIntoFault;
{
[super willTurnIntoFault];
if ([self observationInfo])
{
NSLog(@"%@ has observers:\n%@", [self objectID], [self observationInfo]);
}
}
@mikeabdullah
mikeabdullah / gist:8371693
Created January 11, 2014 14:41
Creating a URL ending in an invalid character
NSURL *url = [NSURL URLWithString:@"http://example.com/path "];