-
-
Notifications
You must be signed in to change notification settings - Fork 69
Concurrency thread limitations in CoreDataStackFactory not necessary #77
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Thanks @wiedem ! 🎉 😄 Sounds like we can remove Would you like to submit a PR? |
You're welcome 😎
Well, I personally wouldn't remove the async method but rather make it the default and remove the synchronous one. Adding persistent stores to the psc asynchronously is a good practice. Besides what I just mentioned: have you ever considered providing a convenient method to listen for store events on the CoreDataStack? This listener closure would be called by the
I would if I would be 100% sure how I would want to exactly change the mentioned methods, besides simply removing the |
Ah good points. Only having I'll think about this a bit more and push some fixes! 😄 |
hey @wiedem - check the commit above for my solution here. 😄 let me know what you think! |
The implementation seems fine to me 😉 |
@jessesquires I found some time to change the method, you can take a look here What this change does is it synchronously creates the store coordinator and the mocs because this shouldn't be time critical tasks. The persistent store is on the other hand is added asynchronously because this is the potentially time consuming task that shouldn't block the main thread. The completion callback will be called once the store has been added, that's when the UI should refresh its content and should make write operations possible. Let me know if you want me to create a PR. I haven't changed the corresponding tests yet. |
Ah, that's interesting. I think this is a bit too confusing for clients though — having a completion block and a return. I think there should be 1 way to get the stack and have it be fully ready to use. 😄 |
You could of course simply remove the return value. As said, the advantage of the solution you see in my changes is that you can fully initialize your UI before your persistent stores are loaded since a valid CoreDataStack is already available. Once the stores are loaded / migrated your UI just needs to load the data to display. But I understand if you want to do it your way. |
The current implementations in the
createStackInBackground
andcreateStack
methods of theCoreDataStackFactory
have some technically unnecessary limitations regarding the calling thread (precondition
statements).Creating queue based contexts with the
NSPrivateQueueConcurrencyType
andNSMainQueueConcurrencyType
on any thread (including the main thread) is absolutely legit and valid.As described in the NSManagedObjectContext Class Reference only contexts using the deprecated confinement pattern must be created and used from a single thread. I.e. those contexts are no longer valid if the thread to which the context was bound during the init phase ends.
Queue based contexts on the other hand must not be used with anything else but the
performBlock
andperformBlockAndWait
which automatically ensures that the operations are run in a correct context (thread).That being said:
createStack
ofCoreDataStackFactory
is unnecessary. Calling this method asynchronously would be totally fine, bothbackgroundContext
and themainContext
would work as expected.createStackInBackground
ofCoreDataStackFactory
is also unnecessary.CoreDataFunctions
and theresetStack
have the same limitation.It does however make sense for some of the mentioned methods to be called asynchronously (or to be called on the main queue) even though there's no technical requirement to do so.
If the mentioned limitations are intended because it helps the UI to stay responsive then this should be at least documented. Otherwise it would be a good thing to give developers the freedom to use those methods on any queue they want for whatever reason there may be.
The text was updated successfully, but these errors were encountered: