@@ -280,15 +280,49 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
280280
281281 if (!shouldFail && !error) {
282282 NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc ] initWithManagedObjectModel: [self managedObjectModel ]];
283- NSURL *url = [applicationDocumentsDirectory URLByAppendingPathComponent: @" OSXCoreDataObjC.storedata" ];
284- NSDictionary *presistentStoreOptions = @{NSInferMappingModelAutomaticallyOption : @YES ,
283+ NSURL *xmlStoreURL = [applicationDocumentsDirectory URLByAppendingPathComponent: @" OSXCoreDataObjC.storedata" ];
284+ NSURL *sqliteStoreURL = [applicationDocumentsDirectory URLByAppendingPathComponent: @" OSXCoreDataObjC.sqlite" ];
285+ NSDictionary *persistentStoreOptions = @{NSInferMappingModelAutomaticallyOption : @YES ,
285286 NSMigratePersistentStoresAutomaticallyOption : @YES };
286- if (![coordinator addPersistentStoreWithType: NSXMLStoreType
287- configuration: nil
288- URL: url
289- options: presistentStoreOptions
290- error: &error]) {
291- coordinator = nil ;
287+
288+ // Use SQLite store for new installs; migrate existing XML stores for backward compatibility
289+ BOOL xmlStoreExists = [fileManager fileExistsAtPath: xmlStoreURL.path];
290+ BOOL sqliteStoreExists = [fileManager fileExistsAtPath: sqliteStoreURL.path];
291+
292+ if (xmlStoreExists && !sqliteStoreExists) {
293+ // Migrate existing XML store to SQLite
294+ NSPersistentStore *xmlStore = [coordinator addPersistentStoreWithType: NSXMLStoreType
295+ configuration: nil
296+ URL: xmlStoreURL
297+ options: persistentStoreOptions
298+ error: &error];
299+ if (xmlStore) {
300+ NSPersistentStore *sqliteStore = [coordinator migratePersistentStore: xmlStore
301+ toURL: sqliteStoreURL
302+ options: persistentStoreOptions
303+ withType: NSSQLiteStoreType
304+ error: &error];
305+ if (sqliteStore) {
306+ DDLogInfo (@" Successfully migrated Core Data store from XML to SQLite." );
307+ // Remove old XML store file after successful migration
308+ [fileManager removeItemAtURL: xmlStoreURL error: nil ];
309+ } else {
310+ DDLogError (@" Failed to migrate to SQLite store: %@ " , error.localizedDescription );
311+ coordinator = nil ;
312+ }
313+ } else {
314+ DDLogError (@" Failed to open existing XML store for migration: %@ " , error.localizedDescription );
315+ coordinator = nil ;
316+ }
317+ } else {
318+ // Use SQLite store (new install or already migrated)
319+ if (![coordinator addPersistentStoreWithType: NSSQLiteStoreType
320+ configuration: nil
321+ URL: sqliteStoreURL
322+ options: persistentStoreOptions
323+ error: &error]) {
324+ coordinator = nil ;
325+ }
292326 }
293327 _persistentStoreCoordinator = coordinator;
294328 }
@@ -301,7 +335,7 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
301335 if (error) {
302336 dict[NSUnderlyingErrorKey ] = error;
303337 }
304- error = [NSError errorWithDomain: @" YOUR_ERROR_DOMAIN " code: 9999 userInfo: dict];
338+ error = [NSError errorWithDomain: @" com.developerinsider.AppBox " code: 9999 userInfo: dict];
305339 [[NSApplication sharedApplication ] presentError: error];
306340 }
307341 return _persistentStoreCoordinator;
0 commit comments