mainContext ? Can I access it? #66

Closed
opened 2025-12-29 15:23:26 +01:00 by adam · 5 comments
Owner

Originally created by @wm-j-ray on GitHub (May 30, 2016).

John,

I have a collection of predicates that I'd like to use to create NSFetchedResultsControllers. I realize this is defeating a lot of the purpose of CoreStore, but nonetheless. I've tried:

CoreStore.mainContext and CoreStore.defaultStack.mainContext

lazy var fetchedResultsController: NSFetchedResultsController = {
// Initialize Fetch Request
let fetchRequest = NSFetchRequest(entityName: "Item")

// Add Sort Descriptors
let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]

// Initialize Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)

// Configure Fetched Results Controller
fetchedResultsController.delegate = self

return fetchedResultsController
}()
Originally created by @wm-j-ray on GitHub (May 30, 2016). John, I have a collection of predicates that I'd like to use to create NSFetchedResultsControllers. I realize this is defeating a lot of the purpose of CoreStore, but nonetheless. I've tried: CoreStore.mainContext and CoreStore.defaultStack.mainContext ``` lazy var fetchedResultsController: NSFetchedResultsController = { // Initialize Fetch Request let fetchRequest = NSFetchRequest(entityName: "Item") // Add Sort Descriptors let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true) fetchRequest.sortDescriptors = [sortDescriptor] // Initialize Fetched Results Controller let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: nil) // Configure Fetched Results Controller fetchedResultsController.delegate = self return fetchedResultsController }() ```
adam added the question label 2025-12-29 15:23:26 +01:00
adam closed this issue 2025-12-29 15:23:26 +01:00
Author
Owner

@JohnEstropia commented on GitHub (May 31, 2016):

You can use NSFetchedResultsController.createFor(...) to create a CoreStore-managed controller:

let controller = NSFetchedResultsController.createFor(
    CoreStore.defaultStack,
    From(Item),
    OrderBy(.Ascending("createdAt"))
)
controller.delegate = self
return controller

If you have raw NSPredicate and NSSortDescriptors, Where clauses and OrderBy clauses accept them in their initializers

let controller = NSFetchedResultsController.createFor(
    CoreStore.defaultStack,
    From(Item),
    Where(yourPredicate),
    OrderBy(yourSortDescriptors)
)
@JohnEstropia commented on GitHub (May 31, 2016): You can use `NSFetchedResultsController.createFor(...)` to create a CoreStore-managed controller: ``` swift let controller = NSFetchedResultsController.createFor( CoreStore.defaultStack, From(Item), OrderBy(.Ascending("createdAt")) ) controller.delegate = self return controller ``` If you have raw `NSPredicate` and `NSSortDescriptor`s, `Where` clauses and `OrderBy` clauses accept them in their initializers ``` swift let controller = NSFetchedResultsController.createFor( CoreStore.defaultStack, From(Item), Where(yourPredicate), OrderBy(yourSortDescriptors) ) ```
Author
Owner

@wm-j-ray commented on GitHub (May 31, 2016):

Thanks John. I'm learning albeit slowly.

On May 30, 2016, 6:21 PM -0400, John Estropianotifications@github.com, wrote:

You can useNSFetchedResultsController.createFor(...)to create a CoreStore-managed controller:

letcontroller=NSFetchedResultsController.createFor( CoreStore.defaultStack, From(Item), OrderBy(.Ascending("createdAt")) ) controller.delegate=selfreturncontroller

If you have rawNSPredicateandNSSortDescriptors,Whereclauses andOrderByclauses accept them in their initializers

letcontroller=NSFetchedResultsController.createFor( CoreStore.defaultStack, From(Item), Where(yourPredicate), OrderBy(yourSortDescriptors) )


You are receiving this because you authored the thread.
Reply to this email directly,view it on GitHub(https://github.com/JohnEstropia/CoreStore/issues/77#issuecomment-222560925), ormute the thread(https://github.com/notifications/unsubscribe/AAvDJPxhgMQRHjI6bxg0z4Jx1oicxbBnks5qG2LNgaJpZM4IqCef).

@wm-j-ray commented on GitHub (May 31, 2016): Thanks John. I'm learning albeit slowly. On May 30, 2016, 6:21 PM -0400, John Estropianotifications@github.com, wrote: > You can useNSFetchedResultsController.createFor(...)to create a CoreStore-managed controller: > > letcontroller=NSFetchedResultsController.createFor( CoreStore.defaultStack, From(Item), OrderBy(.Ascending("createdAt")) ) controller.delegate=selfreturncontroller > > If you have rawNSPredicateandNSSortDescriptors,Whereclauses andOrderByclauses accept them in their initializers > > letcontroller=NSFetchedResultsController.createFor( CoreStore.defaultStack, From(Item), Where(yourPredicate), OrderBy(yourSortDescriptors) ) > > — > You are receiving this because you authored the thread. > Reply to this email directly,view it on GitHub(https://github.com/JohnEstropia/CoreStore/issues/77#issuecomment-222560925), ormute the thread(https://github.com/notifications/unsubscribe/AAvDJPxhgMQRHjI6bxg0z4Jx1oicxbBnks5qG2LNgaJpZM4IqCef).
Author
Owner

@JohnEstropia commented on GitHub (May 31, 2016):

If you don't need an NSFetchedResultsController per se, I recommend you use ListMonitors instead. They're much more type-safe and handles a lot of NSFetchedResultsController bugs, and they also support "multiple delegates".

@JohnEstropia commented on GitHub (May 31, 2016): If you don't need an `NSFetchedResultsController` per se, I recommend you use `ListMonitor`s instead. They're much more type-safe and handles a lot of NSFetchedResultsController bugs, and they also support "multiple delegates".
Author
Owner

@wm-j-ray commented on GitHub (May 31, 2016):

I'm going to give it a shot. I want to start playing around with that DTTableManager library that abstracts out a lot of the lower level mechanics of tableviews and its just pure FRC. (At first blush).

I used to use TLIndexPathTools, very good but it's ObjC and I am trying to make the transition to Swift and get more toward a Clean Swift architecture.

Again, thanks so much for the help.

On May 30, 2016, 10:03 PM -0400, John Estropianotifications@github.com, wrote:

If you don't need anNSFetchedResultsControllerper se, I recommend you useListMonitors instead. They're much more type-safe and handles a lot of NSFetchedResultsController bugs, and they also support "multiple delegates".


You are receiving this because you authored the thread.
Reply to this email directly,view it on GitHub(https://github.com/JohnEstropia/CoreStore/issues/77#issuecomment-222577005), ormute the thread(https://github.com/notifications/unsubscribe/AAvDJIJjcM9k43imyJyzdjo4HfLASxf1ks5qG5cLgaJpZM4IqCef).

@wm-j-ray commented on GitHub (May 31, 2016): I'm going to give it a shot. I want to start playing around with that DTTableManager library that abstracts out a lot of the lower level mechanics of tableviews and its just pure FRC. (At first blush). I used to use TLIndexPathTools, very good but it's ObjC and I am trying to make the transition to Swift and get more toward a Clean Swift architecture. Again, thanks so much for the help. On May 30, 2016, 10:03 PM -0400, John Estropianotifications@github.com, wrote: > If you don't need anNSFetchedResultsControllerper se, I recommend you useListMonitors instead. They're much more type-safe and handles a lot of NSFetchedResultsController bugs, and they also support "multiple delegates". > > — > You are receiving this because you authored the thread. > Reply to this email directly,view it on GitHub(https://github.com/JohnEstropia/CoreStore/issues/77#issuecomment-222577005), ormute the thread(https://github.com/notifications/unsubscribe/AAvDJIJjcM9k43imyJyzdjo4HfLASxf1ks5qG5cLgaJpZM4IqCef).
Author
Owner

@JohnEstropia commented on GitHub (May 31, 2016):

If your app still have Objective-C code left you might want to play with CoreStore 2.0 (see corestore2_develop branch).

All CoreStore Swift types have their ObjectiveC counterparts (and bridgeable back and forth).

Just to give you more options :)

I'll close this issue for now. Feel free to ask anything else anytime!

@JohnEstropia commented on GitHub (May 31, 2016): If your app still have Objective-C code left you might want to play with CoreStore 2.0 (see corestore2_develop branch). All CoreStore Swift types have their ObjectiveC counterparts (and bridgeable back and forth). Just to give you more options :) I'll close this issue for now. Feel free to ask anything else anytime!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#66