Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikeabdullah/3181368 to your computer and use it in GitHub Desktop.
Save mikeabdullah/3181368 to your computer and use it in GitHub Desktop.
NSManagedObjectContext for iOS 4. Maybe?
/*
NSManagedObjectContextShim.h
NSManagedObjectContextShim
Created by Daniel Tull on 25.07.2012.
Copyright (c) 2012 Daniel Tull. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import <CoreData/CoreData.h>
@interface NSManagedObjectContext (Shim)
#if !(defined MAC_OS_X_VERSION_10_7) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7
enum {
NSConfinementConcurrencyType = 0x00,
NSPrivateQueueConcurrencyType = 0x01,
NSMainQueueConcurrencyType = 0x02
};
typedef NSUInteger NSManagedObjectContextConcurrencyType;
#endif
// The returned context is guaranteed to implement -performBlock: and -performBlockAndWait: on older OS releases that predate Core Data's own ability for this
+ (NSManagedObjectContext *)dct_contextWithConcurrencyType:(NSManagedObjectContextConcurrencyType)ct;
@end
/*
NSManagedObjectContextShim.m
NSManagedObjectContextShim
Created by Daniel Tull on 25.07.2012.
Copyright (c) 2012 Daniel Tull. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its contributors may be used
to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "NSManagedObjectContextShim.h"
@interface DCTThreadedManagedObjectContext : NSManagedObjectContext
{
@private
NSOperationQueue *_operationQueue;
}
@end
@implementation NSManagedObjectContext (Shim)
+ (NSManagedObjectContext *)dct_contextWithConcurrencyType:(NSManagedObjectContextConcurrencyType)ct;
{
if ([self instancesRespondToSelector:@selector(initWithConcurrencyType:)])
{
return [[[self alloc] initWithConcurrencyType:ct] autorelease];
}
else if (ct == NSConfinementConcurrencyType)
{
return [[[self alloc] init] autorelease];
}
else
{
return [[[DCTThreadedManagedObjectContext alloc] initWithConcurrencyType:ct] autorelease];
}
}
@end
@implementation DCTThreadedManagedObjectContext
- (id)initWithConcurrencyType:(NSManagedObjectContextConcurrencyType)ct;
{
// Create a queue matching the concurrency type
NSOperationQueue *queue;
switch (ct)
{
case NSPrivateQueueConcurrencyType:
{
queue = [NSOperationQueue new];
[queue setMaxConcurrentOperationCount:1];
break;
}
case NSMainQueueConcurrencyType:
{
// If called on main thread, must do work immediately to avoid deadlock
if ([NSThread isMainThread])
{
if (self = [self init])
{
_operationQueue = [[NSOperationQueue mainQueue] retain];
}
return self;
}
queue = [[NSOperationQueue mainQueue] retain];
break;
}
default:
return [self init];
}
// Bounce the initialisation over to correct queue so context knows which it's associated with
__block DCTThreadedManagedObjectContext *myself = self;
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
myself = [self init];
}];
[queue addOperation:blockOperation];
[blockOperation waitUntilFinished];
if ((self = myself))
{
_operationQueue = queue;
}
else
{
[queue release];
}
return self;
}
- (void)dealloc
{
[_operationQueue release];
[super dealloc];
}
- (void)performBlock:(void(^)(void))block
{
[_operationQueue addOperationWithBlock:block];
}
- (void)performBlockAndWait:(void(^)(void))block
{
NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:block];
[_operationQueue addOperation:blockOperation];
[blockOperation waitUntilFinished];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment