-
-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Noob Question : Access to default context and NSFetchedResultsController #76
Comments
The only NSManagedObjectContext exposed by CoreStore is the context held by let transaction = CoreStore.beginUnsafe() // store this somewhere so it doesn't get deallocated
let context = transaction.internalContext // this is what you need and you're free to do dirty things to it :) See the documentation on |
Also, note that this means the objects you are working on are not from the main context. Don't mix objects fetched from the // BAD!!
let someObject = CoreStore.fetchOne(From(...))
let transaction = CoreStore.beginUnsafe() // store this somewhere so it doesn't get deallocated
let context = transaction.internalContext
let anotherObject = // fetch from transaction/context
anotherObject.someRelationship = someObject // BAD! |
If all you need is an |
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. |
Let me push an update so the API call is simpler. Once it's up you should be able to call with let controller = NSFetchedResultsController.createForStack(
CoreStore.defaultStack,
From(MyEntity),
SectionBy("sectionKey"), // optional
Where("value > %@", minValue), // optional
OrderBy(.Ascending("value")) // optional
) 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). |
Check out version 1.6.8. Renamed |
Excellent. Thanks so much. |
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. |
Sorry for the confusion, I renamed let controller = NSFetchedResultsController.createFor(
CoreStore.defaultStack,
From(MyEntity),
SectionBy("sectionKey"), // optional
Where("value > %@", minValue), // optional
OrderBy(.Ascending("value")) // sorting is required
) |
By the way, let controller = NSFetchedResultsController.createFor(
CoreStore.defaultStack,
From(NoteMO)) will compile, but will raise an assertion because NSFetchedResultsControllers requires sorting information. Provide an I'll update the documentation. |
Got it and many thanks. On May 27, 2016, 7:28 PM -0400, John Estropianotifications@github.com, wrote:
|
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... |
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 |
Try to print the content after performFetch()
|
That was it. Thanks again and I can't tell you how much I appreciate your patience. |
No problem! Feel free to ask anything else anytime :) |
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
The text was updated successfully, but these errors were encountered: