mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-11 20:00:30 +01:00
Observing two different Entity-Types on the same Controller #129
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 @brosnic on GitHub (Feb 28, 2017).
Hi!
i'm trying to observe two ListMonitors with two different ListEntityType (Task and Person) with no common parent Entity (except NSManagedObject).
I changed my ListObserver implementation like this:
func listMonitorWillChange(_ monitor: ListMonitor) {...}
func listMonitorDidChange(_ monitor: ListMonitor) {...}
func listMonitorWillRefetch(_ monitor: ListMonitor) {...}
func listMonitorDidRefetch(_ monitor: ListMonitor) {...}
But no i get this error when trying to call self.taskMonitor.addObserver(self) and self.personMonitor.addObserver(self):
Argument type "TasksAndPersonViewController" does not conform to expected type "ListSectionObserver"
When i implement "ListSectionObserver" the error changes to this:
Ambiguous reference to member 'addObserver'
Can you tell me if and how I can manage two different ListMonitors on one ViewController?
Thank you!
@JohnEstropia commented on GitHub (Feb 28, 2017):
ListMonitor is a generic type, so you need to provide different methods for each entities you need:
I'm not sure if the latest swift allows this though, but I think it should work as long as you don't explicitly assign a typealias to ListEntityType
@brosnic commented on GitHub (Feb 28, 2017):
unfortunately this doesn't work.
I got this compile error:
Type 'TasksAndPersonViewController' does not conform to protocol 'ListObserver'
@brosnic commented on GitHub (Feb 28, 2017):
Errors:
1: Ambiguous inference of associated type 'ListEntityType': 'Task' vs. 'Person' (CoreStore.ListObserver)
2. Matching requirement 'listMonitorDidChange' to this declaration inferred associated type to 'Task'
3. Matching requirement 'listMonitorDidChange' to this declaration inferred associated type to 'Person'
@JohnEstropia commented on GitHub (Mar 1, 2017):
Ah, I guess Swift got tighter about this. ListMonitor can probably add closure-based notifications for this use-case. In the mean time, you have two ways to work around this:
typealias ListEntityType = NSManagedObject). Basically your protocol implementation will be:You can keep your monitors' property type in their corresponding generic types
but you will need to downcast whenever you access
addObserver(..)andremoveObserver(...). Here's one way:Don't worry about the
unsafeBitCast()here, it just type-erases the generic type and there should be no side effects aside from type inferences.@brosnic commented on GitHub (Mar 1, 2017):
Works!
I use the downcast method and it works perfectly!
Thank you very much for your excellent work on CoreStore and for your quick help!