Fixes “error migrating persistent store” issue

We now set AppVersion.sourceID during migration, which fixes AppVersion entries conflicting across different Sources if multiple contain the same app + version.
This commit is contained in:
Riley Testut
2022-09-20 13:12:47 -05:00
committed by Joseph Mattello
parent 8fcb897800
commit 54ccb9611e

View File

@@ -16,6 +16,11 @@ fileprivate extension NSManagedObject
return bundleID return bundleID
} }
var storeAppSourceID: String? {
let sourceID = self.value(forKey: #keyPath(StoreApp.sourceIdentifier)) as? String
return sourceID
}
var storeAppVersion: String? { var storeAppVersion: String? {
let version = self.value(forKey: #keyPath(StoreApp._version)) as? String let version = self.value(forKey: #keyPath(StoreApp._version)) as? String
return version return version
@@ -55,6 +60,7 @@ fileprivate extension NSManagedObject
downloadURL: URL, downloadURL: URL,
size: Int64, size: Int64,
appBundleID: String, appBundleID: String,
sourceID: String,
in context: NSManagedObjectContext) -> NSManagedObject in context: NSManagedObjectContext) -> NSManagedObject
{ {
let appVersion = NSEntityDescription.insertNewObject(forEntityName: AppVersion.entity().name!, into: context) let appVersion = NSEntityDescription.insertNewObject(forEntityName: AppVersion.entity().name!, into: context)
@@ -64,6 +70,7 @@ fileprivate extension NSManagedObject
appVersion.setValue(downloadURL, forKey: #keyPath(AppVersion.downloadURL)) appVersion.setValue(downloadURL, forKey: #keyPath(AppVersion.downloadURL))
appVersion.setValue(size, forKey: #keyPath(AppVersion.size)) appVersion.setValue(size, forKey: #keyPath(AppVersion.size))
appVersion.setValue(appBundleID, forKey: #keyPath(AppVersion.appBundleID)) appVersion.setValue(appBundleID, forKey: #keyPath(AppVersion.appBundleID))
appVersion.setValue(sourceID, forKey: #keyPath(AppVersion.sourceID))
return appVersion return appVersion
} }
} }
@@ -76,6 +83,7 @@ class StoreApp10ToStoreApp11Policy: NSEntityMigrationPolicy
try super.createDestinationInstances(forSource: sInstance, in: mapping, manager: manager) try super.createDestinationInstances(forSource: sInstance, in: mapping, manager: manager)
guard let appBundleID = sInstance.storeAppBundleID, guard let appBundleID = sInstance.storeAppBundleID,
let sourceID = sInstance.storeAppSourceID,
let version = sInstance.storeAppVersion, let version = sInstance.storeAppVersion,
let versionDate = sInstance.storeAppVersionDate, let versionDate = sInstance.storeAppVersionDate,
// let versionDescription = sInstance.storeAppVersionDescription, // Optional // let versionDescription = sInstance.storeAppVersionDescription, // Optional
@@ -95,6 +103,7 @@ class StoreApp10ToStoreApp11Policy: NSEntityMigrationPolicy
downloadURL: downloadURL, downloadURL: downloadURL,
size: Int64(size), size: Int64(size),
appBundleID: appBundleID, appBundleID: appBundleID,
sourceID: sourceID,
in: context) in: context)
destinationStoreApp.setStoreAppLatestVersion(appVersion) destinationStoreApp.setStoreAppLatestVersion(appVersion)