Permalink
Browse files

Patch MagicalRecord to ensure we always use a MotionData::Context ins…

…tance.
  • Loading branch information...
1 parent 9a10308 commit a303512c8798568f402f33f0a449b4974ec694f8 @alloy committed Jul 7, 2012
View
@@ -1,5 +1,11 @@
module MotionData
+ class Context < NSManagedObjectContext
+ def self.default
+ self.MR_defaultContext
+ end
+ end
+
class EntityDescription < NSEntityDescription
def property(name, type, options={})
ad = AttributeDescription.withReflection(:name => name, :type => type, :options => options)
@@ -45,7 +51,9 @@ def self.defineVersion(version)
def setupCoreDataStackWithInMemoryStore
NSManagedObjectModel.MR_setDefaultManagedObjectModel(self)
- MagicalRecord.setupCoreDataStackWithInMemoryStore
+ coordinator = NSPersistentStoreCoordinator.MR_coordinatorWithInMemoryStore
+ NSPersistentStoreCoordinator.MR_setDefaultStoreCoordinator(coordinator)
+ Context.MR_initializeDefaultContextWithCoordinator(coordinator)
end
# TODO handle errors!
View
@@ -0,0 +1,41 @@
+describe "MotionData::Context" do
+
+ before do
+ MotionData::Schema.current.setupCoreDataStackWithInMemoryStore
+ end
+
+ after do
+ MagicalRecord.cleanUp
+ end
+
+ it "has a default MotionData::Context instance" do
+ MotionData::Context.default.should.be.instance_of MotionData::Context
+ end
+
+ # TODO currently these methods yield the default context, I'm pretty sure
+ # that's not supposed to be the case. Waiting to hear from Saul Mora.
+ #
+ describe "concerning saving on a background thread" do
+ # This is to ensure that we properly fix MagicalRecord that would always return a NSManagedObjectContext
+ # TODO create patch
+ it "yields a MotionData::Context instance" do
+ localContextClass = nil
+ MotionData::ManagedObject.saveInBackground do |localContext|
+ localContextClass = localContext.class
+ end
+ wait 0.1 do
+ localContextClass.should == MotionData::Context
+ end
+ end
+
+ it "is performed in a seperate context" do
+ localContextIsChildOfDefaultContext = nil
+ MotionData::ManagedObject.saveInBackground do |localContext|
+ localContextIsChildOfDefaultContext = (localContext.parentContext == NSManagedObjectContext.defaultContext)
+ end
+ wait 0.1 do
+ localContextIsChildOfDefaultContext.should == true
+ end
+ end
+ end
+end
@@ -36,20 +36,7 @@
end
end
- # TODO currently these methods yield the default context, I'm pretty sure
- # that's not supposed to be the case. Waiting to hear from Saul Mora.
- #
describe "concerning saving on a background thread" do
- it "is performed in a seperate context" do
- localContextIsChildOfDefaultContext = nil
- MotionData::ManagedObject.saveInBackground do |localContext|
- localContextIsChildOfDefaultContext = (localContext.parentContext == NSManagedObjectContext.defaultContext)
- end
- wait 0.1 do
- localContextIsChildOfDefaultContext.should == true
- end
- end
-
it "merges the changes into the default context afterwards" do
MotionData::ManagedObject.saveInBackground do
Author.new(:name => "Edgar Allan Poe")
@@ -115,7 +115,7 @@ + (NSManagedObjectContext *) MR_context;
+ (NSManagedObjectContext *) MR_contextWithParent:(NSManagedObjectContext *)parentContext;
{
- NSManagedObjectContext *context = [self MR_contextWithoutParent];
+ NSManagedObjectContext *context = [[parentContext class] MR_contextWithoutParent];
[context setParentContext:parentContext];
return context;
}
@@ -129,7 +129,7 @@ + (NSManagedObjectContext *) MR_newMainQueueContext;
+ (NSManagedObjectContext *) MR_contextThatPushesChangesToDefaultContext;
{
NSManagedObjectContext *defaultContext = [self MR_defaultContext];
- NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
+ NSManagedObjectContext *childContext = [[self alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[childContext setParentContext:defaultContext];
return childContext;
}

0 comments on commit a303512

Please sign in to comment.