I am trying to implement state restoration on a Core Data project (Swift) and am having problems with implementing the UIDataSourceModelAssociation protocol on the data source for a UITableView in a split view controller, which is a wrapper class around an NSFetchedResultsController. The code is :
1 extension EventDataProvider : UIDataSourceModelAssociation
2 {
3 public func modelIdentifierForElement(at idx: IndexPath, in view: UIView) -> String?
4 {
5 let elementAtIndexPath = self.fetchedResultsController.object(at: idx)
6
7 return String(describing: elementAtIndexPath.objectID.uriRepresentation())
8 }
9 public func indexPathForElement(withModelIdentifier identifier: String, in view: UIView) -> IndexPath?
10 {
11 if let url = URL(string: identifier),
12 let objectID = self.fetchedResultsController.managedObjectContext.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: url),
13 let object = self.fetchedResultsController.managedObjectContext.object(with: objectID) as? CDEvent
14 {
15 return self.fetchedResultsController.indexPath(forObject: object) as NSIndexPath?
16 }
17
18 return nil
19 }
20 }
I am getting an EXC_BAD_INSTRUCTION exception, which stops the debugger at the top of the AppDelegate class, on state restoration which seems to point to a problem with "static Foundation.IndexPath._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSIndexPath>) -> Foundation.IndexPath".
I use the restorationArchiveTool to convert the resulting data.data file from the savedState folder to a plist with the command ".../restorationArchiveTool --plist --structured -o path/to/outputfile
If I view the resulting plist file with Preview, I get the following :
kApplicationSelectedCellIndexPathsKey...( "<NSIndexPath: 0x7fe60054cb00> {length = 2, path = 0 - 3}")
... but if I open the plist in Xcode, I get the following :
kApplicationSelectedCellIndexPathsKey for the key but with just ( for the value
Presuming the decoder uses the same algorithm as the plist reader for converting the data file, it would not be surprising to get some kind of exception.
If I remove the UIDataSourceModelAssociation extension, the exception goes away.
Can anyone else confirm this problem or have I missed something really obvious?
your function has the signature:
indexPathForElement(withModelIdentifier identifier: String, in view: UIView) -> IndexPath?
thats why you should cast the return type to IndexPath
instead of NSIndexPath
:
return self.fetchedResultsController.indexPath(forObject: object) as NSIndexPath?
to
return self.fetchedResultsController.indexPath(forObject: object) as IndexPath?