mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-14 07:33:28 +01:00
Noob Question : Access to default context and NSFetchedResultsController #62
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 @wm-j-ray on GitHub (May 27, 2016).
John,
Just getting started with CoreStore, so...
If I 'needed' to use another complementary framework that needs a NSFetchedResultsController and a context for reading//writing, what is the safest way to create/access them without breaking a CoreStore dataStack set up like so:
And the stack is used from an interactor like structure using something like this:
I 'think' what I'm looking for is just a way to access the correct context to used for the NSFetchedResultsController (yours ideally, so I can use CoreStore fetching and queries) and the best way to deal with CoreStore transactions. If I can't access your NSFetchedController I would create one. Monitors and Observers, with my level of knowledge about CoreStore, seem a little beyond me.
And what are the pitfalls and downside to doing something like this?
The library is https://github.com/DenHeadless/DTTableViewManager by the way (which lightens up tableViews by using a manager (https://github.com/DenHeadless/DTModelStorage) to bind the model against tableViewCells), and I envision using it primarily for reads and using the CoreStore functionality for hopefully all of the writes.
Basically, I would really like to somehow wire CoreStore and DTTableViewManager together.
I want to make sure I'm aware of all of the potential issues and pick the best approach before I go any further.
Thanx in Advance
@JohnEstropia commented on GitHub (May 27, 2016):
The only NSManagedObjectContext exposed by CoreStore is the context held by
UnsafeDataTransactions.See the documentation on
UnsafeDataTransaction.internalContext: https://github.com/JohnEstropia/CoreStore/blob/master/CoreStore/Saving%20and%20Processing/UnsafeDataTransaction.swift#L153@JohnEstropia commented on GitHub (May 27, 2016):
Also, note that this means the objects you are working on are not from the main context. Don't mix objects fetched from the
DataStackto the objects fetched from the transaction@JohnEstropia commented on GitHub (May 27, 2016):
If all you need is an
NSFetchedResultsControllerinstance, CoreStore can also create one for you. SeeNSFetchedResultsController+Convenience.swift
@wm-j-ray commented on GitHub (May 27, 2016):
Thanks for the help. Let me make sure I understand...
So I can call NSFetchedResultsController.createForStack I would be using the correct context. I could use that NSFetchedResultsController to drive the tableView. Can you give me an example of what that call would look like using my dataStack? (lame, I know, but thanks in advance)
And, if I update I wouldn't be able to use the default CoreStore methods even though I'm in the proper context but rather I would have to do a save() manually or an UnsafeDataTransaction commit.
@JohnEstropia commented on GitHub (May 27, 2016):
Let me push an update so the API call is simpler. Once it's up you should be able to call with
If I understand how DTTableViewManager works, it's only for displaying data, correct? If so then the NSFetchedResultsController created with CoreStore should notify DTTableViewManager without any issues, and you can still make updates using any of CoreStore's transactions (not just UnsafeDataTransactions).
@JohnEstropia commented on GitHub (May 27, 2016):
Check out version 1.6.8. Renamed
createForStack()tocreateFor()and now accepts eitherDataStackorUnsafeDataTransaction@wm-j-ray commented on GitHub (May 27, 2016):
Excellent. Thanks so much.
On to the next.
@wm-j-ray commented on GitHub (May 27, 2016):
John,
I pulled down 1.6.8 and I must be an idiot because I can't get the stub you provided to work:
)
How do the optional parameters work? Why can't I do this?
I get a missing fetchClauses in call error.
I've also tried this:
I'm not sure what I'm doing incorrectly.
@JohnEstropia commented on GitHub (May 28, 2016):
Sorry for the confusion, I renamed
createForStack()tocreateFor(). The currentcreateForStack()is deprecated and requires argument labels. Here's the correct syntax@JohnEstropia commented on GitHub (May 28, 2016):
By the way,
will compile, but will raise an assertion because NSFetchedResultsControllers requires sorting information. Provide an
OrderByclause.I'll update the documentation.
@wm-j-ray commented on GitHub (May 28, 2016):
Got it and many thanks.
I'll give it a shot.
On May 27, 2016, 7:28 PM -0400, John Estropianotifications@github.com, wrote:
@wm-j-ray commented on GitHub (May 28, 2016):
OK, now I'm perplexed (and probably don't know what I'm doing)
Returns all of the records.
}
And I see the controller, but there is nothing in it...
@JohnEstropia commented on GitHub (May 28, 2016):
Did you set the NSFetchedResultsControllerDelegate? NSFetchedResultsControllers are lazy, and will not do actual fetching until you access it's array or unless you have a delegate that implements
controllerDidChangeContent(_:)@JohnEstropia commented on GitHub (May 28, 2016):
Try to print the content after performFetch()
@wm-j-ray commented on GitHub (May 28, 2016):
That was it. Thanks again and I can't tell you how much I appreciate your patience.
@JohnEstropia commented on GitHub (May 28, 2016):
No problem! Feel free to ask anything else anytime :)