{"action":"created","issue":{"url":"https://api.github.com/repos/paulw11/Seam3/issues/45","repository_url":"https://api.github.com/repos/paulw11/Seam3","labels_url":"https://api.github.com/repos/paulw11/Seam3/issues/45/labels{/name}","comments_url":"https://api.github.com/repos/paulw11/Seam3/issues/45/comments","events_url":"https://api.github.com/repos/paulw11/Seam3/issues/45/events","html_url":"https://github.com/paulw11/Seam3/issues/45","id":261166673,"number":45,"title":"persistentStoreCoordinator.migratePersistentStore() fails with SMStore.type ","user":{"login":"tifroz","id":150367,"avatar_url":"https://avatars2.githubusercontent.com/u/150367?v=4","gravatar_id":"","url":"https://api.github.com/users/tifroz","html_url":"https://github.com/tifroz","followers_url":"https://api.github.com/users/tifroz/followers","following_url":"https://api.github.com/users/tifroz/following{/other_user}","gists_url":"https://api.github.com/users/tifroz/gists{/gist_id}","starred_url":"https://api.github.com/users/tifroz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tifroz/subscriptions","organizations_url":"https://api.github.com/users/tifroz/orgs","repos_url":"https://api.github.com/users/tifroz/repos","events_url":"https://api.github.com/users/tifroz/events{/privacy}","received_events_url":"https://api.github.com/users/tifroz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-09-28T01:34:57Z","updated_at":"2017-09-28T03:17:02Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"When migrating an existing sore (of type`NSSQLiteStoreType`) into a new one (of type `SMStore.type`), the Core Data stack throws an uncatchable error\r\n\r\n```\r\n2017-09-27 18:06:30.915044-0700 Seam3_Example[5110:986518] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM replaceObjectAtIndex:withObject:]: object cannot be nil'\r\n*** First throw call stack:\r\n(0x18201efe0 0x180a80538 0x181f01d2c 0x1843d58fc 0x1843e0630 0x100e49a10 0x100e565bc 0x1843cfd70 0x1843d52fc 0x1000ff424 0x1000ffce0 0x1881c1e48 0x1883ce37c 0x1883d3e24 0x1883e88b0 0x1883d10b8 0x183bc8884 0x183bc86f0 0x183bc8aa0 0x181fcd42c 0x181fccd9c 0x181fca9a8 0x181efada4 0x1881bafc8 0x1881b5c9c 0x100101ef8 0x180f0959c)\r\nlibc++abi.dylib: terminating with uncaught exception of type NSException\r\n```\r\n\r\nThe error can be duplicated by replacing the `didFinishLaunchingWithOptions` function in the *Example* project.\r\n\r\n```\r\nfunc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {\r\n      \r\n      let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last!\r\n      let oldURL = documentDirectory.appendingPathComponent(\"old.sqlite\")\r\n      let newURL = documentDirectory.appendingPathComponent(\"new.sqlite\")\r\n      let modelUrl = Bundle.main.url(forResource: \"Seam3Demo\", withExtension: \"momd\")!\r\n      if let mom = NSManagedObjectModel(contentsOf: modelUrl) {\r\n        let container = NSPersistentContainer(name: \"container\", managedObjectModel: mom)\r\n        \r\n        let psc = container.persistentStoreCoordinator\r\n        guard let store = try? psc.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: oldURL, options: nil) else {\r\n          fatalError(\"Failed to create old store\")\r\n        }\r\n        \r\n        let device = NSEntityDescription.insertNewObject(forEntityName: \"Device\", into: container.viewContext) as! Device\r\n        device.deviceID = \"123\"\r\n        guard let saved = try? container.viewContext.save() else {\r\n          fatalError(\"Can't save\")\r\n        }\r\n        \r\n        let exists = FileManager.default.fileExists(atPath: oldURL.path)\r\n        guard let oldStore = psc.persistentStore(for: oldURL) else {\r\n          fatalError(\"Failed to reference old store\")\r\n        }\r\n        \r\n        do {\r\n          SMStore.registerStoreClass()\r\n          SMStore.syncAutomatically = false\r\n         // try psc.migratePersistentStore(oldStore, to: newURL, options:nil, withType: NSSQLiteStoreType) // Does not crash\r\n          try psc.migratePersistentStore(oldStore, to: newURL, options:nil, withType:SMStore.type) // Crashes\r\n          // We never make it here when using SMStore.type\r\n        } catch {\r\n          // ...or here\r\n          fatalError(\"Failed to migrate store: \\(error)\")\r\n        }\r\n      }\r\n}\r\n```\r\n\r\nI did a good deal of troubleshooting but have unfortunately little to show for it. Here is what I know:\r\n- The single record created in the code above gets copied over to the new store as part of the migration, firing `SMStore`'s `func execute(_ request: NSPersistentStoreRequest, with context: NSManagedObjectContext?)`.\r\n- The `execute` function ends normally, returning an empty array as it should with a request type `.saveRequestType`.\r\n- The error occurs while executing core data stack code, before ` psc.migratePersistentStore()` actually completes.\r\n\r\nIn other words, there is something SMStore does while executing the insert command that upsets the Core Data stack ... but what could it be??"},"comment":{"url":"https://api.github.com/repos/paulw11/Seam3/issues/comments/332716157","html_url":"https://github.com/paulw11/Seam3/issues/45#issuecomment-332716157","issue_url":"https://api.github.com/repos/paulw11/Seam3/issues/45","id":332716157,"user":{"login":"paulw11","id":6835876,"avatar_url":"https://avatars2.githubusercontent.com/u/6835876?v=4","gravatar_id":"","url":"https://api.github.com/users/paulw11","html_url":"https://github.com/paulw11","followers_url":"https://api.github.com/users/paulw11/followers","following_url":"https://api.github.com/users/paulw11/following{/other_user}","gists_url":"https://api.github.com/users/paulw11/gists{/gist_id}","starred_url":"https://api.github.com/users/paulw11/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/paulw11/subscriptions","organizations_url":"https://api.github.com/users/paulw11/orgs","repos_url":"https://api.github.com/users/paulw11/repos","events_url":"https://api.github.com/users/paulw11/events{/privacy}","received_events_url":"https://api.github.com/users/paulw11/received_events","type":"User","site_admin":false},"created_at":"2017-09-28T03:17:02Z","updated_at":"2017-09-28T03:17:02Z","author_association":"OWNER","body":"This is interesting; it appears to be an Apple bug; they shouldn't do whatever they are doing with a `nil` object.  There are a few hits on this on the web, the fix in one case was source and target store urls being the same, and in the other case the guy just moved the database without the migration.\r\n\r\nI set a breakpoint and I can see the record being inserted into the Seam store and I pulled the application container down and the migration has taken place; the new.sqllite contains the single device record.\r\n\r\nIt seems that there is some operation that occurs after the migration that is causing the crash.\r\n\r\nI may have to create a smaller example and open a ticket with Apple."}}
{"actor":{"display_login":"paulw11"}}