Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Core data operation without semaphores #85

Conversation

lukestringer90
Copy link
Contributor

Removes use of semaphores which attempted to ensure an operation was completed on the same thread it was executing in. This has been removed as occasionally the semaphore was not signalled properly, and after some investigation nothing bad seems to happen from completing the operation on the main thread.

This is an alternative to @CaptainRedmuff's fix in pr #83 for issue #84. The benefit of the approach taken here is that the operation is still responsible for merging the contexts, and therefore can ensure it is only completed once the merge has finished. In #83 the operation completes and lets the context manger handle the merge, which could result in the changes not being merged into the context before another operation starts that is dependent on them being there.

Luke Stringer added 2 commits September 2, 2016 13:28
No longer uses semaphores. The operation merges the contexts then
completes itself while on the main thread. I haven't observed any problems
with this, but added a comment for future reference incase it does.
Deprecated the `completeOperationBySavingContext:` method in favour
of a simpler `completeAndSave`, that doesn't take a context and uses it's
own private context instead. `completeOperationBySavingContext:` is marked
as deprecated, and if called calls `completeAndSave` internally anyway.
self.managedObjectContextToMerge.shouldMergeOnSave = NO;

[self.managedObjectContextToMerge performBlockAndWait:^{
[self performWorkWithPrivateContext:self.managedObjectContextToMerge];
}];
}

- (BOOL)isConcurrent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if you should have both isConcurrent and isAsynchronous, but as long as they agree it should be okay.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

- (BOOL)isConcurrent
{
    return [self isAsynchronous];
}

- (BOOL)isAsynchronous
{
    return YES;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should work.

* Note: we are now on the main thread when we finish the opreation.
* Not sure if finishing the operation (thus triggering the KVO notifications)
* on a different thread (main thread) to the operation's execution causes
* problems. Seems fine but might not be ¯\_(ツ)_/¯
Copy link
Contributor

@blork blork Sep 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

¯_(ツ)_/¯

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants