mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-11 20:00:30 +01:00
NSManagedObjectContext Access #5
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @mrichtsfeld on GitHub (Jul 24, 2015).
Hi John,
I found you're library a few days ago and it is really nice. I especially like the concept of how managed object contexts interact with each other. The write and read only store are a really great way to properly use CoreData. +1 for that.
I already use it for my new project, but I found one downside and don't know yet how to overcome it. I use a background sync mechanism which is syncing data from a server to all clients via socket connection. Everytime data is received the sync library requests a managed object context where it applies the updates and saves them. Each context is only used once.
That is my situation so far. Since I don't want to make my sync library depending on CoreStore and want to keep a simple and basic NSMangedObjectContexts for that matter. You wrap up all contexts in your library to enforce correct usage, so unless I misunderstood something I'm pretty lost on how to use it with my sync mechanism.
I hope we can find a solution for this and I would like to contribute the solution to your code.
Thanks in advance.
@JohnEstropia commented on GitHub (Jul 25, 2015):
@mrichtsfeld, thanks for the feedback!
I understand your case and I am sure a lot of frameworks out there work with arbitrary
NSManagedObjectContexts, but I'm not quite sure how to provide a solution that wont turn the rest of CoreStore to an all-bets-are-off state. Here are some I can think of:BaseDataTransactioninstances expose their childNSManagedObjectContext.CoreStore.beginDetached(...), as it already bypasses the serialism of transactions and allows arbitrary saves.parentContextand all the ancestor contexts. If it doesn't, theListObservers andObjectObservers will never get notified of changes; worse, the changes won't be saved to the persistent store.DataStackexpose its root savingNSManagedObjectContext.save(), the second task's partial changes will be saved as well and you run a high risk of validation errors because of missing data, which in turn results in nothing from both tasks being saved.DataStackwhere the root context can be safely updated as a semi-transaction.performBlock()ordispatch_async()), then all the Cons in solution (2) apply.If it's possible, can you tell me what syncing library you are using? It might help if I consider how other libraries use and abuse their
NSManagedObjectContexts.In any case, all solutions above still requires you to design your app just to work around CoreStore's intricacies, so you may find it easier to just use another Core Data library that allows you to use contexts freely in the first place.
@mrichtsfeld commented on GitHub (Jul 25, 2015):
@JohnEstropia, thanks for you answer.
I understand your concerns, but maybe we find a solution. Your library is the only one I found in Cocoapods that enforces proper setup and usage of Coredata, that's why I wanna use it and push it so it can be used for even more use cases.
I think only 1 provides a solution. Let's create a separate transaction type derived from the detached transaction which exposes the
NSManagedObjectContextand can be used for cases like mine. Then it needs to be ensured that saving is done correctly and all contexts are updated correctly.In my case I would let the sync library ask for a temporary edit context which is requested on the sync queue. All changes are added and then the context is passed back to my main program still on the sync queue. Then the main program will save it properly by calling
.commit()so updates work correctly.What do you think about this?
For syncing I use two different libraries. For the first project I use Firebase with a completely custom sync. It basically receives changes in real time on a background queue which are save to Coredata.
My second project is pretty new and my business partner and me are currently writing a real time sync library which can sync any sql server database to any client. We make it Open Source as soon as it has received a state in which this makes sense.
@JohnEstropia commented on GitHub (Jul 26, 2015):
@mrichtsfeld
Can you check the commit I made here (
1c6085ad82) and see if this should be enough for your usage?I didn't see any need to make another kind of transaction. Since
DetachedDataTransactionalready provides the flexibility we need we just needed to expose its context (see newinternalContextproperty).@mrichtsfeld commented on GitHub (Jul 26, 2015):
@JohnEstropia
That looks great, but I'm not 100% confident about the Queue usage. So it is still possible to create a main queue concurrency and a private queue concurrency context, right? As far as I have seen the context determines automatically which type to use based on the queue.
Regarding the
internalContextthis is exactly what I need, thanks.Just one question aside. You are using new Swift 2.0 features in your GCDKit so I can only test with xcode 7, correct?
@JohnEstropia commented on GitHub (Jul 27, 2015):
@mrichtsfeld
I changed the detached transactions to use private concurrency context so you can use it on any dispatch queue, even the main thread. It was previously set to main queue concurrency which would have caused the main queue to lag if you call
performBlockon the context.CoreStore 1.0.0 is written in Swift 2.0/XCode 7. If you need it for Swift 1.2 you'll need to branch from git tag 0.2.0 and cherrypick the commit above.
@mrichtsfeld commented on GitHub (Jul 27, 2015):
@JohnEstropia
Thanks for clarification regarding the concurrency type and queues.
Regarding Swift 2.0 that is what I assumed and I have already used tag 0.2.0 in my xcode 6 project.
Thanks for the fast extension, this is much appreciated.
@JohnEstropia commented on GitHub (Jul 27, 2015):
I currently have no projects that heavily use the
DetachedDataTransactionso if you find any problems just post new issues! I'll close this one for now.@mrichtsfeld commented on GitHub (Jul 27, 2015):
Ok, great, thanks.