Skip to content

Commit 3f45d81

Browse files
committed
Refactor loading store
- Check if migration needs to happen and invalidate; if so, purge things that store OIDs outside of store - Drop support for migrating 1.x stores
1 parent 7e261eb commit 3f45d81

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

Submariner/SBAppDelegate.swift

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,21 @@ fileprivate let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, catego
7373
NSInferMappingModelAutomaticallyOption: true,
7474
NSMigratePersistentStoresAutomaticallyOption: true
7575
]
76-
// migrate legacy stores when possible; legacyStoreFile
77-
// XXX: Error handling sufficient here?
78-
let oldURL = SBAppDelegate.legacyStoreFileName
76+
// check if the model needs a migration; we let Core Data do lightweight migrations and let us handle heavyweight,
77+
// but we should probably invalidate object IDs in the defaults DB. migration should handle OIDs in the store.
78+
// if we were doing the migration manually we could try to convert the ID, but we don't have this control with
79+
// NSMigratePersistentStoresAutomaticallyOption.
7980
let newURL = SBAppDelegate.storeFileName
80-
if FileManager.default.fileExists(atPath: oldURL.path) && !FileManager.default.fileExists(atPath: newURL.path) {
81-
let oldStore = try! self.persistentStoreCoordinator.addPersistentStore(type: .xml,
82-
configuration: nil,
83-
at: oldURL,
84-
options: storeOpts)
85-
// It's OK to ignore the return value, since it mutates the coordinator to include it
86-
_ = try! self.persistentStoreCoordinator.migratePersistentStore(oldStore,
87-
to: newURL,
88-
options: storeOpts,
89-
type: .sqlite)
90-
} else {
91-
// usual path, we aren't converting, but just using modern store
92-
_ = try! self.persistentStoreCoordinator.addPersistentStore(type: .sqlite,
93-
configuration: nil,
94-
at: newURL,
95-
options: storeOpts)
81+
if let metadata = try? NSPersistentStoreCoordinator.metadataForPersistentStore(type: .sqlite, at: newURL),
82+
!self.managedObjectModel.isConfiguration(withName: nil, compatibleWithStoreMetadata: metadata) {
83+
// SBDatabaseController defaults to local music for now
84+
UserDefaults.standard.removeObject(forKey: "LastViewedResource")
9685
}
86+
// we no longer migrate from Submariner 1.x stores. use 3.1.1 or older first beforehand
87+
_ = try! self.persistentStoreCoordinator.addPersistentStore(type: .sqlite,
88+
configuration: nil,
89+
at: newURL,
90+
options: storeOpts)
9791

9892
// #MARK: Init Core Data (managed object store)
9993
// must be main queue for SwiftUI
@@ -160,12 +154,6 @@ fileprivate let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, catego
160154

161155
// #MARK: - Application Files/Directories
162156

163-
static var legacyStoreFileName: URL {
164-
let libraryURL = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).last!
165-
let storeURL = libraryURL.appendingPathComponent("Submariner.storedata")
166-
return storeURL
167-
}
168-
169157
@objc static var musicDirectory: URL {
170158
let path = FileManager.default.urls(for: .musicDirectory, in: .userDomainMask).last!.appendingPathComponent("Submariner/Music")
171159
if !FileManager.default.fileExists(atPath: path.path) {

0 commit comments

Comments
 (0)